Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Search Your Question

Thursday, July 24, 2008

C Programming Puzzles

C programming is very important topic for freshers, interviewers usually asks questions from C,C++,Data structures when it comes to programming. So i am starting this Topic , i will gather some good puzzles and keep on continuing this topic. I will also post the answers to the puzzles listed here soon.

The puzzles mostly covers on typo mistakes, basic understanding of some C concepts , i will explain the answers tho these puzzles covering those concepts also, And please use comment section.

A small poll here, should i post the answers just below each puzzle or should i post the answer in some other post and link it from here. I will post the answers in 3 days.Poll through comments.


1)
#include

main()
{
auto int i = 0;
printf("%d\n",i);

{
int i = 2;
printf("%d\n",i);
{
i+=1;
printf("%d\n",i);
}
printf("%d\n",i);
}
printf("%d\n",i);
printf("%d\n",reset());
printf("%d\n",ret10());
printf("%d\n",reset());
printf("%d\n",ret10());
}


int reset()
{
int j = 0;
return(j);
}

int ret10()
{
static int i = 10;
i+=1;
return(i);
}


2)
#include
#include
main()
{
struct emp1
{
char *name;
int age;
};
struct emp2
{
char *cp;
struct emp1 e1;
}e2 = {"ghi",{"jkl",123}};

struct emp1 e3 = {"rwer",2341};
printf("\n%s %d\n",e3.name,e3.age);
printf("\n%s %s %d\n",e2.cp,e2.e1.name,e2.e1.age);
}

3)
#include
struct xyz
{
int xyz ;
}
;

main()
{
union xyz
{
int xyz;
}
;
}

4)
#include
main()
{
char s[] = "Bouquets and Brickbats";
printf("\n%c, ",*(&s[2]));
printf("%s, ",s+5);
printf("\n%s",s);
printf("\n%c",*(s+2));
}

5)
#include
#include
struct s
{
char *st;
struct s *sptr;
};
main()
{
int i;
struct s *p[3];
static struct s a[]={
{"abcd",a+1},
{"pqrs",a+2},
{"stuv",a}
};
for( i=0;i<3;i++ )
p[i] = a[i].sptr;
swap(*p,a);
printf("%s %s %s \n",p[0]->st,(*p)->st, (*p)->sptr->st);
}

swap(p1,p2)
struct s *p1,*p2;
{
char *temp;
temp = p1->st;
p1->st = p2->st;
p2->st = temp;
}

6)
#include
Swap( int *x , int *y)
{
int tmp = *x ;
*x = *y ;
*y = tmp;
}
main()
{
int a = 1, b = 2;
Swap(&a, &b);
printf("%d %d\n", a, b);
}

7)
#include
main()
{
int i;
scanf("%d",&i);
switch(i) {
printf("\nHello");
case 1: printf("\none");
break;
case 2: printf("\ntwo");
break;
}
}

8)
#include
main()
{
int x;
x = 3;
f(x);
printf("MAIN");

}

f(int n)
{
printf("F");
if (n != 0)
f(n-1);
}


9)
#include
main()
{
int ptr[] = {1,2,23,6,5,6};
char str[] = {'a','b','c','d','e','f','g','h'};

printf("pointer differences are %ld, %d",&ptr[3], &str[3]-&str[0]);
}

10)
#include
main()
{
char a,b,c;
scanf("%c %c %c",&a,&b,&c);
printf("%c %c %c ", a, b, c);
}

11)
#include
#define PRINT(int) printf( "int = %d ", int)
main()
{
int x=03,y=02,z=01;
PRINT (x | y & ~z);
PRINT (x & y && z);
PRINT (x ^ (y & ~z));
}

12)
#include
main()
{
int a = 10000;
char b='c';
int i,j;

printf("%d,%d",printf("%d\n",a),printf("%c\n",b));
}

13)
#include
#define PR(a) printf("%d\t",(int) (a));
#define PRINT(a,b,c) PR(a);PR(b);PR(c);
#define MAX(a,b) (amain(){
int x = 1,y = 2;
PRINT(MAX(x++,y),x,y);
PRINT(MAX(x++,y),x,y);
}

14)
#include
main()
{
unsigned int i=1;
for(;i>=0;i--) printf("hello: %u\n",i);
}

