3.5k views

Consider the following program

{
int x=1;
printf("%d",(*char(char*)&x));
}

Assuming required header files are included and if the machine in which this program is executed is little endian, then the output will be

1. 0
2. 99999999
3. 1
4. unpredictable

recategorized | 3.5k views
0
A. 0
0
I think, it should be either "no output"(gcc) or "error"(zapcc) depending on the type of compiler used. The problem is with "*char" part because we don't need to typecast it again to access the value stored at the type-casted char pointer. Thus, option D should be the answer.
+1
unpredictable means undefined behavior?

It is working on certain value 1, then why will be undefined behavior?

Moreover it is working on little endian machine
0

It gives the below error:

+1
Here integer typecasting to  char pointer

Without last pointer code will work
0
In that case, the answer would be 1. But since they have used *char, it won't work. We don't need to typecast it(again) to get the value stored at the char pointer.

main()
{
int x=1;
printf("%d",(*char(char*)&x));
}

Here,we will get a compilation error because 'char' is extra.

If the code is :

main()
{
int x=1;
printf("%d",(*(char*)&x));
}

OUTPUT: 1 option(c)

by Boss (10.8k points)
selected by
0
Yes u r right..then which option is correct....?
+1
We can challenge this question,I suppose.
0
ok..done
0
I too marked it (c) i.e. 1

But due to compilation error in the actual code, none of the options is correct.
0
What is answer in ISRO key
0
Given the option then I feel Option $\mathbf{(d)}$ is safe to mark.
Little endian is given, so A
by Junior (725 points)
0
What will be the output if Big Endian was given?
0
if its big endian the number 1 can be represented as 00000001 00000000 00000000 00000000 if int occupies four bytes hence the answer will be 1.