C Puzzles

Posted by Unknown at 18:42

Interview Questions

Write the output of this program
#include
main()
{
int *a, *s, i;
s = a = (int *) malloc( 4 * sizeof(int));
for (i=0; i<4; i++) *(a+i) = i * 10;
printf("%d\n", *s++);
printf("%d\n", (*s)++);
printf("%d\n", *s);
printf("%d\n", *++s);
printf("%d\n", ++*s);
}
Checkout this program result
#include
void fn(int);
static int val = 5;
main()
{
while (val --) fn(val);
printf("%d\n", val);
}
void fn(int val)
{
static int val = 0;
for (; val < 5; val ++) printf("%d\n", val);
}

Can you predict the output of this program ?
#include
main()
{
typedef union {
int a;
char b[10];
float c;
} Union;
Union x, y = { 100 };
x.a = 50;
strcpy (x.b, "hello");
x.c = 21.50;
printf ("Union 2 : %d %s %f\n", x.a, x.b, x.c);
printf ("Union Y : %d %s %f\n", y.a, y.b, y.c);
}


Print the output of the program
#include
main()
{
struct Data {
int a;
int b;
} y[4] = { 1, 10, 3, 30, 2, 20, 4, 40};
struct Data *x = y;
int i;
for(i=0; i<4; i++) {
x->a = x->b, ++x++->b;
printf("%d %d\t", y[i].a, y[i].b);
}
}


Write the output of this program
#include
main()
{
typedef struct {
int a;
int b;
int c;
char ch;
int d;
}xyz;
typedef union {
xyz X;
char y[100];
}abc;
printf("sizeof xyz = %d sizeof abc = %d\n",
sizeof(xyz), sizeof(abc));
}


Find out the error in this code
#define Error(str) printf("Error : %s\n", str); exit(1);
main()
{
int fd;
char str[20] = "Hello! Test me";
if ((fd = open("xx", O_CREAT | O_RDWR)) < 0)
Error("open failed");
if (write(fd, str, strlen(str)) < 0)
Error("Write failed");
if (read(fd, str, strlen(str)) < 0)
Error("read failed");
printf("File read : %s\n", str);
close(fd);
}


What will be the output of this program ?
main()
{
int *a, i;
a = (int *) malloc(10*sizeof(int));
for (i=0; i<10; i++)
*(a + i) = i * i;
for (i=0; i<10; i++)
printf("%d\t", *a++);
free(a);
}



click the Below link download this file 


If you enjoyed this post and wish to be informed whenever a new post is published, then make sure you subscribe to my regular Email Updates. Subscribe Now!


Kindly Bookmark and Share it:

YOUR ADSENSE CODE GOES HERE

0 comments:

Have any question? Feel Free To Post Below:

 

© 2011. All Rights Reserved | Interview Questions | Template by Blogger Widgets

Home | About | Top