GATE CSE
First time here? Checkout the FAQ!
x
+3 votes
73 views

asked in Programming by Active (1.3k points)   | 73 views
-121. range of character -128 to 127. after 127 , -128 comes and so on. it is cyclic. so add 10 to 127 will give -121

Output would most likely be -121. However, there is a catch. Please check this.

2 Answers

+3 votes
Best answer

As per C standard, there are three distinct types.

  1. a char
  2. a signed char
  3. an unsigned char

 gcc by default takes plain char as signed char. in a 2's complement machine following things would happen when ever char value exceeds $127$.

  • Likewise in the above program, it will print $-121$

 


The following program will stop for an unsigned char. But loop forever for a signed char

 

#include <stdio.h>
int main() {
	unsigned char c = 0; // loops forever for char c = 0;
	while(c < 130) {
		printf("hello\n");
		c++;
	}
	return 0;
}

 

answered by Veteran (47.1k points)  
selected by
+1 vote

We should know the below concepts before solving this question:-

Concept1:- char c ; means by default signed char c;

Conecpt2:- C Follows 2's Complement representation.

Concept3(Follows from Concept2):-(Imp):- Whatever U store it stored in Binary Number but while retrieval  it gives 2's Complement of Binary Number

So here c=125+10=135

(135)10 = (1000 0111)

So in memory 1000 0111 will store which is binary equivalent of 135

but when we will print char C means ->>( 1000 0111 )it will give us the 2's Complement representation of c which is -121.

Hence -121 is Ans.

 

answered by Veteran (16.8k points)  
edited by

Related questions

0 votes
1 answer
2
asked ago in Programming by Manu FG (21 points)   | 39 views
+4 votes
1 answer
3
asked in Programming by Rohan Mundhey Loyal (4.3k points)   | 83 views


Top Users May 2017
  1. akash.dinkar12

    3568 Points

  2. pawan kumarln

    2206 Points

  3. Bikram

    1940 Points

  4. sh!va

    1682 Points

  5. Arjun

    1650 Points

  6. Devshree Dubey

    1272 Points

  7. Debashish Deka

    1270 Points

  8. Angkit

    1056 Points

  9. LeenSharma

    1028 Points

  10. Arnab Bhadra

    904 Points

Monthly Topper: Rs. 500 gift card
Top Users 2017 May 22 - 28
  1. Bikram

    1026 Points

  2. pawan kumarln

    832 Points

  3. Arnab Bhadra

    818 Points

  4. akash.dinkar12

    448 Points

  5. Arjun

    378 Points


22,897 questions
29,213 answers
65,336 comments
27,713 users