recategorized by
487 views
1 votes
1 votes

Can you see why the output of the following code is 256?

main ()
    {
        int a=0x1ff;
        char *careful= &a;
        *careful = 0;
        printf("%d", a);
    }
recategorized by

2 Answers

0 votes
0 votes
char is a signed type. It takes value from -128 to 127

Now, u r taking a value which is in int and pointing by a character pointer.Now u make the value pointed by char pointer is 0. Now, again trying to print that value to print in integer, which will be out of range. So, it gives an overflow and that makes 9th bit as 1.

Related questions

2 votes
2 votes
2 answers
3
atulcse asked Jan 15, 2022
661 views
Consider the following programint find (int n) { int a = 1; for (i = 1; i < = n; i ++) for (j = 1; j < = i; j++) for (k = 1; k <= j, k++) a = a + 1; ...
0 votes
0 votes
3 answers
4
ramakrushna asked Dec 23, 2021
685 views
What should be the ans. Isn’t the Function initialization should be outside main function? They are given inside main. If declaration would be outside main then ans sho...