0% found this document useful (0 votes)
28 views5 pages

C Programming Puzzles and Riddles

The document presents a series of C programming puzzles and riddles designed to challenge understanding of C syntax and logic. Each puzzle includes a code snippet or question that tests knowledge on topics such as variable naming, loop behavior, and error identification. The riddles further explore common programming pitfalls and concepts, providing answers to enhance learning.

Uploaded by

Nelvit FernNdes
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views5 pages

C Programming Puzzles and Riddles

The document presents a series of C programming puzzles and riddles designed to challenge understanding of C syntax and logic. Each puzzle includes a code snippet or question that tests knowledge on topics such as variable naming, loop behavior, and error identification. The riddles further explore common programming pitfalls and concepts, providing answers to enhance learning.

Uploaded by

Nelvit FernNdes
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

C Programming Puzzles and Riddles

🔍 C Programming Puzzles

Puzzle 1: What’s the Output?

#include <stdio.h>
int main() {
int a = 5, b = 2;
printf("%d", a++ + ++b);
return 0;
}

🔸 Question: What will be the output of this code and why?

Puzzle 2: Odd One Out

Choose the invalid variable name:


a) _value
b) int
c) number1
d) sum_total

🔸 Question: Which one is invalid and why?

Puzzle 3: Missing Semicolon

#include <stdio.h>
int main() {
int x = 10
printf("%d", x);
return 0;
}

🔸 Question: Find and fix the error.

Puzzle 4: Loop Logic

#include <stdio.h>
int main() {
int i;
for (i = 0; i < 5; i++);
printf("i = %d\n", i);
return 0;
}

🔸 Question: What is the output and why?

Puzzle 5: Array Mystery

#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
printf("%d", *(arr + 3));
return 0;
}

🔸 Question: What will be printed and why?

Puzzle 6: Conditional Confusion

#include <stdio.h>
int main() {
int a = 10;
if (a = 5)
printf("True\n");
else
printf("False\n");
return 0;
}

🔸 Question: What will this print and why?

🧩 C Programming Riddles

1. Riddle of the Equal Signs


I look like I check,
but instead, I assign.
Use me in a condition,
and I might cross the line.
❓ Who am I?

🧠 Answer: = (assignment operator, not comparison)


2. The Disappearing Print
I’m inside a loop,
but never run.
A tiny semicolon,
spoils the fun.
❓ What happened?

🧠 Answer: A semicolon after for() makes it an empty loop.

3. Infinity Puzzle
I go on forever,
no break or end.
No condition to stop me,
only ctrl+C is your friend.
❓ What kind of loop am I?

🧠 Answer: while(1) or for(;;)

4. The Shadowed Soul


I'm declared twice,
one inside, one out.
When I print,
the outer one shouts.
❓ What happened to the inner one?

🧠 Answer: Inner variable shadows the outer, but it goes out of scope.

5. The Missing main()


I compile just fine,
no error, no whine.
But run me once,
I’m empty – not divine.
❓ What did the coder forget?

🧠 Answer: Forgot the main() function.

6. The Divide Disaster


I divide two numbers,
seems simple and neat.
But try it with zero,
and boom – no repeat.
❓ What error is this?

🧠 Answer: Division by zero.


7. Hidden in the Heap
I’m dynamic and free,
but forget me, and you’ll see,
your memory will leak,
like a sinking sea.
❓ What did the coder forget?

🧠 Answer: Forgot to free() dynamically allocated memory.

8. Return of the Void


I do some work,
but give nothing back.
You try to catch me,
and your program might crack.
❓ Who am I?

🧠 Answer: A function returning void being assigned to a variable.

9. The Slippery Snake


You think I’m adding,
but I do something slick.
I increase and then add,
a classic trick.
❓ What does a+++b really mean?

🧠 Answer: It’s parsed as (a++) + b

10. The Forgotten Braces