15)
#include
main()
{
struct ist{
int x;
int y;
};

struct list{
int x;
struct ist next;
}head;
head.x = 100;
head.next.x=10;
printf("%d %d", head.x,head.next.x);
}

16)
#include
main()
{
typedef union
{
struct
{
char c1,c2;
} s;
long j;
float x;
} U;

U example;
example.s.c1 = 'a';
example.s.c2 = 'b';
example.j = 5;
printf("%c %c %d", example.s.c1, example.s.c2, example.j);
}

17)
#include
main()
{
struct s1
{ char *str;
struct s1 *ptr;
};
static struct s1 arr[] = { {"Hyderabad",arr+1},{"Bangalore",arr+2},
{"Delhi",arr}};
struct s1 *p[3];
int i;

for(i=0;i<=2;i++)
p[i] = arr[i].ptr;

printf("%s\n",(*p)->str);
printf("%s\n",(++*p)->str);
printf("%s\n",((*p)++)->str);
}

18)
#include
main()
{
struct s1
{ char *str;
struct s1 *ptr;
};
static struct s1 arr[] = {{"Hyderabad",arr+1},
{"Bangalore",arr+2},
{"Delhi",arr}
};
struct s1 *p[3];
int i;

for(i=0;i<=2;i++) p[i] = arr[i].ptr;

printf("%s ",(*p)->str);
printf("%s ",(++*p)->str);
printf("%s ",((*p)++)->str);
}

19)
#include
main()
{
char input[] = "SSSWILTECH1\1\1";
int i, c;
for ( i=2; (c=input[i])!='\0'; i++){
switch(c){
case 'a': putchar ('i'); continue;
case '1': break;
case 1: while (( c = input[++i]) != '\1' && c!= '\0');
case 9: putchar('S');
case 'E': case 'L': continue;
default: putchar(c);continue;
}
putchar(' ');
}
putchar('\n');
}

20)
#include
main()
{
int i, n, m, b, x[25];
int f1(int, int, int j[25]);
for(i=0;i<25;i++) x[i] = i;
i=0; m = 24;
b=f1(i, m, x);
printf("res %d\n",b);
}

int f1( int p, int q, int a[25])
{
int m1,m2;
if (q==0)
return(a[p]);
else
{
m1 = f1 (p, q/2, a);
m2 = f1(p+q/2+1,q/2,a);
if(m1 return (m2);
else
return(m1);
}
}

21)
#include
main()
{
int a[3][4] ={1,2,3,4,5,6,7,8,9,10,11,12} ;
int i,j,k=99 ;
for(i=0;i<3;i++)
for(j=0;j<4;j++)
if(a[i][j] < k) k = a[i][j];
printf("%d", k);
}

22)
#include
main()
{
int a,b,c;
for (b=c=10;a= "Love Your INDIA \
TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJq TNn*
RPn/QPbEWS_JSWQAIJO^NBELP\
eHBFHT}TnALVlBLOFAKFCFQHFOQIAIREETMSQGCSQOU
HATFAJKSbEALGSkMCSlOASn^r\
^r\\tZvYxXyT|S~Pn SPm SOn TNn
ULo0ULo#ULo-WHq!WFs XDt!"[b+++6];)
while(a-->64) putchar (++c=='Z'?c=c/9:33^b&1);
}

23)
#include
main()
{
char *p = "hello world!";
*(p+0) = 'H';
printf("%s",p);
}

24)
#include
main()
{
unsigned int m[] = { 0x01,0x02, 0x04, 0x08,0x10, 0x20, 0x40, 0x80};
unsigned int n,i;
printf("%d",m[7]);
scanf("%d",&n);
for(i=0;i<=7;i++)
{ if (n& m[i])
printf("\nyes");
else
printf("\nno");
}
}

25)
#include
main()
{
int a,b=2,c;
int *pointer;
c = 3;
pointer = &c;
a = c/*pointer*/;
b = c /* assigning 3 to b*/;
printf("a = %d; b = %d", a,b);
}

solve them and comment the solutions.

2 comments:

honey said...

i want solutions to the above puzzles

honey said...

i want solutions to the above puzzles

Archives