Output of C Programs | Set 10

Predict the output of the below programs. 
Difficulty Level: Rookie
Question 1 

c

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
int main()
{
   typedef int i;
   i a = 0;   
   printf("%d", a);
   getchar();
   return 0;
}   

chevron_right


Output: 0 
There is no problem with the program. It simply creates a user defined type i and creates a variable a of type i.
Question 2 

c

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
int main()
{
  typedef int *i;
  int j = 10;
  i *a = &j;   
  printf("%d", **a);
  getchar();
  return 0;
}   

chevron_right


Output: Compiler Error -> Initialization with incompatible pointer type. 
The line typedef int *i makes i as type int *. So, the declaration of a means a is pointer to a pointer. The Error message may be different on different compilers. 

One possible correct solution of this code is in Question 4.  Also now try this:

C

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
 
int main()
{
  typedef int *i;
  int j = 10;
  int *p = &j;
  i *a = &p;   
  printf("%d", **a);
  getchar();
  return 0;
}

chevron_right


Question 3  

c

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
int main()
{
  typedef static int *i;
  int j;
  i a = &j;   
  printf("%d", *a);
  getchar();
  return 0;
}   

chevron_right


Output: Compiler Error -> Multiple Storage classes for a. 
In C, typedef is considered as a storage class. The Error message may be different on different compilers. 
Question 4 

c

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
int main()
{
  typedef int *i;
  int j = 10;
  i a = &j;   
  printf("%d", *a);
  getchar();
  return 0;

chevron_right


Output: 
10 
Please write comments if you find any of the answers/explanations incorrect, or you want to share more information about the topics discussed above. 
References: 
http://www.itee.uq.edu.au/~comp2303/Leslie_C_ref/C/SYNTAX/typedef.html 
http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc03sc03.htm 
http://msdn.microsoft.com/en-us/library/4x7sfztk.aspx




My Personal Notes arrow_drop_up

Article Tags :

10


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