Professional Documents
Culture Documents
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
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.
Show answer
6/20/13
C Programming Puzzlers
x n
x n
Show answer
Show answer
stevenkobes.com/ctest.html
3/10
6/20/13
C Programming Puzzlers
Show answer
Show answer
6/20/13
C Programming Puzzlers
Show answer
6/20/13
C Programming Puzzlers
( d )
f 1 ,f 2 , and f 3
Show answer
Show answer
6/20/13
C Programming Puzzlers
Show answer
0120
7/10
6/20/13
C Programming Puzzlers
( b ) ( c ) ( d )
Show answer
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
Show answer
stevenkobes.com/ctest.html
8/10
6/20/13
C Programming Puzzlers
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
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 ; }
Show answer