Dear visitor,

Most of the programs are meant to be compiled, run and to be explained for their behaviour. The puzzles/questions can be broadly put into the following categories:

General typo errors, which C programmers do often and are very difficult to trace.

Small programs which are extremely hard to understand at the first examination. These questions make a good excercise of reading and understanding effecient code written by others.

I have used Gnu/Linux/gcc for all of them. The order in which the programs appear doesn't have any relation with the level of difficulty. Please feel free to contact me if you need any help in solving the problems. My contact info. is available here And you might be interested in a few references for C programming, which I personally found very interesting.



If you are preparing for campus interviews, you might find the following link interesting:

http://placementsindia.blogspot.com

http://www.interviewmantra.net/category/interview-questions/c





Regards,

Gowri Kumar





C puzzles

#include <stdio.h> #define TOTAL_ELEMENTS ( sizeof ( array ) / sizeof ( array [ 0 ])) int array [] = { 23 , 34 , 12 , 17 , 204 , 99 , 16 };

int main ()

{

int d ;

for ( d =- 1 ; d <= ( TOTAL_ELEMENTS - 2 ); d ++ )

printf ( " %d

" , array [ d + 1 ]);

return 0 ;

}



#include <stdio.h> void OS_Solaris_print () { printf ( "Solaris - Sun Microsystems

" ); } void OS_Windows_print () { printf ( "Windows - Microsoft

" ); } void OS_HP-UX_print () { printf ( "HP-UX - Hewlett Packard

" ); } int main () { int num ; printf ( "Enter the number (1-3):

" ); scanf ( " %d " , & num ); switch ( num ) { case 1 : OS_Solaris_print (); break ; case 2 : OS_Windows_print (); break ; case 3 : OS_HP-UX_print (); break ; default : printf ( "Hmm! only 1-3 :-)

" ); break ; } return 0 ; }

What's the expected output for the following program and why? enum { false , true }; int main () { int i = 1 ; do { printf ( " %d

" , i ); i ++ ; if ( i < 15 ) continue ; } while ( false ); return 0 ; } hint

The following program doesn't "seem" to print "hello-out". (Try executing it) #include <stdio.h> #include <unistd.h> int main ()

{

while ( 1 )

{

fprintf ( stdout , "hello-out" );

fprintf ( stderr , "hello-err" );

sleep ( 1 );

}

return 0 ;

}

What could be the reason?



#include <stdio.h> #define f ( a , b ) a ## b #define g ( a ) # a #define h ( a ) g ( a ) int main ()

