• edited by
2,628 views
3 3 votes
#include<stdio.h>
int main()
{
    char c = 125;
    c = c+10;
    printf("%d", c);
    return 0;
}



what will be the output , explain with logic(size of char 1 byte)

2 Answers

Best answer
5 5 votes
char takes 1 byte. But when given to a function, these are promoted to int. So, positive char values are padded with 0s on the left and negatives ones with 1s (sign extension).

The maximum value which can be stored in a char is 127 if it is signed. Whether by default a char is signed or unsigned is "implementation defined" and lets assume signed here. Now, when we add 10, this becomes 135 which overflows and hence becomes 135-256 = -121 (2's complement representation). Now, if this is printed using %d, we get -121. If we use %u, we get UINT_MAX - 135.
• edited by
3 3 votes

C = 0111 1101  (125)
C = C + 10;
C = 1000 0111 (135)

%d is format specifier for signed integers which treats numbers in 2's complement form.
2's complement of 1000 0111 will be 0111 1001 = -121 

Hence O/P will be -121.

Position:
Show:

Related questions

0 0 votes
1 1 answer
462
462 views
Rajib Datta Roy asked Dec 26, 2023
462 views
#include <iostream>using namespace std;int main(){ float a,b,c; cout<<"Enter a="; cin>>a; b=a + (1-1); cout<<"a + (1-1)="<<b<<endl; c=(a+1)-1; cout<...
1 1 vote
3 3 answers
1.0k
1.0k views
shiva0 asked Apr 3, 2019
1,033 views
What is the output of the program? int main() { union a { int i; char ch ; }; union a u; u.ch[0] = 3; u.ch = 2; printf("%d, %d, %d", u.ch[0], u.ch , u.i); return 0; }
1 1 vote
1 answers 1 answer
1.5k
1.5k views
srestha asked Aug 8, 2018
1,486 views
#include <stdio.h int main() { long int a = 0x7fffffff * 0x7fffffff; long int b = 0x7fffffff * 0x7fffffffl; printf("a = %ld, b = %ld\n", a, b); return 0; }What will be ou...
0 0 votes
1 1 answer
716
716 views
radha gogia asked Aug 10, 2015
716 views
CASE A: CASE A: &#8234;#&lrm;include&#8236;<stdio.h int divide( int a, b) { return 7; } int main() { int a=divide(8,3); printf("%d",a); return 0; }CASE B :CASE B : #inclu...