in Programming in C retagged by
8,509 views
5 votes
5 votes

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
in Programming in C retagged by
by
8.5k views

9 Comments

A. 0
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.
0
0
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
1
1

It gives the below error:

0
0
Here integer typecasting to  char pointer

Without last pointer code will work
1
1
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.
0
0
which year of ISRO paper is this?
0
0
22 april 2018
0
0

https://www.geeksforgeeks.org/little-and-big-endian-mystery/

useful to understand Big endian and little endian.

2
2

2 Answers

18 votes
18 votes
Best answer
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)

selected by
by

4 Comments

I too marked it (c) i.e. 1

But due to compilation error in the actual code, none of the options is correct.
0
0
What is answer in ISRO key
0
0
Given the option then I feel Option $\mathbf{(d)}$ is safe to mark.
0
0
6 votes
6 votes
Little endian is given, so A

2 Comments

What will be the output if Big Endian was given?
0
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.
0
0
Answer:

Related questions