Friday, 15 January 2016

Programming Output Questions in C

1. Find the output of the following code:

#include<stdio.h>
main()
{
  int variable1=1;
  int variable2=12;
  int variable3=12;
  variable1=variable2==variable3;
  printf("%d",variable1);
}

OUTPUT:   1
12==12 implies it's true , therefore variable1=1


2. Find the output of the following code:

#include<stdio.h>
main()
{
  char *pointer;
  printf("%d %d",sizeof(*pointer),sizeof(pointer));
}

OUTPUT:   1   2
sizeof(pointer) gives the value 2 because pointer is a character pointer which needs two bytes
to store the address while storing the value of a character requires 1 byte.


3. Find the output of the following code:

#include<stdio.h>
main()
{
  int a=7;
  printf("%d",a=++a==8)
}

OUTPUT   1
The resolved expression is a=(++a==8) , therefore the inner expression results in TRUE i.e. 1.

4. Find the output of the following code:

#include<stdio.h>
#include<conio.h>
main()
{
    int *m,n[10];
    printf("Enter the values of m and n \n");
    scanf("%d \t %d",&m,&n);
    //  Suppose m=5 and n=10
    printf("%d \t %d",m,n);
}

 OUTPUT   5      garbage value
m is a pointer variable and it's value is 5 which is printed as it is
n is an array, it prints garbage value

No comments:

Post a Comment