Saturday, March 17, 2012

What is the output ?

6. Give sizeof(a) in bytes for the following: (assume 32bit PC)

char *a[10];
char (*a)[10];
char (**a)[20];
char (*a)(int x, char c);

Ans: 40 , 4 , 4 , 4

7. Find out the output :

#include<stdio.h>
main()
{
    int n1 = 10, n2 = 12;
    if( n2=!0)
        printf("Result is:%d\n",(n1/n2));
    else
        printf("cannot divide by zero\n");
}
Ans: Result is:10

8. Find out the output:

#include<stdio.h>
main()
{
    long int one_million;
    one_million=1,000,000;
    printf("One million is %d",one_million);
}
Ans: One million is 1

9.  what is the output ?

#include<stdio.h>
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)
main()
{
    printf("%s %s\n",h(f(1,2)),g(f(1,2)));
}
Ans: 12 f(1,2)

10. what is the output?

#include<stdio.h>
main()
{
    static int j=printf("%d",456);;
    printf("%d",j);
}
Ans: Error

11. What is the output ?

#include<stdio.h>
main()
{
int i = 5;
int j;
j=sizeof(i++);
printf("%d",i);
}
Ans: 5

12. What is the output of z?

#include<stdio.h>
main()
{
int z,x=5,y=-10,a=4,b=2;
z=x++ - --y * b / a;
}
Ans: 10

What is the output ?

1. what is the output of the following program ? “alloc failed or alloc successful” ?

#include<stdio.h>
void alloc(int *p)
{
p=(int*)malloc(10*sizeof(*p));

}
main()
{
int *p=NULL;
alloc(p);
if(NULL==p)
printf("alloc failed");
else
printf("alloc successful");
}
Ans: alloc failed

make changes to get “alloc successful” as output.

2. what is the output of the program ?

#include<stdio.h>
main()
{
    struct node
    {
            int a;
            int b;
            int c;
    };
    struct node s={3,5,6};
    struct node *pt=&s;
    printf("%d",*(int*)pt);
    }
Ans: 3

3.  what is the output of the following program ?

#include<stdio.h>
main()
{
    printf(3+"Hello World");
    }
Ans: lo World

4. what is the output of the following program ?

#include<stdio.h>
main()
{
    int a=0;
    if(a=2)
    printf("I am inside IF\n");
    printf("It is correct");
    }
Ans:

I am inside IF
It is correct

5. what is the output ?

#include<stdio.h>
main()
{
    unsigned i=5;
    if(i > -5)
        printf("it is less than -5");
}
Ans: No output