C Programming Tricky Questions

Tricky Questions on Conditional Statements

What is the difference between if(x = 5) and if(x == 5)?
if(x = 5) assigns 5 to x, then checks if x is non-zero (always true). if(x == 5) compares x with 5. The first is a common bug where assignment (=) is used instead of equality (==).
int x = 0; if(x = 5) { // Assigns 5 to x, condition is always true printf("Always executes\n"); } if(x == 5) { // Correct comparison printf("Executes only if x equals 5\n"); }
What is dangling else problem and how is it resolved in C?
When an else can belong to multiple if statements, it's ambiguous. C resolves this by associating else with the nearest unmatched if. Use braces {} to avoid confusion.
if(condition1) if(condition2) statement1; else // This else belongs to the inner if statement2; // To make else belong to outer if: if(condition1) { if(condition2) statement1; } else { statement2; }
What happens when you put a semicolon after if condition?
It creates an empty statement as the if body. The code block after if (even if indented) executes unconditionally. This is a common syntax error.
if(x > 0); // SEMICOLON ERROR: empty if body { printf("This always prints!\n"); // Not part of if }
Warning: This bug can be hard to spot. The code compiles but behaves incorrectly.
What is the value of if(5) or if(-3)?
In C, any non-zero value is considered true. if(5) and if(-3) are both true. Only if(0) or if(NULL) are false.
Tricky Point: Many languages have boolean types, but C uses integers: 0 = false, non-zero = true.
What is short-circuit evaluation in if conditions?
For && (AND): if left operand is false, right operand is not evaluated. For || (OR): if left operand is true, right operand is not evaluated. This can prevent errors.
if(ptr != NULL && ptr->value > 0) { // Safe: if ptr is NULL, ptr->value is not evaluated } if(x == 0 || 10/x > 2) { // Safe: if x is 0, division is not evaluated }
What is the difference between else-if ladder and multiple if statements?
Else-if ladder: conditions checked sequentially, stops at first true. Multiple ifs: all conditions checked independently. Else-if is for mutually exclusive cases, multiple ifs for independent checks.
// Else-if ladder (mutually exclusive) if(grade >= 90) printf("A"); else if(grade >= 80) printf("B"); // Only if first was false else if(grade >= 70) printf("C"); // Only if both above were false // Multiple ifs (independent checks) if(x > 0) printf("Positive"); if(x % 2 == 0) printf(" Even"); // Could print both "Positive Even"
What is the output of if(0.1 + 0.2 == 0.3) in C?
It's usually false due to floating-point precision errors. 0.1 + 0.2 ≠ 0.3 exactly in binary floating point. Always compare floats with tolerance: fabs(a - b) < epsilon.
float a = 0.1, b = 0.2, c = 0.3; if(a + b == c) { // Usually FALSE printf("Equal\n"); } // Correct way: if(fabs((a + b) - c) < 0.0001) { printf("Close enough\n"); }
Can you have an if without else? Can you have else without if?
Yes to if without else. No to else without if - it's a syntax error. else must immediately follow an if or else if.
What is the difference between nested if and else-if?
Nested if: if inside another if (hierarchical). Else-if: sequential alternatives at same level. Nested if checks condition only if outer if is true.
// Nested if if(x > 0) { if(y > 0) { // Only checked if x > 0 printf("Both positive\n"); } } // Else-if (different structure) if(x > 0) { printf("Positive\n"); } else if(x < 0) { printf("Negative\n"); }
What is the output of if('A' == 65)?
It's true if using ASCII encoding (most common). Character 'A' has ASCII value 65. In C, characters are integers, so this comparison is valid.
Tricky Point: This works because chars are promoted to ints in expressions. The actual value depends on character encoding (ASCII, EBCDIC, etc.).
What happens with if(x = 0)?
It assigns 0 to x, then checks if x is non-zero. Since x is now 0 (false), the if body doesn't execute. This is often a bug when programmer meant if(x == 0).
Can you put a declaration inside if condition?
In C99 and later: yes, with limitations. The variable scope is limited to the if statement. Not allowed in traditional C (C89).
// C99 and later: if(int x = some_function()) { // x is available here printf("%d\n", x); } // x is NOT available here
What is the difference between if(ptr) and if(ptr != NULL)?
They are equivalent in C. if(ptr) checks if pointer is not NULL (non-zero). if(ptr != NULL) is more explicit but longer. Both are commonly used.
What is the ternary operator and how does it differ from if-else?
Ternary: condition ? expr1 : expr2. Returns a value, can be used in expressions. If-else: control flow statement, doesn't return value. Ternary is for simple choices in expressions.
// Ternary operator (returns value) int max = (a > b) ? a : b; printf("%d\n", (x > 0) ? x : -x); // Absolute value // If-else statement if(a > b) { max = a; } else { max = b; }
What is fall-through in switch vs else-if?
In switch, cases fall through to next case unless you use break. In else-if ladder, only one block executes (no fall-through). This is a key difference between switch and else-if.
What is Duff's device and how does it relate to if statements?
Duff's device is a loop unrolling technique that uses switch statement inside a loop. Not directly about if, but shows C's flexibility with control structures. It's a famous tricky C idiom.
Can you use floating-point numbers as switch case labels?
No, switch cases must be integer constants (including chars). For floating-point comparisons, use if-else ladder. This is a limitation of switch statement.
What is the difference between if(x & 1) and if(x % 2 == 1)?
Both check if x is odd, but work differently: & 1 does bitwise AND (checking LSB), % 2 does modulo division. For positive integers, same result. For negative: % can give -1 (in C), while & gives 1.
int x = -3; if(x & 1) { // True: -3 & 1 = 1 printf("Odd by bitwise\n"); } if(x % 2 == 1) { // False: -3 % 2 = -1 (not equal to 1) printf("Odd by modulo\n"); }
What is the "ifdef" directive and how is it different from if?
#ifdef is a preprocessor directive checked at compile time. if is a runtime statement. #ifdef can conditionally include/exclude code from compilation.
// Preprocessor - compile-time decision #ifdef DEBUG printf("Debug mode\n"); #endif // Runtime if statement if(debug_flag) { // Evaluated at runtime printf("Debug mode\n"); }
What is the comma operator in if conditions?
Comma operator evaluates both expressions, returns the second one's value. Can be used in if but often confusing. Example: if(x = 5, y = 10, x < y) sets x=5, y=10, checks if x
int x, y; if(x = 5, y = 10, x < y) { // Sets x=5, y=10, checks 5<10 (true) printf("x=%d, y=%d\n", x, y); }
Warning: Comma operator in if conditions is rarely needed and can make code hard to read. Use separate statements instead.
What is the difference between if(!strcmp(a, b)) and if(strcmp(a, b) == 0)?
They are equivalent. strcmp() returns 0 if strings equal. !0 is 1 (true). Both check if strings are equal. The first uses logical NOT, second explicit comparison.
char *a = "hello", *b = "hello"; if(!strcmp(a, b)) { // True if strings equal printf("Equal\n"); } if(strcmp(a, b) == 0) { // Same check, more explicit printf("Equal\n"); }
Can you nest ternary operators?
Yes, but it becomes hard to read. Use parentheses for clarity. Nested ternary is like nested if-else but in expression form.
// Nested ternary (hard to read) int result = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c); // Same logic with if-else (clearer) int result; if(a > b) { result = (a > c) ? a : c; } else { result = (b > c) ? b : c; }
What is the "if-else-if" ladder without final else equivalent to?
It's equivalent to nested if statements where each condition is checked only if all previous conditions were false. The final else is optional - acts as default case if none match.
What is the problem with if(0 <= x <= 10) to check if x is between 0 and 10?
It doesn't work as expected. C evaluates left to right: (0 <= x) gives 0 or 1, then (0 or 1) <= 10 is always true. Correct way: if(0 <= x && x <= 10).
int x = 20; if(0 <= x <= 10) { // WRONG: always true! // (0 <= 20) = 1, then (1 <= 10) = true printf("This prints even though x=20!\n"); } if(0 <= x && x <= 10) { // CORRECT printf("Only prints if 0 <= x <= 10\n"); }
What is the difference between logical AND (&&) and bitwise AND (&) in if conditions?
Logical AND (&&): returns 0 or 1, short-circuits. Bitwise AND (&): performs bitwise operation, doesn't short-circuit. Use && for boolean logic, & for bitmask operations.
int x = 5, y = 3; if(x && y) { // True: both non-zero printf("Logical AND: true\n"); } if(x & y) { // True: 5 (0101) & 3 (0011) = 1 (non-zero) printf("Bitwise AND: %d\n", x & y); // Prints 1 } // Check if both bits 0 and 2 are set: if((x & 5) == 5) { // 5 = 0101 in binary printf("Bits 0 and 2 are set\n"); }
Note: These tricky questions cover common pitfalls, edge cases, and subtle behaviors of if, if-else, else-if ladder, and nested if statements in C. Understanding these concepts is crucial for writing correct and efficient conditional logic.
Previous if, if-else, else-if ladder & nested if Next