Monday, January 5, 2009

C Programming Language coding question with solution

What will be output ?
(1)
#include"stdio.h"
void main()
{
union k
{
int p;
union k *up;
} *u1;
clrscr();
u1=(union k*)malloc(sizeof(union k));
u1->p=10;
u1->p=NULL;
free(u1);
printf("%d",*u1);
getch();
}


output: 0
(2)

#include"stdio.h"
void main()
{
typedef union XXX
{
int a;
char b;
}myunion;
myunion myvalue={15,'Z'};
clrscr();
printf("%d",myvalue.a);
getch();
}


output:error we can only initialize first member of union

(3)

#define show(p) -##p
void main()
{
int q=11;
clrscr();
printf("%d",show(-q));
getch();
}


output:10
(4)

void main()
{
int b;
clrscr();
for(b=1;b<5;)
{
int b;
printf("hello\n");
b=5;
}
getch();
}


output:infinite loop
(5)

void main()
{
float *p=(float *)malloc(25.5);
clrscr();
printf("%d",sizeof(p));
getch();
}


output:2

No comments:

Blog List