C Rapidfire 2

Look at the code, figure out the output, and then read the explanation to see if you got it right.
- Floating Point Precision:
double a = 0.7;
if (a < 0.7)
printf("Less than 0.7\n");
else
printf("Not less than 0.7\n");
Output: Less than 0.7
Explanation: 0.7
is treated as a double in the variable a
but as a float in the comparison, leading to a precision mismatch.
- Shift Operator Behavior:
int x = 1;
printf("%d\n", x << 32);
Output: Depends on the system (could be 1
or 0
) Explanation: Shifting by the bit width of the type or more yields undefined behavior in C.
- Character Arrays and Null Terminator:
char str[6] = "hello";
printf("%s\n", str);
Output: hello
Explanation: The string "hello"
fits exactly into str
, including the null terminator, which is implicitly added.
- Integer Promotion in Variadic Functions:
printf("%lu\n", sizeof('A'));
Output: 4
or 8
(depending on the system) Explanation: Character constants are promoted to int
in C, and sizeof
returns the size of int
.
- Volatile Keyword:
volatile int x = 10;
x = 20;
x = 30;
printf("%d\n", x);
Output: 30
Explanation: The volatile
keyword prevents the compiler from optimizing out multiple assignments to x
, ensuring the last value is printed.
- Array Initialization:
int arr[5] = {1, 2, 3};
printf("%d %d\n", arr[3], arr[4]);
Output: 0 0
Explanation: Uninitialized elements in an array initializer list are automatically set to zero.
- Pointer and Array Relationship:
int arr[] = {10, 20, 30, 40, 50};
int *p = arr;
printf("%d\n", p[3]);
Output: 40
Explanation: p
points to the array arr
, so p[3]
is equivalent to arr[3]
.
- Static Variable in Function:
void func() {
static int count = 0;
count++;
printf("%d ", count);
}
func();
func();
Output: 1 2
Explanation: The static variable count
retains its value between function calls.
- Struct and Union Difference:
union {
int a;
char b;
} u;
u.a = 1;
printf("%d\n", u.b);
Output: 1
or 0
(depends on system endianness) Explanation: In a union, all members share the same memory. The output depends on how integers are stored in memory (endianness).
- Pre-increment and Post-increment:
int i = 1;
printf("%d %d\n", i, i++);
Output: 2 1
Explanation: The value of i
is incremented before it's passed to printf
, but the original value (1
) is used in the expression due to post-increment.
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.