C Programming Puzzlers

These questions originally appeared as an article on programmersheaven.com, written by Ashok K. Pathak, a researcher at Bharat Electronics Limited (CRL), Ghaziabad. They are reproduced here with minor modifications.

The questions test advanced knowledge of the C language, including some rarely-used features. Effective C programming requires a strong understanding of concepts like undefined behavior, recursion, and pointer arithmetic, but the deliberately convoluted examples on this page are not representative of real-world code, and certainly won't win any prizes for clarity and maintainability.

Performance on these questions is not a good indicator of broader competence in software development. As such, they are unlikely to be useful in an interview setting.

Jump to question: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

#include <stdio.h> #include <setjmp.h> static jmp_buf buf; int main ( void ) { volatile int b = 3; if ( setjmp ( buf ) != 0 ) { printf ( "%d

" , b ) ; exit ( 0 ) ; } b = 5; longjmp ( buf, 1 ) ; }

What is the output of this program?

(a) 3

(b) 5

(c) 0

(d) none of the above

#include <stdio.h> int main ( void ) { struct node { int a; int b; int c; } ; struct node s = { 3, 5, 6 } ; struct node *pt = &s; printf ( "%d

" , * ( int* ) pt ) ; return 0; }

What is the output of this program?

(a) 3

(b) 5

(c) 6

(d) 7

int foo ( int x, int n ) { int val = 1; if ( n > 0 ) { if ( n % 2 == 1 ) val *= x; val *= foo ( x * x, n / 2 ) ; } return val; }

What function of x and n is computed by foo ?

(a) xn

(b) x × n

(c) nx

(d) none of the above

#include <stdio.h> int main ( void ) { int a [ 5 ] = { 1, 2, 3, 4, 5 } ; int *ptr = ( int * )( &a + 1 ) ; printf ( "%d %d

" , * ( a + 1 ) , * ( ptr - 1 )) ; return 0; }

What is the output of this program?

(a) 2 2

(b) 2 1

(c) 2 5

(d) none of the above

#include <stdio.h> void foo ( int [][ 3 ]) ; int main ( void ) { int a [ 3 ][ 3 ] = { { 1, 2, 3 } , { 4, 5, 6 } , { 7, 8, 9 } } ; foo ( a ) ; printf ( "%d

" , a [ 2 ][ 1 ]) ; return 0; } void foo ( int b [][ 3 ]) { ++b; b [ 1 ][ 1 ] = 9; }

What is the output of this program?

(a) 8

(b) 9

(c) 7

(d) none of the above

#include <stdio.h> int main ( void ) { int a, b, c, d; a = 3; b = 5; c = a, b; d = ( a, b ) ; printf ( "c=%d " , c ) ; printf ( "d=%d

" , d ) ; return 0; }

What is the output of this program?

(a) c=3 d=3

(b) c=5 d=3

(c) c=3 d=5

(d) c=5 d=5

#include <stdio.h> int main ( void ) { int a [][ 3 ] = { 1, 2, 3, 4, 5, 6 } ; int ( *ptr )[ 3 ] = a; printf ( "%d %d " , ( *ptr )[ 1 ] , ( *ptr )[ 2 ]) ; ++ptr; printf ( "%d %d

" , ( *ptr )[ 1 ] , ( *ptr )[ 2 ]) ; return 0; }

What is the output of this program?

(a) 2 3 5 6

(b) 2 3 4 5

(c) 4 5 0 0

(d) none of the above

#include <stdlib.h> int *f1 ( void ) { int x = 10; return &x; } int *f2 ( void ) { int *ptr; *ptr = 10; return ptr; } int *f3 ( void ) { int *ptr; ptr = malloc ( sizeof *ptr ) ; return ptr; }

Which of these functions uses pointers incorrectly?

(a) f3 only

(b) f1 and f3

(c) f1 and f2

(d) f1 , f2 , and f3

#include <stdio.h> int main ( void ) { int i = 3; int j; j = sizeof ( ++i + ++i ) ; printf ( "i=%d j=%d

" , i, j ) ; return 0; }

What is the output of this program on an implementation where int occupies 2 bytes?

(a) i=4 j=2

(b) i=3 j=2

(c) i=5 j=2

(d) the behavior is undefined

#include <stdio.h> void f1 ( int *, int ) ; void f2 ( int *, int ) ; void ( *p [ 2 ])( int *, int ) ; int main ( void ) { int a = 3; int b = 5; p [ 0 ] = f1; p [ 1 ] = f2; p [ 0 ]( &a, b ) ; printf ( "%d %d " , a, b ) ; p [ 1 ]( &a, b ) ; printf ( "%d %d

" , a, b ) ; return 0; } void f1 ( int *p, int q ) { int tmp = *p; *p = q; q = tmp; } void f2 ( int *p, int q ) { int tmp = *p; *p = q; q = tmp; }

What is the output of this program?

(a) 5 5 5 5

(b) 3 5 3 5

(c) 5 3 3 5

(d) none of the above

#include <stdio.h> void e ( int ) ; int main ( void ) { int a = 3; e ( a ) ; putchar ( '

' ) ; return 0; } void e ( int n ) { if ( n > 0 ) { e ( --n ) ; printf ( "%d " , n ) ; e ( --n ) ; } }

What is the output of this program?

(a) 0 1 2 0

(b) 0 1 2 1

(c) 1 2 0 1

(d) 0 2 1 1

typedef int ( *test )( float *, float * ) ; test tmp;

What is the type of tmp ?

(a) function taking two pointer-to- float arguments and returning pointer to int

(b) pointer to int

(c) pointer to function taking two pointer-to- float arguments and returning int

(d) none of the above

#include <stdio.h> int main ( void ) { char p; char buf [ 10 ] = { 1, 2, 3, 4, 5, 6, 9, 8 } ; p = ( buf + 1 )[ 5 ] ; printf ( "%d

" , p ) ; return 0; }

What is the output of this program?

(a) 5

(b) 6

(c) 9

(d) none of the above

#include <stdio.h> void f ( char ** ) ; int main ( void ) { char *argv [] = { "ab" , "cd" , "ef" , "gh" , "ij" , "kl" } ; f ( argv ) ; return 0; } void f ( char **p ) { char *t; t = ( p += sizeof ( int ))[ -1 ] ; printf ( "%s

" , t ) ; }

What is the output of this program on an implementation where int and all pointer types occupy 2 bytes?

(a) ab

(b) cd

(c) ef

(d) gh

#include <stdarg.h> #include <stdio.h> int ripple ( int n, ... ) { int i, j, k; va_list p; k = 0; j = 1; va_start ( p, n ) ; for ( ; j < n; ++j ) { i = va_arg ( p, int ) ; for ( ; i; i &= i - 1 ) ++k; } va_end ( p ) ; return k; } int main ( void ) { printf ( "%d

" , ripple ( 3, 5, 7 )) ; return 0; }

What is the output of this program?

(a) 7

(b) 6

(c) 5

(d) 3

#include <stdio.h> int counter ( int i ) { static int count = 0; count = count + i; return count; } int main ( void ) { int i, j; for ( i = 0; i <= 5; i++ ) j = counter ( i ) ; printf ( "%d

" , j ) ; return 0; }

What is the output of this program?