You are on page 1of 10

6/20/13

C Programming Puzzlers

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 realworld 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. Steve Kobes, 8/25/04 (amended 6/19/11) Jump to question: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

1. Consider the following program:


# i n c l u d e< s t d i o . h > # i n c l u d e< s e t j m p . h > s t a t i cj m p _ b u fb u f ; i n tm a i n ( v o i d ) { v o l a t i l ei n tb=3 ; i f( s e t j m p ( b u f )! =0 ) { p r i n t f ( " % d \ n " ,b ) ; e x i t ( 0 ) ; } b=5 ; l o n g j m p ( b u f ,1 ) ; }

What is the output of this program?


( a ) ( b ) ( c ) ( d ) 3 5 0

none of the above

stevenkobes.com/ctest.html

1/10

6/20/13

C Programming Puzzlers

Hide answer Answer: (b) The s e t j m pfunction stores context information for a non-local goto, and returns 0. The l o n g j m pfunction transfers control to the s e t j m pcall that initialized b u f , and execution continues from this point as if s e t j m phad returned 1. Note: a non-volatile automatic variable that has been modified after s e t j m p becomes indeterminate after l o n g j m p . Without the v o l a t i l equalifier, this programs behavior would be undefined. This rule permits better optimization of code.

2. Consider the following program:


# i n c l u d e< s t d i o . h > i n tm a i n ( v o i d ) { s t r u c tn o d e { i n ta ; i n tb ; i n tc ; } ; s t r u c tn o d es={3 ,5 ,6} ; s t r u c tn o d e* p t=& s ; p r i n t f ( " % d \ n " ,* ( i n t * ) p t ) ; r e t u r n0 ; }

What is the output of this program?


( a ) ( b ) ( c ) ( d ) 3 5 6 7

Show answer

3. Consider the following code segment:


i n tf o o ( i n tx ,i n tn ) { i n tv a l=1 ; i f( n>0 ) {
stevenkobes.com/ctest.html 2/10

6/20/13

C Programming Puzzlers

i f( n%2= =1 ) v a l* =x ; v a l* =f o o ( x*x ,n/2 ) ; } r e t u r nv a l ; }

What function of xand nis computed by f o o ?


( a ) ( b ) ( c ) ( d )
n x

x n
x n

none of the above

Show answer

4. Consider the following program:


# i n c l u d e< s t d i o . h > i n tm a i n ( v o i d ) { i n ta [ 5 ]={1 ,2 ,3 ,4 ,5} ; i n t* p t r=( i n t * ) ( & a+1 ) ; p r i n t f ( " % d% d \ n " ,* ( a+1 ) ,* ( p t r-1 ) ) ; r e t u r n0 ; }

What is the output of this program?


( a ) ( b ) ( c ) ( d ) 22 21 25

none of the above

Show answer

5. Consider the following program:


# i n c l u d e< s t d i o . h > v o i df o o ( i n t [ ] [ 3 ] ) ; i n tm a i n ( v o i d ) { i n ta [ 3 ] [ 3 ]={{ 1 ,2 ,3 } ,{ 4 ,5 ,6 } ,{ 7 ,8 ,9 }} ;

stevenkobes.com/ctest.html

3/10

6/20/13

C Programming Puzzlers

f o o ( a ) ; p r i n t f ( " % d \ n " ,a [ 2 ] [ 1 ] ) ; r e t u r n0 ; } v o i df o o ( i n tb [ ] [ 3 ] ) { + + b ; b [ 1 ] [ 1 ]=9 ; }

What is the output of this program?


( a ) ( b ) ( c ) ( d ) 8 9 7

none of the above

Show answer

6. Consider the following program:


# i n c l u d e< s t d i o . h > i n tm a i n ( v o i d ) { i n ta ,b ,c ,d ; a=3 ; b=5 ; c=a ,b ; d=( a ,b ) ; p r i n t f ( " c = % d " ,c ) ; p r i n t f ( " d = % d \ n " ,d ) ; r e t u r n0 ; }

What is the output of this program?


( a ) ( b ) ( c ) ( d ) c = 3d = 3 c = 5d = 3 c = 3d = 5 c = 5d = 5

Show answer

7. Consider the following program:


stevenkobes.com/ctest.html 4/10

6/20/13

C Programming Puzzlers

# i n c l u d e< s t d i o . h > i n tm a i n ( v o i d ) { i n ta [ ] [ 3 ]={ 1 ,2 ,3 ,4 ,5 ,6 } ; i n t( * p t r ) [ 3 ]=a ; p r i n t f ( " % d% d" ,( * p t r ) [ 1 ] ,( * p t r ) [ 2 ] ) ; + + p t r ; p r i n t f ( " % d% d \ n " ,( * p t r ) [ 1 ] ,( * p t r ) [ 2 ] ) ; r e t u r n0 ; }

What is the output of this program?


( a ) ( b ) ( c ) ( d ) 2356 2345 4500

none of the above

Show answer

8. Consider the following code segment:


# i n c l u d e< s t d l i b . h > i n t* f 1 ( v o i d ) { i n tx=1 0 ; r e t u r n& x ; } i n t* f 2 ( v o i d ) { i n t* p t r ; * p t r=1 0 ; r e t u r np t r ; } i n t* f 3 ( v o i d ) { i n t* p t r ; p t r=m a l l o c ( s i z e o f* p t r ) ; r e t u r np t r ; }

Which of these functions uses pointers incorrectly?


( a ) ( b ) ( c )
stevenkobes.com/ctest.html

f 3only f 1and f 3 f 1and f 2


5/10

6/20/13

