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 <stdio.h>

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 <stdio.h>
#include<string.h>
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 <stdio.h>
struct xyz
{
int xyz ;
}
;

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

4)
#include <stdio.h>
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 <stdio.h>
#include <stdio.h>
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 <stdio.h>
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 <stdio.h>
main()
{
int i;
scanf("%d",&i);
switch(i) {
printf("\nHello");
case 1: printf("\none");
break;
case 2: printf("\ntwo");
break;
}
}

8)
#include <stdio.h>
main()
{
int x;
x = 3;
f(x);
printf("MAIN");

}

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


9)
#include <stdio.h>
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 <stdio.h>
main()
{
char a,b,c;
scanf("%c %c %c",&a,&b,&c);
printf("%c %c %c ", a, b, c);
}

11)
#include <stdio.h>
#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 <stdio.h>
main()
{
int a = 10000;
char b='c';
int i,j;

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

13)
#include <stdio.h>
#define PR(a) printf("%d\t",(int) (a));
#define PRINT(a,b,c) PR(a);PR(b);PR(c);
#define MAX(a,b) (a<b?b:a)
main(){
int x = 1,y = 2;
PRINT(MAX(x++,y),x,y);
PRINT(MAX(x++,y),x,y);
}

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

15)
#include <stdio.h>
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 <stdio.h>
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 <stdio.h>
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 <stdio.h>
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 <stdio.h>
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 <stdio.h>
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<m2)
return (m2);
else
return(m1);
}
}

21)
#include <stdio.h>
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 <stdio.h>
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 <stdio.h>
main()
{
char *p = "hello world!";
*(p+0) = 'H';
printf("%s",p);
}

24)
#include <stdio.h>
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 <stdio.h>
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.

24 comments:

  1. 1)

    0
    2
    3
    3
    0
    0
    11
    0
    12

    2)

    rwer 2341

    ghi jkl 123

    3)

    4)

    u, ets and Brickbats,
    Bouquets and Brickbats
    u

    5)

    abcd abcd stuv

    6)

    2 1

    7)
    in:1=>one
    in:2=>two

    8)

    FFFFMAIN

    9)

    pointer differences are -1081544168, 3

    10)
    output same as the input

    11)

    int = 3 int = 1 int = 1

    12)
    c
    10000
    6,2

    13)
    2 2 2 3 4 2

    14)

    : 1
    hello: 0
    hello: 4294967295
    hello: 4294967294
    hello: 4294967293
    hello: 4294967292
    hello: 4294967291
    hello: 4294967290
    hello: 4294967289
    hello: 4294967288
    hello: 4294967287
    hello: 4294967286
    hello: 4294967285
    hello: 4294967284
    hello: 4294967283
    hello: 4294967282
    hello: 4294967281
    hello: 4294967280
    hello: 4294967279
    hello: 4294967278
    hello: 4294967277
    hello: 4294967276
    hello: 4294967275
    .
    .
    .
    .
    .
    .

    15)

    100 10

    16)

     5

    17)

    Bangalore
    Delhi
    Delhi

    18)

    Bangalore Delhi Delhi

    19)

    SWITCH S

    20)

    res 24

    21)

    1

    22)

    !!!!!!!!!!!!!!!!!!!! !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!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !!!

    23)

    Segmentation fault

    24)
    128

    'yes' , 'no' depends on the number it scans

    25)

    a = 3; b = 2

    ReplyDelete
  2. i think that the answers must be given alonside the questions because then it becomes easier to find out your mistake.

    ReplyDelete
  3. how to create the Algorithm

    ReplyDelete
  4. Where can i get answers for this

    ReplyDelete
  5. Give answers by providing a link

    ReplyDelete
  6. Please provide answers alongside questions

    ReplyDelete
  7. hey...
    can u plz write answers along wd the questions or describe sum site for this

    ReplyDelete
  8. answer here only it will be less time consuming

    ReplyDelete
  9. answer shuld be given by the side of question because it's get easier to grasp the concept

    ReplyDelete
  10. good questions.pls tell the answers.

    ReplyDelete
  11. .Just give the answer in link along with each question

    .AFter user decided answer, user click that link, to know answer !

    .TR Vengatesh

    ReplyDelete
  12. thnks !!!!!
    really nice questions answers here only will help us to verify ourselves.

    ReplyDelete
  13. PLEASE SEND ANSWER AFTER EVERY QUESTION

    ReplyDelete
  14. If you give the answers with the puzzle then the eyes will go straight to the answer. So, I think that the answers should be in some other post.

    ReplyDelete
  15. plzzzzzzzzzzzz send answers to verify

    ReplyDelete
  16. Answers please you idiot

    ReplyDelete
  17. I appreciate your efforts and your collection of questions is really good but please provide the answers aswell as these questions are not helpful without them. and if they are already present please highlight the link as i cant find them.

    ReplyDelete
  18. post the ans in other site and link it from here so that we will try 2 solve the puzzles and compare with the solutions

    ReplyDelete
  19. can sum1 please tell the logic for program 22. .

    kritika

    ReplyDelete
  20. answer for 22 is not complete. .

    ReplyDelete
  21. where can i find the solutions

    ReplyDelete