{

printf ( " %s

" , h ( f ( 1 , 2 )));

printf ( " %s

" , g ( f ( 1 , 2 )));

return 0 ;

}



#include <stdio.h> int main ()

{

int a = 10 ;

switch ( a )

{

case '1' :

printf ( "ONE

" );

break ;

case '2' :

printf ( "TWO

" );

break ;

defa1ut :

printf ( "NONE

" );

}

return 0 ;

}



int main ()

{

int * p ;

p = ( int * ) malloc ( sizeof ( int ));

* p = 10 ;

return 0 ;

}



Input Output 0 0(0000000) 5 2(0000101) 7 3(0000111)

int CountBits ( unsigned int x )

{

static unsigned int mask [] = { 0x55555555 ,

0x33333333 ,

0x0F0F0F0F ,

0x00FF00FF ,

0x0000FFFF } ;

int i ;

int shift ; /* Number of positions to shift to right*/ for ( i = 0 , shift = 1 ; i < 5 ; i ++ , shift *= 2 )

x = ( x & mask [ i ]) + ( ( x >> shift ) & mask [ i ]);

return x ;

}



#include <stdio.h> int main () { float f = 0.0f ; int i ; for ( i = 0 ; i < 10 ; i ++ ) f = f + 0.1f ; if ( f == 1.0f ) printf ( "f is 1.0

" ); else printf ( "f is NOT 1.0

" ); return 0 ; }

#include <stdio.h> int main () { int a = 1 , 2 ; printf ( "a : %d

" , a ); return 0 ; }

#include <stdio.h> int main () { int i = 43 ; printf ( " %d

" , printf ( " %d " , printf ( " %d " , i ))); return 0 ; }

void duff ( register char * to , register char * from , register int count )

{

register int n = ( count + 7 ) / 8 ;

switch ( count % 8 ){

case 0 : do { * to ++ = * from ++ ;

case 7 : * to ++ = * from ++ ;

case 6 : * to ++ = * from ++ ;

case 5 : * to ++ = * from ++ ;

case 4 : * to ++ = * from ++ ;

case 3 : * to ++ = * from ++ ;

case 2 : * to ++ = * from ++ ;

case 1 : * to ++ = * from ++ ;

} while ( -- n > 0 );

}

}



int CountBits ( unsigned int x )

{

int count = 0 ;

while ( x )

{

count ++ ;

x = x & ( x - 1 );

}

return count ;

}



int foobar ( void );

int foobar ();



#include <stdio.h> void foobar1 ( void )

{

printf ( "In foobar1

" );

}

void foobar2 ()

{

printf ( "In foobar2

" );

}

int main ()

{

char ch = 'a' ;

foobar1 ();

foobar2 ( 33 , ch );

return 0 ;

}



#include <stdio.h> void foobar1 ( void )

{

printf ( "In foobar1

" );

}

void foobar2 ()

{

printf ( "In foobar2

" );

}

int main ()

{

char ch = 'a' ;

foobar1 ( 33 , ch );

foobar2 ();

return 0 ;

}



#include <stdio.h> int main ()

{

float a = 12.5 ;

printf ( " %d

" , a );

printf ( " %d

" , * ( int * ) & a );

return 0 ;

}



int arr [ 80 ];



extern int * arr ;

int main ()

{

arr [ 1 ] = 100 ;

return 0 ;

}



#include <stdio.h> int main ()

{

int a = 1 ;

switch ( a )

{ int b = 20 ;

case 1 : printf ( "b is %d

" , b );

break ;

default : printf ( "b is %d

" , b );

break ;

}

return 0 ;

}



#define SIZE 10 void size ( int arr [ SIZE ])

{

printf ( "size of array is: %d

" , sizeof ( arr ));

}

int main ()

{

int arr [ SIZE ];

size ( arr );

return 0 ;

}



#include <stdlib.h> #include <stdio.h> void Error ( char * s )

{

printf ( s );

return ;

}

int main ()

{

int * p ;

p = malloc ( sizeof ( int ));

if ( p == NULL )

{

Error ( "Could not allocate the memory

" );

Error ( "Quitting....

" );

exit ( 1 );

}

else {

/*some stuff to use p*/ }

return 0 ;

}



#include <stdio.h> int main ()

{

char c ;

scanf ( " %c " , & c );

printf ( " %c

" , c );

scanf ( " %c " , & c );

printf ( " %c

" , c );

return 0 ;

}



#include <stdio.h> int main ()

{

char str [ 80 ];

printf ( "Enter the string:" );

scanf ( " %s " , str );

printf ( "You entered: %s

" , str );

return 0 ;

}



#include <stdio.h> int main ()

{

int i ;

i = 10 ;

printf ( "i : %d

" , i );

printf ( "sizeof(i++) is: %d

" , sizeof ( i ++ ));

printf ( "i : %d

" , i );

return 0 ;

}



#include <stdio.h> void foo ( const char ** p ) { }

int main ( int argc , char ** argv )

{

foo ( argv );

return 0 ;

}



#include <stdio.h> int main ()

{

int i ;

i = 1 , 2 , 3 ;

printf ( "i: %d

" , i );

return 0 ;

}



#include <stdio.h> #include <stdlib.h> #define MAX 80 #define NUMBER '0' int getop ( char []);

void push ( double );

double pop ( void );

int main ()

{

int type ;

char s [ MAX ];

while (( type = getop ( s )) != EOF )

{

switch ( type )

{

case NUMBER :

push ( atof ( s ));

break ;

case '+' :

push ( pop () + pop ());

break ;

case '*' :

push ( pop () * pop ());

break ;

case '-' :

push ( pop () - pop ());

break ;

case '/' :

push ( pop () / pop ());

break ;

/* ... * ... * ... */ }

}

}



#include <stdio.h> #include <ctype.h> char t [] = {

0 , 0 , 0 , 0 , 0 , 0 , 12 , 18 , 33 , 63 ,

33 , 33 , 62 , 32 , 62 , 33 , 33 , 62 , 30 , 33 ,

32 , 32 , 33 , 30 , 62 , 33 , 33 , 33 , 33 , 62 ,

63 , 32 , 62 , 32 , 32 , 63 , 63 , 32 , 62 , 32 ,

32 , 32 , 30 , 33 , 32 , 39 , 33 , 30 , 33 , 33 ,

63 , 33 , 33 , 33 , 4 , 4 , 4 , 4 , 4 , 4 ,

1 , 1 , 1 , 1 , 33 , 30 , 33 , 34 , 60 , 36 ,

34 , 33 , 32 , 32 , 32 , 32 , 32 , 63 , 33 , 51 ,

45 , 33 , 33 , 33 , 33 , 49 , 41 , 37 , 35 , 33 ,

30 , 33 , 33 , 33 , 33 , 30 , 62 , 33 , 33 , 62 ,

32 , 32 , 30 , 33 , 33 , 37 , 34 , 29 , 62 , 33 ,

33 , 62 , 34 , 33 , 30 , 32 , 30 , 1 , 33 , 30 ,

31 , 4 , 4 , 4 , 4 , 4 , 33 , 33 , 33 , 33 ,

33 , 30 , 33 , 33 , 33 , 33 , 18 , 12 , 33 , 33 ,

33 , 45 , 51 , 33 , 33 , 18 , 12 , 12 , 18 , 33 ,

17 , 10 , 4 , 4 , 4 , 4 , 63 , 2 , 4 , 8 ,

16 , 63 };

int main ( int argc , char ** argv )

{

int r , pr ;

for ( r = 0 ; r < 6 ; ++ r )

{

char * p = argv [ 1 ];

while ( pr && * p )

{

int o = ( toupper ( * p ++ ) - 'A' ) * 6 + 6 + r ;

o = ( o < 0 || o >= sizeof ( t )) ? 0 : o ;

for ( pr = 5 ; pr >= - 1 ; -- pr )

{

printf ( " %c " ,( ( ( pr >= 0 ) && ( t [ o ] & ( 1 << pr ))) ? '#' : ' ' ));

}

}

printf ( "

" );

}

return 0 ;

}



#include <stdio.h> #include <stdlib.h> #define SIZEOF ( arr ) ( sizeof ( arr ) / sizeof ( arr [ 0 ])) #define PrintInt ( expr ) printf ( " %s : %d

" ,# expr ,( expr )) int main ()

{

/* The powers of 10 */ int pot [] = {

000 1 ,

00 1 0 ,

0 1 00 ,

1000 };

int i ;

for ( i = 0 ; i < SIZEOF ( pot ); i ++ )

PrintInt ( pot [ i ]);

return 0 ;

}



#include <stdio.h> int gcd ( int u , int v )

{

int t ;

while ( v > 0 )

{

if ( u > v )

{

t = u ;

u = v ;

v = t ;

}

v = v - u ;

}

return u ;

}

int main ()

{

int x , y ;

printf ( "Enter x y to find their gcd:" );

while ( scanf ( " %d%d " , & x , & y ) != EOF )

{

if ( x > 0 && y > 0 )

printf ( " %d %d %d

" , x , y , gcd ( x , y ));

printf ( "Enter x y to find their gcd:" );

}

printf ( "

" );

return 0 ;

}



#include <stdio.h> #define PrintInt ( expr ) printf ( " %s : %d

" ,# expr ,( expr )) int main ()

{

int y = 100 ;

int * p ;

p = malloc ( sizeof ( int ));

* p = 10 ;

y = y/*p; /*dividing y by *p */ ;

PrintInt ( y );

return 0 ;

}



#include <stdio.h> int main ()

{

int day , month , year ;

printf ( "Enter the date (dd-mm-yyyy) format including -'s:" );

scanf ( " %d - %d - %d " , & day , & month , & year );

printf ( "The date you have entered is %d - %d - %d

" , day , month , year );

return 0 ;

}



#include <stdio.h> int main ()

{

int n ;

printf ( "Enter a number:

" );

scanf ( " %d

" , n );

printf ( "You entered %d

" , n );

return 0 ;

}



#include <stdio.h> #define PrintInt ( expr ) printf ( " %s : %d

" ,# expr ,( expr )) int FiveTimes ( int a )

{

int t ;

t = a << 2 + a ;

return t ;

}

int main ()

{

int a = 1 , b = 2 , c = 3 ;

PrintInt ( FiveTimes ( a ));

PrintInt ( FiveTimes ( b ));

PrintInt ( FiveTimes ( c ));

return 0 ;

}



#include <stdio.h> #define PrintInt ( expr ) printf ( " %s : %d

" ,# expr ,( expr )) int max ( int x , int y )

{

( x > y ) ? return x : return y ;

}

int main ()

{

int a = 10 , b = 20 ;

PrintInt ( a );

PrintInt ( b );

PrintInt ( max ( a , b ));

}



#include <stdio.h> int main ()

{

int i ;

int n = 20 ;

for ( i = 0 ; i < n ; i -- )

printf ( "-" );

return 0 ;

}



#include <stdio.h> int main ()

{

int * ptr1 , ptr2 ;

ptr1 = malloc ( sizeof ( int ));

ptr2 = ptr1 ;

* ptr2 = 10 ;

return 0 ;

}



#include <stdio.h> int main ()

{

int cnt = 5 , a ;

do {

a /= cnt ;

} while ( cnt -- );

printf ( " %d

" , a );

return 0 ;

}



#include <stdio.h> int main ()

{

int i = 6 ;

if ( (( ++ i < 7 ) && ( i ++/ 6 )) || ( ++ i <= 9 ))

;

printf ( " %d

" , i );

return 0 ;

}



#include <stdlib.h> #include <stdio.h> #define SIZE 15 int main ()

{

int * a , i ;

a = malloc ( SIZE * sizeof ( int ));

for ( i = 0 ; i < SIZE ; i ++ )

* ( a + i ) = i * i ;

for ( i = 0 ; i < SIZE ; i ++ )

printf ( " %d

" , * a ++ );

free ( a );

return 0 ;

}



#include <stdio.h> int main ()

{

int a = 3 , b = 5 ;

printf ( & a [ "Ya!Hello! how is this? %s

" ], & b [ "junk/super" ]);

printf ( & a [ "WHAT %c%c%c %c%c %c !

" ], 1 [ "this" ],

2 [ "beauty" ], 0 [ "tool" ], 0 [ "is" ], 3 [ "sensitive" ], 4 [ "CCCCCC" ]);

return 0 ;

}



#include <stdio.h> int main ()

{

char dummy [ 80 ];

printf ( "Enter a string:

" );

scanf ( " %[^a] " , dummy );

printf ( " %s

" , dummy );

return 0 ;

}



int a ;

int a = 10 ;

extern int a ; int main () { printf ( "a = %d

" , a ); return 0 ; }

bash$ gcc a.c b.c main.c bash$ ./a.out a = 10

#define offsetof ( a , b ) (( int )( & ((( a * )( 0 )) -> b )))

#define SWAP ( a , b ) (( a ) ^= ( b ) ^= ( a ) ^= ( b ))

#define DPRINTF ( x ) printf ( " %s : %d

" ,# x , x )

int IAddOverFlow ( int * result , int a , int b )

{

/* ... */ }



#define ROUNDUP ( x , n ) (( x + n - 1 ) & ( ~ ( n - 1 )))

#define isupper ( c ) ((( c ) >= 'A' ) && (( c ) <= 'Z' ))

char c ;

/* ... */ if ( isupper ( c ++ ))

{

/* ... */ }



int VarArguments (...)

{

/*....*/ return 0 ;

}



const char * p ;

char * const p ;



This work by Gowri Kumar Chandramouli is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.