C Programming Puzzlers

( d )

f 1 ,f 2 , and f 3

Show answer

9. Consider the following program:


# i n c l u d e< s t d i o . h > i n tm a i n ( v o i d ) { i n ti=3 ; i n tj ; j=s i z e o f ( + + i++ + i ) ; p r i n t f ( " i = % dj = % d \ n " ,i ,j ) ; r e t u r n0 ; }

What is the output of this program on an implementation where i n toccupies 2 bytes?


( a ) ( b ) ( c ) ( d ) i = 4j = 2 i = 3j = 2 i = 5j = 2

the behavior is undefined

Show answer

10. Consider the following program:


# i n c l u d e< s t d i o . h > v o i df 1 ( i n t * ,i n t ) ; v o i df 2 ( i n t * ,i n t ) ; v o i d( * p [ 2 ] ) ( i n t * ,i n t ) ; i n tm a i n ( v o i d ) { i n ta=3 ; i n tb=5 ; p [ 0 ]=f 1 ; p [ 1 ]=f 2 ; p [ 0 ] ( & a ,b ) ; p r i n t f ( " % d% d" ,a ,b ) ; p [ 1 ] ( & a ,b ) ; p r i n t f ( " % d% d \ n " ,a ,b ) ;
stevenkobes.com/ctest.html 6/10

6/20/13

C Programming Puzzlers

r e t u r n0 ; } v o i df 1 ( i n t* p ,i n tq ) { i n tt m p=* p ; * p=q ; q=t m p ; } v o i df 2 ( i n t* p ,i n tq ) { i n tt m p=* p ; * p=q ; q=t m p ; }

What is the output of this program?


( a ) ( b ) ( c ) ( d ) 5555 3535 5335

none of the above

Show answer

11. Consider the following program:


# i n c l u d e< s t d i o . h > v o i de ( i n t ) ; i n tm a i n ( v o i d ) { i n ta=3 ; e ( a ) ; p u t c h a r ( ' \ n ' ) ; r e t u r n0 ; } v o i de ( i n tn ) { i f( n>0 ) { e ( n ) ; p r i n t f ( " % d" ,n ) ; e ( n ) ; } }

What is the output of this program?


( a )
stevenkobes.com/ctest.html

0120
7/10

6/20/13

C Programming Puzzlers

( b ) ( c ) ( d )

0121 1201 0211

Show answer

12. Consider the following code segment:


t y p e d e fi n t( * t e s t ) ( f l o a t * ,f l o a t * ) ; t e s tt m p ;

What is the type of t m p ?


( a )

function taking two pointer-to- f l o a targuments and returning pointer

to i n t pointer to i n t ( c ) pointer to function taking two pointer-to- f l o a targuments and returning i n t ( d ) none of the above
( b )

Show answer

13. Consider the following program:


# i n c l u d e< s t d i o . h > i n tm a i n ( v o i d ) { c h a rp ; c h a rb u f [ 1 0 ]={ 1 ,2 ,3 ,4 ,5 ,6 ,9 ,8 } ; p=( b u f+1 ) [ 5 ] ; p r i n t f ( " % d \ n " ,p ) ; r e t u r n0 ; }

What is the output of this program?


( a ) ( b ) ( c ) ( d ) 5 6 9

none of the above

Show answer

stevenkobes.com/ctest.html

8/10

6/20/13

C Programming Puzzlers

14. Consider the following program:


# i n c l u d e< s t d i o . h > v o i df ( c h a r * * ) ; i n tm a i n ( v o i d ) { c h a r* a r g v [ ]={" a b " ," c d " ," e f " ," g h " ," i j " ," k l "} ; f ( a r g v ) ; r e t u r n0 ; } v o i df ( c h a r* * p ) { c h a r* t ; t=( p+ =s i z e o f ( i n t ) ) [ 1 ] ; p r i n t f ( " % s \ n " ,t ) ; }

What is the output of this program on an implementation where i n tand all pointer types occupy 2 bytes?
( a ) ( b ) ( c ) ( d ) a b c d e f g h

Show answer

15. Consider the following program:


# i n c l u d e< s t d a r g . h > # i n c l u d e< s t d i o . h > i n tr i p p l e ( i n tn ,. . . ) { i n ti ,j ,k ; v a _ l i s tp ; k=0 ; j=1 ; v a _ s t a r t ( p ,n ) ; f o r( ;j<n ;+ + j ) { i=v a _ a r g ( p ,i n t ) ; f o r( ;i ;i& =i-1 ) + + k ; } v a _ e n d ( p ) ;
stevenkobes.com/ctest.html 9/10

6/20/13

C Programming Puzzlers

r e t u r nk ; } i n tm a i n ( v o i d ) { p r i n t f ( " % d \ n " ,r i p p l e ( 3 ,5 ,7 ) ) ; r e t u r n0 ; }

What is the output of this program?


( a ) ( b ) ( c ) ( d ) 7 6 5 3

Show answer

16. Consider the following program:


# i n c l u d e< s t d i o . h > i n tc o u n t e r ( i n ti ) { s t a t i ci n tc o u n t=0 ; c o u n t=c o u n t+i ; r e t u r nc o u n t ; } i n tm a i n ( v o i d ) { i n ti ,j ; f o r( i=0 ;i< =5 ;i + + ) j=c o u n t e r ( i ) ; p r i n t f ( " % d \ n " ,j ) ; r e t u r n0 ; }

What is the output of this program?


( a ) ( b ) ( c ) ( d ) 1 0 1 5 6 7

Show answer Compute your score


stevenkobes.com/ctest.html 10/10

You might also like