I have if and else,
but no curly race.
Add a new line,
and you’re in the wrong place.
❓ What bug creeps in?

🧠 Answer: Only one statement is part of the if without {}

11. The Painted Zero


I look like the letter,
but I’m really a digit.
Use me in a loop,
and you might fidget.
❓ What am I?

🧠 Answer: 0 (zero) mistaken for O


12. The Infinite Clone
I call myself,
again and again.
With no base case,
it’s a crash in vain.
❓ What programming mistake is this?

🧠 Answer: Infinite recursion with no stopping condition.

13. The Case that Broke


I’m part of a switch,
but fall like a stream.
Forget a keyword,
and I break the dream.
❓ What keyword am I missing?

🧠 Answer: break

14. The Mysterious %d


I print with a code,
but the types don’t agree.
An int for a float?
You’ll get junk from me.
❓ What’s the mistake?

🧠 Answer: Mismatch of format specifier and data type.

15. Phantom \0
I live at the end,
but you don’t see me.
Without me in strings,
chaos there will be.
❓ Who am I?

🧠 Answer: Null terminator \0

Common questions

Powered by AI

A variable is shadowed in C when a local variable within a block has the same name as a variable outside that block. The local variable's scope takes precedence within the block, effectively hiding or "shadowing" the outer variable. This can lead to confusion since modifications to the variable within the block affect only the local instance and not the outer one, potentially leading to logic errors if the programmer is not aware of the shadowing .

Using a mismatched format specifier in printf can result in undefined behavior, including erroneous output such as printing incorrect values or non-readable characters. This occurs because printf expects data in a specific format and interprets the provided data incorrectly if mismatched, which can lead to difficult-to-trace bugs in the program .

The expression 'a+++b' does not result in a syntax error due to operator precedence rules in C. It is parsed as '(a++) + b' where 'a++' increments 'a' but uses its value prior to increment for the addition. This is a result of the post-increment operator having higher precedence than the '+' operator, so 'a' is incremented after using its value in the addition with 'b' .

Failing to free dynamically allocated memory in a C program can lead to memory leaks, whereby allocated memory resources are not returned to the system after use. Over time, as the program consumes more memory without releasing it, this can lead to reduced performance and eventual system memory exhaustion, necessitating program termination or system restart .

Omitting a 'break' statement in a switch case can lead to fall-through, where execution continues to the next case regardless of its condition. This can unintentionally execute multiple cases, causing logical errors unless intentional, for example, allowing multiple cases to share the same code block. The 'break' statement prevents fall-through by exiting the switch structure after the current case executes .

A null terminator, '\0', marks the end of a string in C, allowing functions to determine the string's length. Without it, functions might read beyond the intended string boundary into adjacent memory, leading to undefined behavior like accessing garbage data or causing segmentation faults. Proper string termination is thus essential for correct string handling .

Infinite recursion occurs when a recursive function lacks a base case to terminate the recursion. This results in the function calling itself indefinitely, consequently leading to a stack overflow due to excessive memory usage on the call stack, often crashing the program. Ensuring well-defined stopping conditions is crucial to prevent infinite recursion .

Using the assignment operator '=' within an if statement leads to direct assignment instead of comparison, which can result in unexpected program logic, as the conditional often evaluates to true due to non-zero assignment. This is a common mistake, as developers might intend to use '==' for comparison, and this oversight can lead to logic errors where the condition appears to mistakenly evaluate true .

A semicolon immediately following a for loop in C makes it an empty loop, meaning that the loop executes its iterations without running any statements within it. This usually results in the loop doing nothing during its run, which can cause confusion and lead to unexpected program behavior. The correct body of the loop should follow in brackets after the loop statement .

Omitting the main() function in a C program allows the program to compile without errors because the compiler does not require main until execution. However, during runtime, the program would not execute as expected because main() is the entry point for execution. Without it, the program lacks a starting point and hence appears empty when run .

You might also like