C Programming Tricky MCQ

Previous C Programming Tricky Questions Next

C Language History, Basics & Tricky Program Structure

1

Who developed the C programming language and in which year?

Correct Answer: B) Dennis Ritchie in 1972 at Bell Labs

C was developed by Dennis Ritchie at Bell Laboratories (formerly AT&T Bell Labs) between 1969 and 1973. It was primarily created to rewrite the Unix operating system, which was originally written in assembly language. The language evolved from B (which was based on BCPL) and was named "C" as it succeeded "B".

2

What is the result of this expression: sizeof('a') in C?

Correct Answer: C) 4 bytes (size of int)

This is a tricky C feature: In C, character constants like 'a' are of type int, not char! So sizeof('a') returns the size of an int (typically 4 bytes on modern systems), not 1. This is different from C++ where character literals are of type char. Example: printf("%zu", sizeof('a')); // Output: 4 (not 1)

3

What is the correct entry point for a C program?

Correct Answer: D) Both A and C are correct

Tricky! According to the C standard, int main() is the correct signature. However, many compilers accept just main() (which implicitly returns int) for backward compatibility. void main() is not standard C (though some compilers allow it). The most portable is int main(void) or int main(int argc, char *argv[]).

4

What will this code output: printf("%d", 5["Hello"]);

Correct Answer: B) ASCII value of 'o' (111)

This is a tricky C feature: In C, a[b] is equivalent to *(a + b). Therefore, 5["Hello"] is the same as "Hello"[5], which accesses the 6th character (index 5) = 'o'. The ASCII value of 'o' is 111. This works because array indexing is commutative due to pointer arithmetic.

// Proof: a[b] == *(a + b) == *(b + a) == b[a]
5

Which is NOT a valid C comment style?

Correct Answer: C) # Shell-style comment

Tricky history question: /* */ is the original C comment style. // was added in C99 (1999 standard). The # is used for preprocessor directives, not comments. In early C (K&R C), only /* */ comments existed. Some compilers might accept // in C89 mode as an extension, but it's not standard until C99.

6

What is the output: int i = 0; printf("%d %d", i++, i++);

Correct Answer: C) Undefined behavior

This is a classic tricky question! When you modify a variable multiple times between sequence points (like in the same printf statement), it's undefined behavior in C. The compiler can evaluate arguments in any order, and the result is unpredictable. Different compilers may produce different results (0 1, 1 0, or something else).

7

Which is true about C preprocessor directives?

Correct Answer: C) They start with # and don't need semicolon

Preprocessor directives like #include, #define, #ifdef, etc., start with # and are processed before compilation (by the preprocessor). They don't end with semicolons (except when the macro expansion includes one). Common mistake: People add semicolons after #include or #define which causes errors.

8

What is the size of an empty structure in C?

Correct Answer: D) Depends on compiler (usually 0 or 1)

In C, an empty structure is invalid according to the standard (C99/C11). However, many compilers allow it as an extension. GCC gives it size 0, while some compilers give it size 1 (to ensure each struct has a unique address). In C++, empty structs have size 1. This is a compiler-dependent behavior, not standard C.

9

Which of these is NOT a valid C identifier?

Correct Answer: B) 2variable

C identifiers must start with a letter (a-z, A-Z) or underscore (_), followed by letters, digits, or underscores. 2variable starts with a digit, so it's invalid. However, _2variable would be valid. Case matters in C identifiers (VariableName ≠ variablename).

10

What does the "volatile" keyword do in C?

Correct Answer: B) Prevents compiler optimization for that variable

volatile tells the compiler that the variable's value may change at any time without any action being taken by the code (e.g., by hardware, interrupts, or other threads). This prevents the compiler from optimizing away "redundant" reads or writes. It's commonly used for memory-mapped hardware registers, variables shared with interrupt service routines, or global variables in multithreaded applications.

11

What is the value of: sizeof(struct {int a; char b;})?

Correct Answer: D) Both B and C

Due to structure padding for alignment, the size is typically 8 bytes on 32-bit systems (4 for int, 1 for char, plus 3 padding bytes). However, this is compiler and architecture dependent. On some systems with 1-byte alignment (#pragma pack(1)), it could be 5 bytes. The exact size depends on the compiler, target architecture, and alignment requirements.

12

Which standard introduced boolean type (_Bool) in C?

Correct Answer: B) C99

The _Bool type was introduced in the C99 standard. Before C99, C programmers used int (0 for false, non-zero for true) or defined their own boolean types. C99 also added <stdbool.h> which defines bool, true, and false as macros. In original K&R C and ANSI C89, there was no built-in boolean type.

13

What is the difference between ++i and i++ when used alone?

Correct Answer: D) Both A and B

Tricky! Conceptually, ++i (pre-increment) increments then returns the new value, while i++ (post-increment) returns the original value then increments. However, when used as standalone statements (not in expressions), modern compilers generate identical code for both, so there's no performance difference. The difference matters only when the value is used in a larger expression.

14

What is "The C Programming Language" book commonly called?

Correct Answer: B) K&R (Kernighan and Ritchie)

The classic C programming book by Brian Kernighan and Dennis Ritchie is affectionately called "K&R". The first edition (1978) described the original K&R C. The second edition (1988) covered ANSI C (C89). It's known for its concise style and is considered one of the best programming books ever written. Its famous "hello, world" program introduced this tradition.

15

What is the result of: printf("%d", 010);

Correct Answer: B) 8 (octal 010 = decimal 8)

In C, integer constants starting with 0 are interpreted as octal (base 8). So 010 in octal = 8 in decimal. Similarly, 0x prefix means hexadecimal (0x10 = 16 decimal). This is a common source of bugs when people accidentally add leading zeros to decimal numbers. Example: 0123 = 1×64 + 2×8 + 3 = 83 decimal.

Previous C Programming Tricky Questions Next