C Rapidfire 3

Look at the code, figure out the output, and then read the explanation to see if you got it right.
Here are 10 additional challenging C code examples that demonstrate tricky aspects of the language:
- Logical vs Bitwise Operators:
int x = 1, y = 0;
if (x & y)
printf("True\n");
else
printf("False\n");
Output: False
Explanation: The bitwise AND (&
) of 1
and 0
is 0
, hence the else branch is executed.
- Array Decay to Pointer:
int arr[] = {10, 20, 30};
int *p = arr;
printf("%d\n", sizeof(arr) / sizeof(arr[0]));
Output: 3
Explanation: sizeof(arr)
gives the total size of the array, divided by the size of one element yields the number of elements.
- Sign Extension with Bit Fields:
struct {
signed int x: 3;
} s;
s.x = 7;
printf("%d\n", s.x);
Output: -1
on many systems Explanation: Assigning 7
(111
in binary) to a 3-bit signed int can result in sign extension, interpreting it as -1
.
- Character Pointer and String Literal:
char *str = "Hello";
printf("%s\n", str);
Output: Hello
Explanation: str
points to a string literal. String literals are stored in read-only memory.
- Integer Overflow:
unsigned int a = 4294967295; // Maximum value for a 32-bit unsigned int
a += 1;
printf("%u\n", a);
Output: 0
Explanation: Adding 1
to the maximum value of an unsigned int causes it to wrap around to 0
.
- Function Pointer Syntax:
void greet() { printf("Hello\n"); }
void (*func_ptr)() = greet;
func_ptr();
Output: Hello
Explanation: func_ptr
is a pointer to the function greet
and is used to call the function.
- Struct Initialization:
struct Point { int x, y; };
struct Point p = {.y = 20, .x = 10};
printf("%d %d\n", p.x, p.y);
Output: 10 20
Explanation: The struct Point
is initialized with named members out of order, but they are correctly assigned.
- Implicit Type Conversion:
short s = 32767;
s += 1;
printf("%d\n", s);
Output: -32768
Explanation: Adding 1
to the maximum value of a short results in overflow, wrapping around to its minimum value.
- Macro Trick:
#define SQUARE(x) x*x
int a = 6 / SQUARE(2);
printf("%d\n", a);
Output: 3
Explanation: The macro expands to 6 / 2*2
, which is evaluated as (6 / 2) * 2
due to operator precedence.
- Enum Constants:
enum { RED, GREEN, BLUE } color;
color = BLUE;
printf("%d\n", color);
Output: 2
Explanation: Enum constants RED
, GREEN
, BLUE
are automatically assigned values 0
, 1
, 2
respectively.
Subscribe to my newsletter
Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Jyotiprakash Mishra
Jyotiprakash Mishra
I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.