Open In App

Output of C Program | Set 17

Improve
Improve
Like Article
Like
Save
Share
Report

Predict the output of following C programs.

Question 1




#include<stdio.h>
  
#define R 10
#define C 20
  
int main()
{
   int (*p)[R][C];
   printf("%d"sizeof(*p));
   getchar();
   return 0;
}


Output: 10*20*sizeof(int) which is “800” for compilers with integer size as 4 bytes.
The pointer p is de-referenced, hence it yields type of the object. In the present case, it is an array of array of integers. So, it prints R*C*sizeof(int).
Thanks to Venki for suggesting this solution.



Question 2




#include<stdio.h>
#define f(g,g2) g##g2
int main()
{
   int var12 = 100;
   printf("%d", f(var,12));
   getchar();
   return 0;
}


Output: 100
The operator ## is called “Token-Pasting” or “Merge” Operator. It merges two tokens into one token. So, after preprocessing, the main function becomes as follows, and prints 100.




int main()
{
   int var12 = 100;
   printf("%d", var12);
   getchar();
   return 0;
}




Question 3




#include<stdio.h>
int main() 
{
   unsigned int x = -1;
   int y = ~0;
   if(x == y)
      printf("same");
   else
      printf("not same");
   printf("\n x is %u, y is %u", x, y);
   getchar();
   return 0;
}


Output: “same x is MAXUINT, y is MAXUINT” Where MAXUINT is the maximum possible value for an unsigned integer.
-1 and ~0 essentially have same bit pattern, hence x and y must be same. In the comparison, y is promoted to unsigned and compared against x. The result is “same”. However, when interpreted as signed and unsigned their numerical values will differ. x is MAXUNIT and y is -1. Since we have %u for y also, the output will be MAXUNIT and MAXUNIT.
Thanks to Venki for explanation.

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



Last Updated : 31 Jul, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads