Skip to content
QuiznExam

C Preprocessor Quiz – 15 Questions with Answers

Test your knowledge of the C preprocessor with 15 questions covering preprocessor directives, macros, header files, conditional compilation, include files, and predefined macros.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

const in C

15 questions · 12 min · Medium

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. What is the primary purpose of the C preprocessor?

    • A. To execute the C program
    • B. To process source code before compilation
    • C. To allocate memory at runtime
    • D. To debug the program
    Show answer

    B. To process source code before compilation

    The C preprocessor processes directives and performs tasks such as macro expansion and header inclusion before the source code is compiled.

  2. 2. Which symbol begins a preprocessor directive in C?

    • A. $
    • B. @
    • C. #
    • D. %
    Show answer

    C. #

    C preprocessor directives begin with the # symbol, such as #include and #define.

  3. 3. Which preprocessor directive is used to define a macro?

    • A. #macro
    • B. #define
    • C. #constant
    • D. #def
    Show answer

    B. #define

    The #define directive is used to define object-like and function-like macros.

  4. 4. What does the following directive do? #define PI 3.14159

    • A. Creates a runtime variable named PI
    • B. Defines a macro named PI
    • C. Creates a function named PI
    • D. Allocates memory for PI
    Show answer

    B. Defines a macro named PI

    The #define directive creates a macro. During preprocessing, occurrences of PI are replaced according to the macro definition.

  5. 5. Which preprocessor directive is used to include a header file?

    • A. #include
    • B. #header
    • C. #import
    • D. #using
    Show answer

    A. #include

    The #include directive tells the preprocessor to include the contents of another file at that point in the source.

  6. 6. Which syntax is commonly used to include a standard C library header?

    • A. #include <stdio.h>
    • B. #include (stdio.h)
    • C. #header <stdio.h>
    • D. include [stdio.h]
    Show answer

    A. #include <stdio.h>

    Angle brackets are commonly used with #include for system or standard headers, such as #include <stdio.h>.

  7. 7. Which syntax is commonly used to include a user-defined header file?

    • A. #include <myheader.h>
    • B. #include "myheader.h"
    • C. #header "myheader.h"
    • D. #import "myheader.h"
    Show answer

    B. #include "myheader.h"

    Double quotes are commonly used when including user-defined headers, such as #include "myheader.h".

  8. 8. Which directive is used for conditional compilation when a macro is defined?

    • A. #ifdef
    • B. #ifdefined
    • C. #condition
    • D. #defined
    Show answer

    A. #ifdef

    #ifdef conditionally includes code when the specified macro has been defined.

  9. 9. Which directive is used to test whether a macro has not been defined?

    • A. #ifnot
    • B. #ifndef
    • C. #notdefined
    • D. #undefine
    Show answer

    B. #ifndef

    #ifndef means 'if not defined' and conditionally includes code when the specified macro is not defined.

  10. 10. What does the #undef directive do?

    • A. Creates a new macro
    • B. Removes the definition of a macro
    • C. Includes a header file
    • D. Stops compilation permanently
    Show answer

    B. Removes the definition of a macro

    The #undef directive cancels the current definition of a macro.

  11. 11. Which directive provides an alternative block when an #if condition is false?

    • A. #otherwise
    • B. #else
    • C. #alternative
    • D. #default
    Show answer

    B. #else

    The #else directive specifies the alternative section of code when the preceding conditional preprocessing condition is false.

  12. 12. Which directive ends a conditional preprocessing block?

    • A. #end
    • B. #endif
    • C. #end if
    • D. #stop
    Show answer

    B. #endif

    The #endif directive marks the end of a conditional preprocessing block started by directives such as #if, #ifdef, or #ifndef.

  13. 13. What is the main advantage of function-like macros?

    • A. They can represent reusable code-like substitutions
    • B. They always provide type safety
    • C. They are stored as runtime functions
    • D. They automatically allocate memory
    Show answer

    A. They can represent reusable code-like substitutions

    Function-like macros can accept arguments and perform textual substitution during preprocessing, allowing reusable code patterns.

  14. 14. Which predefined macro is commonly used to obtain the current source file name?

    • A. __FILE__
    • B. __SOURCE__
    • C. __NAME__
    • D. __FILENAME__
    Show answer

    A. __FILE__

    The predefined macro __FILE__ expands to the name of the current source file as a string literal.

  15. 15. Which predefined macro is commonly used to obtain the current source line number?

    • A. __LINE__
    • B. __ROW__
    • C. __LINE_NUMBER__
    • D. __SOURCE_LINE__
    Show answer

    A. __LINE__

    The predefined macro __LINE__ expands to the current source line number and is commonly useful for diagnostics and debugging.