First 50 of 199 questions. Each one lists the choices, the correct answer, and an explanation. The checker at the top of this page saves progress in this browser when it is available.
Continue in the practice tool above to load more questions, 50 at a time.
Question 1Program Structure, Compilation, and Preprocessing
In a translation unit, which declaration of an object with internal linkage at file scope is valid and gives every function in that file access to the same single object?
- static int counter = 0; Correct answer.
- extern int counter = 0;
- register int counter = 0;
- int counter;
Explanation
A file-scope declaration with the static storage-class specifier gives the object internal linkage, so all functions in that translation unit refer to one shared object that is not visible to the linker. The initializer is applied once at program start.
Question 2Program Structure, Compilation, and Preprocessing
A source file contains the line #include "config.h". Which search order does a conforming implementation apply for the quoted form?
- The file is searched only after all system headers have been processed.
- The current file's directory is searched first, then the implementation-defined search path. Correct answer.
- Only directories named by the -I option are searched.
- Only the system include directories are searched.
Explanation
For the quoted form the implementation searches the directory of the including file (or otherwise an implementation-defined place) first, then falls back to the same search used for the angle-bracket form. The angle-bracket form does not perform the current-directory search.
Question 3Program Structure, Compilation, and Preprocessing
What does the preprocessor do with the token ## when it appears in a macro replacement list?
- It marks the following token as optional and removes it.
- It evaluates the following expression at translation time.
- It concatenates the preceding and following preprocessing tokens into one token. Correct answer.
- It turns the following token into a string literal.
Explanation
The ## operator pastes adjacent preprocessing tokens together, forming a single token, and is applied during macro replacement. If the result is not a valid token the behavior is undefined.
Question 4Program Structure, Compilation, and Preprocessing
Which statement about the main function is correct for a hosted implementation?
- main must be declared with exactly two parameters.
- main may return any arithmetic type as long as a return statement is present.
- main may be defined with no parameters or with the two standard parameters, and it returns int. Correct answer.
- main must be declared static so that the linker can find it.
Explanation
A hosted implementation requires main to return int and permits two defined forms: one with no parameters, and one with an int and a pointer to pointer to char. Falling off the end of main returns zero.
Question 5Program Structure, Compilation, and Preprocessing
How does the compiler treat the declaration int * const p; at file scope?
- p is a const pointer whose target is also const.
- p is a constant pointer to a modifiable int, and p must be initialized. Correct answer.
- p is a pointer to a const int, and p may be reassigned.
- p is a pointer whose const qualifier is ignored at file scope.
Explanation
The const qualifies the pointer itself because it follows the *, so p cannot be assigned after initialization and therefore requires an initializer. The pointed-to int remains modifiable through p.
Question 6Program Structure, Compilation, and Preprocessing
Which of these identifiers is reserved for the implementation and must not be defined by a program?
- FILE_COUNT
- _Static_assert_impl Correct answer.
- my_header_guard
- MAX_SIZE
Explanation
Identifiers beginning with an underscore followed by an uppercase letter are reserved for any use, so a program may not define them. Names beginning with an underscore and a lowercase letter are reserved at file scope.
Question 7Program Structure, Compilation, and Preprocessing
A header defines a function body and is included by two source files that are linked together. What is the most likely result?
- The preprocessor silently keeps only the first copy.
- The program links but the function is called only once.
- The compiler merges the two definitions into one.
- The linker reports a multiple definition of the function. Correct answer.
Explanation
Each inclusion creates a definition in that translation unit, so linking two translation units that both define the same externally linked function violates the one-definition rule. The usual fix is to keep only a declaration in the header and the definition in one source file.
Question 8Program Structure, Compilation, and Preprocessing
Given #define SQR(x) x*x, what value does SQR(1+2) produce in the expression int v = SQR(1+2);?
- 7
- 9
- 5 Correct answer.
- 3
Explanation
Macro arguments are substituted textually without added parentheses, so SQR(1+2) expands to 1+2*1+2. Multiplication binds tighter than addition, giving 1+2+2, which is 5.
Question 9Program Structure, Compilation, and Preprocessing
What is the effect of the directive #undef LIMIT when LIMIT was previously defined as a macro?
- It removes the macro definition so LIMIT is no longer replaced. Correct answer.
- It redefines LIMIT to an empty replacement list.
- It is an error because LIMIT was already defined.
- It hides LIMIT only until the next #include.
Explanation
#undef cancels the current macro definition, after which the identifier is treated normally by the preprocessor. Undefining a name that is not currently defined is permitted and has no effect.
Question 10Program Structure, Compilation, and Preprocessing
A source file contains the line #if defined(FEATURE) && FEATURE > 2, and FEATURE is not defined. What happens?
- The whole expression evaluates to false and the group is skipped. Correct answer.
- A preprocessor error occurs because FEATURE is undefined.
- FEATURE is implicitly defined as zero.
- The condition is deferred to the compiler for evaluation.
Explanation
In a #if expression, an identifier that survives macro replacement is replaced by 0. Because the left operand of && is false, the group is skipped without an error.
Question 11Program Structure, Compilation, and Preprocessing
Which statement correctly describes the translation phases for trigraph and line-splicing handling?
- Neither is handled by the preprocessor.
- Trigraphs are replaced before backslash-newline splicing. Correct answer.
- Both are handled after macro expansion.
- Backslash-newline splicing happens before trigraph replacement.
Explanation
Trigraph replacement occurs in an early phase, and deletion of backslash-newline pairs follows it, both before tokenization and macro replacement. This ordering matters because a trigraph can form the backslash that begins a spliced line.
Question 12Program Structure, Compilation, and Preprocessing
A function is declared with the _Noreturn specifier. What does this communicate to the compiler?
- The function returns a value only on the first call.
- The function may be called only once in a program.
- The function is inlined at every call site.
- The function does not return to its caller, allowing optimizations such as omitting return handling. Correct answer.
Explanation
_Noreturn tells the compiler that control does not flow back to the caller, for example because the function calls exit or abort. The compiler may then omit code that would otherwise follow the call.
Question 13Program Structure, Compilation, and Preprocessing
What distinguishes a declaration of an object from a definition of that object at file scope?
- A declaration is allowed only inside functions.
- A definition is allowed only for objects with internal linkage.
- A declaration must include an initializer, while a definition never does.
- A declaration introduces the name and type, while a definition also reserves storage. Correct answer.
Explanation
A declaration makes the identifier and its type known; a definition additionally causes storage to be reserved for the object. For objects, a declaration without an initializer and with extern is only a declaration.
Question 14Program Structure, Compilation, and Preprocessing
A macro is defined as #define STR(x) #x. What does STR(a + b) expand to?
- "a" "+" "b"
- "ab"
- a + b
- "a + b" Correct answer.
Explanation
The # operator stringizes the argument by inserting the spelling of the argument tokens into a string literal, preserving internal spacing between tokens. The result is a single string literal containing a + b.
Question 15Program Structure, Compilation, and Preprocessing
Which construct ensures a header is processed only once per translation unit without relying on nonstandard pragmas?
- Declaring all header contents static.
- Wrapping the contents in #ifndef GUARD / #define GUARD / #endif. Correct answer.
- Placing #pragma once at the top of the header.
- Wrapping the contents in #if 1 / #endif.
Explanation
An include guard tests a macro that the header defines on first inclusion and skips the body on later inclusions within the same translation unit. It is portable because it uses only standard conditional compilation.
Question 16Program Structure, Compilation, and Preprocessing
What is the value of the predefined macro __LINE__ inside a macro expansion, and when is it determined?
- The total number of lines in the file.
- The line number of the source line being processed, determined at expansion. Correct answer.
- The line number of the #include that brought the file in.
- The line number of the macro definition, fixed at definition time.
Explanation
__LINE__ expands to the presumed line number of the current source line at the point of processing, so it changes from one use to the next. It can be used inside macros and reflects the line where the macro is invoked.
Question 17Program Structure, Compilation, and Preprocessing
A source file declares int f(void); and later defines static int f(void) { return 0; }. What is the result?
- The static definition silently overrides the earlier declaration.
- The definition is treated as a tentative definition.
- A conflict: the earlier declaration gives external linkage, so the static definition is invalid. Correct answer.
- The program compiles and f has internal linkage.
Explanation
If an identifier is first declared without a storage-class specifier at file scope, it has external linkage, and a later static declaration of the same identifier in the same translation unit has undefined behavior. The two declarations disagree about linkage.
Question 18Program Structure, Compilation, and Preprocessing
A macro is defined as #define CAT(a,b) a##b. What is the result of CAT(1,2) in a context expecting a preprocessing number?
- The result is the string "12".
- The result is two separate tokens 1 2.
- The result is a compile error because ## cannot join digits.
- The tokens 1 and 2 are pasted into the single token 12. Correct answer.
Explanation
The ## operator pastes the two argument tokens into one preprocessing token, and 12 is a valid preprocessing number. Pasting is done before the resulting tokens are rescanned for further macro replacement.
Question 19Program Structure, Compilation, and Preprocessing
A file contains #define X 1 followed later by #define X 2. What does a conforming implementation do?
- It uses the first definition and ignores the second.
- It concatenates both replacement lists.
- It diagnoses the redefinition because the replacement lists differ. Correct answer.
- It silently uses the second definition.
Explanation
A macro may be redefined only if the new definition is identical to the old one in spelling and whitespace separation; otherwise the program is ill-formed and a diagnostic is required. Redefinition with a different replacement list is therefore an error.
Question 20Program Structure, Compilation, and Preprocessing
In a #if expression, what does the defined operator test, and how is its operand treated?
- It tests whether the operand is a keyword and expands it first.
- It tests whether the operand is currently a defined macro and does not macro-expand the operand. Correct answer.
- It tests whether the operand is an integer constant and expands it first.
- It tests whether the operand has been included and expands it first.
Explanation
defined NAME or defined(NAME) yields 1 if NAME is currently defined as a macro and 0 otherwise. The operand is not subject to macro expansion, which is why defined can safely guard a name that may not exist.
Question 21Data Types, Declarations, and Type Conversions
In C, which of the following best describes the type of the literal 3.14?
- double Correct answer.
- float
- It depends on the compiler's default float type
- long double
Explanation
An unsuffixed floating constant in C has type double. The suffix f makes it float and the suffix l (or L) makes it long double. The compiler does not choose the type based on a default float setting.
Question 22Data Types, Declarations, and Type Conversions
What is the type of the expression 'A' in C?
- char
- int Correct answer.
- unsigned char
- const char
Explanation
In C, an ordinary character constant such as 'A' has type int, not char. This is why sizeof('A') equals sizeof(int) on typical implementations. The value is the character's value in the execution character set.
Question 23Data Types, Declarations, and Type Conversions
Given `int x = 7; int y = 2;`, what is the value of `x / y`?
- 3.5
- 4
- 3.0
- 3 Correct answer.
Explanation
Integer division truncates toward zero, so 7 / 2 yields 3. The fractional part is discarded rather than rounded. To obtain a floating result, at least one operand must be converted to a floating type.
Question 24Data Types, Declarations, and Type Conversions
Which declaration correctly defines a pointer to a constant integer?
- const int *p; Correct answer.
- int * const p;
- int *p const;
- int const * const p;
Explanation
`const int *p` declares p as a pointer to const int, meaning the pointed-to value cannot be modified through p. The pointer itself can be reassigned. Reading the declaration right to left clarifies which part is const.
Question 25Data Types, Declarations, and Type Conversions
Which header must be included to use the `size_t` type?
- <limits.h>
- <stdint.h>
- <stddef.h> Correct answer.
- <stdlib.h> only
Explanation
`size_t` is defined in <stddef.h>, and several other headers also define it. <stdint.h> defines fixed-width integer types, and <limits.h> defines limits of integer types. Relying on an unrelated header is not portable.
Question 26Data Types, Declarations, and Type Conversions
What does the `static` keyword do when applied to a variable declared inside a function?
- It places the variable in a register for faster access.
- It makes the variable visible to other translation units.
- It gives the variable a single lifetime across all calls to the function. Correct answer.
- It prevents the variable from being modified.
Explanation
A block-scope static variable has static storage duration: it is initialized once and retains its value between calls. Its name remains local to the block. The keyword does not affect const-ness or linkage of a local variable.
Question 27Data Types, Declarations, and Type Conversions
Which of the following is a valid identifier in C?
- 2ndValue
- int
- _count Correct answer.
- my-var
Explanation
An identifier may begin with a letter or an underscore and may contain letters, digits, and underscores. `_count` satisfies these rules. Identifiers beginning with an underscore at file scope are reserved, but the character rules still permit it.
Question 28Data Types, Declarations, and Type Conversions
Given `char c = 200;` on a platform where `char` is signed and 8 bits, what is the value of `c` after the assignment?
- 200
- 56
- Implementation-defined but guaranteed to be 200
- -56 Correct answer.
Explanation
A signed 8-bit char can hold values from -128 to 127. Converting 200 to signed char wraps modulo 256 into that range, giving -56. This conversion is implementation-defined in general but follows two's complement on typical platforms.
Question 29Data Types, Declarations, and Type Conversions
What is the result of the expression `(unsigned char)-1` on a platform with 8-bit bytes?
- 255 Correct answer.
- -1
- 1
- 0
Explanation
Converting -1 to unsigned char reduces it modulo 256, giving 255. Unsigned conversions wrap rather than saturate. The result is well defined by the C standard.
Question 30Data Types, Declarations, and Type Conversions
In `int *a[10];`, what is declared?
- An array of 10 pointers to int Correct answer.
- A pointer to an array of 10 ints
- A pointer to a pointer to int
- An array of 10 ints
Explanation
The postfix `[]` binds tighter than the `*`, so `int *a[10]` declares a as an array of 10 elements, each of type pointer to int. To declare a pointer to an array, parentheses are required: `int (*a)[10]`.
Question 31Data Types, Declarations, and Type Conversions
What is the type of the expression `a + b` when `a` is `int` and `b` is `double`?
- float
- double Correct answer.
- long double
- int
Explanation
The usual arithmetic conversions promote the int operand to double, so the result has type double. The int is converted rather than the double being demoted. The common type is the higher-ranked floating type.
Question 32Data Types, Declarations, and Type Conversions
Which of the following is true about the `_Bool` type?
- It is defined only in <stdbool.h>.
- It can store only the values 0 and 1. Correct answer.
- It cannot be used in arithmetic expressions.
- It is an alias for int in C99.
Explanation
`_Bool` is a distinct standard integer type that can represent 0 and 1. Assigning any nonzero value to a _Bool stores 1. The <stdbool.h> header provides the macro bool as an alias, but _Bool itself is a keyword.
Question 33Data Types, Declarations, and Type Conversions
What does the declaration `typedef int (*fnptr)(double);` define?
- A pointer type named fnptr to a function taking double and returning int Correct answer.
- A function named fnptr returning int
- A function taking a double and returning a pointer to int
- An array of function pointers named fnptr
Explanation
The typedef names the type `int (*)(double)`, which is a pointer to a function accepting a double and returning int. The parentheses around `*fnptr` are essential to bind the pointer to the name. It does not declare a function definition.
Question 34Data Types, Declarations, and Type Conversions
What is the value of `sizeof(char)` in C?
- 2 on most platforms
- It depends on whether char is signed
- 1, always Correct answer.
- Equal to sizeof(int)
Explanation
The C standard guarantees that sizeof(char) is 1 by definition of the size unit. A byte in C is the size of a char, which may be more than 8 bits. Other types are measured in multiples of this unit.
Question 35Data Types, Declarations, and Type Conversions
Given `unsigned int u = 1; int i = -2;`, what is the value of the expression `u + i` on a 32-bit two's complement platform?
- -1
- 4294967295 Correct answer.
- 1
- Undefined behavior
Explanation
The int operand is converted to unsigned int, so -2 becomes 4294967294. Adding 1 gives 4294967295. The arithmetic is performed modulo 2^32 and is well defined.
Question 36Data Types, Declarations, and Type Conversions
Which statement about the `volatile` qualifier is correct?
- It tells the compiler the object may change in ways the compiler cannot see. Correct answer.
- It makes the object read-only.
- It forces the object into a register.
- It guarantees atomic access to the object.
Explanation
`volatile` informs the compiler that accesses to the object may have side effects, so it must not optimize them away. It does not provide atomicity or synchronization. Read-only access is specified with `const`.
Question 37Data Types, Declarations, and Type Conversions
Given `enum color { RED, GREEN = 5, BLUE };`, what is the value of BLUE?
- 0
- 5
- 6 Correct answer.
- 2
Explanation
An enumerator without an explicit initializer takes the value of the previous enumerator plus one. Since GREEN is 5, BLUE becomes 6. RED is 0 because it is the first enumerator.
Question 38Data Types, Declarations, and Type Conversions
What is the type of the expression `c` after integer promotion when `c` is declared as `unsigned char`?
- unsigned char
- char
- unsigned int
- int Correct answer.
Explanation
Integer promotion converts a value of type unsigned char to int if int can represent all values of unsigned char, which is true on typical platforms. The promoted type is int, not unsigned int. This promotion occurs before arithmetic is performed.
Question 39Data Types, Declarations, and Type Conversions
Given `int arr[5];`, what is the type of the expression `arr` in most contexts?
- int **
- int
- int[5]
- int * Correct answer.
Explanation
In most expressions, an array name decays to a pointer to its first element, giving type int *. The exceptions are when the array is an operand of sizeof, _Alignof, or the unary & operator, or when it initializes a character array from a string literal.
Question 40Data Types, Declarations, and Type Conversions
Which declaration declares an array of 3 pointers to functions taking int and returning void?
- void (*f)(int)[3];
- void f[3](int);
- void (*f[3])(int); Correct answer.
- void *f[3](int);
Explanation
The parentheses bind the pointer to the function declarator, and the [3] makes it an array of three such pointers. Reading outward from the name confirms f is an array of pointers to functions. The other forms are either invalid or declare different types.
Question 41Data Types, Declarations, and Type Conversions
On a platform where `int` is 32 bits, what does the expression `(int)4294967295u` yield?
- Implementation-defined behavior
- 4294967295
- -1 Correct answer.
- 0
Explanation
Converting an out-of-range unsigned value to a signed integer is implementation-defined in C, but on two's complement platforms it produces the value whose bit pattern matches, which is -1. The question specifies a 32-bit int and the usual two's complement representation. The bit pattern of 4294967295u is all ones, interpreted as -1.
Question 42Data Types, Declarations, and Type Conversions
Consider `const char *p = "hello";`. Which operation is permitted without invoking undefined behavior?
- free(p);
- p = "world"; Correct answer.
- p[0] = 'H';
- Modifying the string through a cast to char *
Explanation
The pointer p itself is not const, so it can be reassigned to point elsewhere. The string literal's characters must not be modified. Casting away const and writing to a string literal is undefined behavior.
Question 43Data Types, Declarations, and Type Conversions
Given `struct S { char a; int b; };`, which statement about its size is correct?
- sizeof(struct S) is always 5.
- sizeof(struct S) is always 8.
- sizeof(struct S) equals sizeof(char) + sizeof(int) by definition.
- sizeof(struct S) may exceed 5 because of padding. Correct answer.
Explanation
Compilers may insert padding bytes between members to satisfy alignment requirements, so the size can exceed the sum of member sizes. The exact amount is implementation-defined. Only the order of members is guaranteed, not a specific total size.
Question 44Data Types, Declarations, and Type Conversions
What is the value of `sizeof(int) == sizeof(unsigned int)` in a conforming C implementation?
- True only when int is 32 bits
- Always true Correct answer.
- Implementation-defined
- Always false
Explanation
The C standard requires corresponding signed and unsigned integer types to have the same size and alignment. Therefore sizeof(int) always equals sizeof(unsigned int). The same rule applies to other signed/unsigned pairs such as long and unsigned long.
Question 45Operators, Expressions, and Evaluation Order
In C, which operator has the highest precedence among the following?
- * (binary multiplication)
- + (binary addition)
- [] (array subscript) Correct answer.
- && (logical AND)
Explanation
Postfix operators such as array subscript, function call, and the member-access operators bind most tightly, above all unary and binary arithmetic operators. Multiplication and addition rank lower, and logical AND is far lower still. So array subscript wins over the others listed.
Question 46Operators, Expressions, and Evaluation Order
Given `int a = 7, b = 2;`, what is the value of `a / b` in C?
- 3.5
- 4
- 2
- 3 Correct answer.
Explanation
When both operands of / are integers, C performs integer division and truncates the fractional part toward zero. Here 7 divided by 2 yields 3. The result is an int, so no fractional value survives.
Question 47Operators, Expressions, and Evaluation Order
What does the `%` operator require of its operands in C?
- The left operand must be a pointer.
- Both operands must be floating-point.
- Both operands must be integer types. Correct answer.
- The right operand must be a constant expression.
Explanation
The remainder operator % is defined only for integer operands in standard C. Applying it to floating-point values is a constraint violation. Division /, by contrast, accepts floating operands.
Question 48Operators, Expressions, and Evaluation Order
What is the value of the expression `5 + 3 * 2 - 4`?
- 14
- 7 Correct answer.
- 12
- 10
Explanation
Multiplication binds tighter than addition and subtraction, so 3 * 2 is evaluated first to give 6. Then the expression is evaluated left to right: 5 + 6 = 11, and 11 - 4 = 7. Addition and subtraction share the same precedence and associate left to right.
Question 49Operators, Expressions, and Evaluation Order
In the expression `a = b = c`, what does the assignment operator return?
- A boolean indicating success.
- A pointer to the left operand.
- Nothing; assignment is a statement with no value.
- The value that was assigned to the left operand. Correct answer.
Explanation
In C, assignment is an expression, not merely a statement, and its result is the value stored in the left operand after conversion. That is why chained assignment like a = b = c works: b = c yields the value of b, which is then assigned to a. The associativity of = is right to left.
Question 50Operators, Expressions, and Evaluation Order
Which statement about the `&&` operator is true?
- It short-circuits: the right operand is not evaluated if the left is false. Correct answer.
- It always evaluates both operands.
- It performs bitwise AND on the operands.
- It has higher precedence than `==`.
Explanation
The logical AND operator && evaluates its left operand first; if that is false (zero), the right operand is not evaluated at all. This short-circuit behavior is guaranteed by the standard and is widely relied upon. Bitwise AND is a different operator, &, and && ranks below equality.