search
Log In
4 votes
4.7k 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
in Programming
recategorized by
4.7k 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.
0

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

useful to understand Big endian and little endian.

2 Answers

13 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
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.
4 votes
Little endian is given, so A
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.
Answer:

Related questions

5 votes
3 answers
1
3.2k views
Consider the following declaration : structaddr { char city[10]; char street[30]; int pin; }; struct { char name[30]; int gender; struct addr locate; } person, *kd = &person; Then *$(kd->name + 2)$ can be used instead of: $person.name+2$ $kd-> (name+2)$ $*((*kd).name+2)$ either $A)$ or $B)$ , not $C)$
asked Apr 22, 2018 in Programming Arjun 3.2k views
1 vote
3 answers
2
1.4k views
A language with string manipulation facilities uses the following operations. head$(s)$- returns the first character of the string $s$ tails$(s)$- returns all but the first character of the string $s$ concat$(s1, s2)$- concatenates string $s1$ with $s2.$ The output of concat(head$(s)$, head(tail(tail$(s)$))), where s is $acbc$ is: ab ba ac aa
asked Apr 22, 2018 in Programming Arjun 1.4k views
3 votes
1 answer
3
2k views
Which of the following comparisons between static and dynamic type checking incorrect? Dynamic type checking slows down the execution Dynamic type checking offers more flexibility to the programmers In contrast to Static type checking, dynamic type checking may cause failure in runtime due to type errors Unlike static type checking dynamic type checking is done during compilation
asked Apr 22, 2018 in Programming Arjun 2k views
3 votes
3 answers
4
1.9k views
Consider the following C++ program int a (int m) {return ++m;} int b(int&m) {return ++m;} int{char &m} {return ++m;} void main() { int p = 0, q=0, r = 0; p += a(b(p)) ; q+= b(a(q);) r+=a(c(r)); cout<<p<<q << r; } Assuming the required header first are already included, the above program results in compilation error print $123$ print $111$ print $322$
asked Apr 22, 2018 in Programming Arjun 1.9k views
...