Output of C Programs | Set 14

Predict the output of below C programs.

Question 1

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
int main()
{
   int a;
   char *x;
   x = (char *) &a;
   a = 512;
   x[0] = 1;
   x[1] = 2;
   printf("%d\n",a);
  
   getchar();
   return 0;

chevron_right


Answer: The output is dependent on endianness of a machine. Output is 513 in a little endian machine and 258 in a big endian machine.



Let integers are stored using 16 bits. In a little endian machine, when we do x[0] = 1 and x[1] = 2, the number a is changed to 00000001 00000010 which is representation of 513 in a little endian machine. The output would be same for 32 bit numbers also.

In a big endian machine, when we do x[0] = 1 and x[1] = 2, the number is changed to 00000001 00000010 which is representation of 258 in a big endian machine.



Question 2

filter_none

edit
close

play_arrow

link
brightness_4
code

int main()
{
  int f = 0, g = 1;
  int i;
  for(i = 0; i < 15; i++) 
  {
    printf("%d \n", f);
    f = f + g;
    g = f - g;
  }
  getchar();
  return 0;
}

chevron_right


Answer: The function prints first 15 Fibonacci Numbers.



Question 3
Explain functionality of following function.

filter_none

edit
close

play_arrow

link
brightness_4
code

int func(int i)
{
  if(i%2) return (i++);
  else return func(func(i-1));
}

chevron_right


Answer: If n is odd then returns n, else returns (n-1). So if n is 12 then we get and if n is 11 then we get 11.

Please write comments if you find any of the answers/explanations incorrect, or you want to share more information about the topics discussed above.



My Personal Notes arrow_drop_up


Article Tags :

3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.