C Programming Preprocessor Directives Quiz
31 Questions
4 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Preprocessor directives in C are executed after the program is compiled.

False (B)

Which of the following symbols is used to start a preprocessor directive?

  • *
  • !
  • # (correct)
  • %
  • What is the primary purpose of a macro in C?

    Macro substitution replaces a named code block with its actual definition.

    The #define directive is used to define a _____.

    <p>macro</p> Signup and view all the answers

    Which of the following preprocessor directives allows you to include a header file in your C program?

    <p>#include (C)</p> Signup and view all the answers

    Match the following preprocessor directives with their primary functionalities.

    <p>#define = Macro definition #include = File inclusion #ifdef = Conditional compilation</p> Signup and view all the answers

    In the directive #include <stdio.h>, the angle brackets (<>) indicate that the compiler should search for the file in the ______ directory.

    <p>standard</p> Signup and view all the answers

    A semi-colon (;) is required at the end of a preprocessor directive definition.

    <p>False (B)</p> Signup and view all the answers

    What is the purpose of the #if preprocessor directive?

    <p>To conditionally compile code based on a condition (C)</p> Signup and view all the answers

    The #if directive evaluates an ______ to determine whether to compile the code following it.

    <p>expression</p> Signup and view all the answers

    If the condition specified in the #if directive is false, the code following the #if will be executed.

    <p>False (B)</p> Signup and view all the answers

    The #undef preprocessor directive is used to ____ a constant or macro previously defined by #define.

    <p>undefine</p> Signup and view all the answers

    What does #endif signify in the context of #if directives?

    <p>The end of the <code>#if</code> block.</p> Signup and view all the answers

    The #undef directive defines a preprocessor constant to a limited scope.

    <p>False (B)</p> Signup and view all the answers

    What is the correct syntax for using the #undef preprocessor directive?

    <p>#undef token (C)</p> Signup and view all the answers

    If the #if condition is false, the code inside the ______ block will be executed.

    <p>#else</p> Signup and view all the answers

    What happens when you use #undef on a constant or macro that has not been defined previously?

    <p>It has no effect.</p> Signup and view all the answers

    Which of these preprocessor directives can be used in conjunction with #if to create multiple conditions?

    <p>#elif (A)</p> Signup and view all the answers

    In the provided code snippet, the #if condition is true because the variable a is defined and initialized.

    <p>False (B)</p> Signup and view all the answers

    Match the following preprocessor directives with their respective actions:

    <p><code>#define</code> = Defines a constant or macro <code>#include</code> = Includes a header file <code>#undef</code> = Undefines a constant or macro <code>#ifdef</code> = Checks if a constant is defined <code>#ifndef</code> = Checks if a constant is not defined</p> Signup and view all the answers

    Match the following preprocessor directives with their descriptions:

    <p>#if = Starts a block of code that is conditionally compiled based on an expression #else = Starts a block of code that is executed only if the preceding <code>#if</code> or <code>#elif</code> condition is false #endif = Marks the end of an <code>#if</code> block #elif = Starts a block of code that is executed only if the preceding <code>#if</code> or <code>#elif</code> condition is false and a new expression is true</p> Signup and view all the answers

    In the provided program example, what output will be generated after compilation and execution?

    <p>Value of Number is non-zero (C)</p> Signup and view all the answers

    The #undef directive is used to define the preprocessor constant to a limited scope.

    <p>False (B)</p> Signup and view all the answers

    What is the purpose of the getch() function used in the provided program example?

    <p>The <code>getch()</code> function waits for a key press from the user before ending the program.</p> Signup and view all the answers

    What is the purpose of the #ifdef directive in C?

    <p>It is used to conditionally compile code based on the existence of a macro. (B)</p> Signup and view all the answers

    The #else directive is optional in an #ifdef block.

    <p>True (A)</p> Signup and view all the answers

    What is the output of the following C code snippet?

    #include <stdio.h>
    #define X 10
    
    int main() {
      #ifdef X
      printf("one");
      #else
      printf("two");
      #endif
      return 0;
    }
    

    <p>one</p> Signup and view all the answers

    The #endif directive marks the end of a(n) _____ block.

    <p>conditional compilation</p> Signup and view all the answers

    Which of the following C code snippets will output "two"?

    <pre><code class="language-C">#define Y 10 int main() { #ifdef X printf(&quot;one&quot;); #else printf(&quot;two&quot;); #endif return 0; } ``` (A) </code></pre> Signup and view all the answers

    What is the main difference between #ifdef and #ifndef directives in C?

    <p><code>#ifdef</code> checks if a macro is defined, while <code>#ifndef</code> checks if a macro is <em>not</em> defined.</p> Signup and view all the answers

    Conditional compilation can be used to create platform-specific code in C programs.

    <p>True (A)</p> Signup and view all the answers

    Flashcards

    Preprocessor Directives

    Statements processed before compilation in C.

    Macro Processor

    Another name for the C preprocessor.

    Macro Substitution

    Replaces a name with a code snippet using #define.

    File Inclusion

    Directives to include files in a C program.

    Signup and view all the flashcards

    Standard Directory Search

    Using #include <> to find files in standard locations.

    Signup and view all the flashcards

    Source File Directory Search

    Using #include "filename" to find local files.

    Signup and view all the flashcards

    Conditional Compilation

    Compile specific code sections based on conditions.

    Signup and view all the flashcards

    #ifdef Directive

    Checks if a macro is defined, executes corresponding code.

    Signup and view all the flashcards

    #ifdef

    A preprocessor directive that checks if a macro is defined.

    Signup and view all the flashcards

    #else

    A preprocessor directive that provides an alternative to #ifdef.

    Signup and view all the flashcards

    #endif

    A preprocessor directive that marks the end of an #ifdef block.

    Signup and view all the flashcards

    macro_name

    The name of the macro checked by #ifdef.

    Signup and view all the flashcards

    clrscr()

    A function that clears the screen in a console application.

    Signup and view all the flashcards

    printf()

    A function that outputs formatted text to the console.

    Signup and view all the flashcards

    #define

    A preprocessor directive that defines a macro.

    Signup and view all the flashcards

    #if directive

    Preprocessor directive that executes code based on a condition.

    Signup and view all the flashcards

    #else directive

    Executes alternative code if the #if condition is false.

    Signup and view all the flashcards

    #elif directive

    Allows multiple conditions in preprocessor directives.

    Signup and view all the flashcards

    #endif directive

    Marks the end of a conditional preprocessor block.

    Signup and view all the flashcards

    Preprocessor

    Software that modifies code before compilation based on directives.

    Signup and view all the flashcards

    scanf function

    Standard function to read input from the user in C.

    Signup and view all the flashcards

    printf function

    Standard function to print output to the console in C.

    Signup and view all the flashcards

    Macro Definitions

    Named constants or functions in C that are defined for use in preprocessor directives.

    Signup and view all the flashcards

    Macro

    A name defined with #define that represents a value or expression.

    Signup and view all the flashcards

    Scope

    The context in which a macro or constant is valid or accessible.

    Signup and view all the flashcards

    Compile Time Error

    An error detected by the compiler before the code runs.

    Signup and view all the flashcards

    Study Notes

    Preprocessors in C

    • Preprocessor directives are processed before compilation, typically at the start of a program.
    • They begin with a '#' symbol.
    • Preprocessors are also known as macro processors.
    • Three main types of preprocessor directives exist: macro substitution, file inclusion, and conditional compilation.

    Macro Substitution

    • Macros are named code snippets within a program.
    • The compiler replaces the name with the code whenever it encounters it.
    • The #define directive is used to define macros.
    • Example syntax: #define token value or #define first_part second_part.
    • No semicolon is needed after a macro definition.
    • An example macro definition: #define SIZE 10.

    File Inclusion

    • This directive includes external files into the source code.
    • Header files are typically included using this directive.
    • Standard syntax: #include <filename>. (compiler searches standard directories)
    • Alternate syntax with quotes: #include "filename" (compiler searches current directory and standard directories)

    Conditional Compilation

    • This allows conditional inclusion or exclusion of code parts during compilation.

    • #ifdef: Checks if a macro is defined.

    • If defined, the code block after #ifdef is compiled; otherwise, the code after #else (if present) is compiled.

    • #endif: Marks the end of a conditional block.

    • Example syntax:

      #ifdef macro_name
      //code
      #else
      //alternative code
      #endif
      
    • #if: Evaluates a conditional expression.

    • If true, the code block within #if is processed; otherwise, the code after #else (if present) or #elif(if present) is processed.

    • #elif: used for additional conditions.

    • #endif : Marks the end of all conditional blocks.

    • #undef: Removes a macro definition, useful for declaring variables with same names in different scope of the code or to limit scope of preprocessor constant, like macro.

    • Example syntax: #undef token.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Preprocessors in C PDF

    Description

    Test your knowledge of preprocessor directives in C with this quiz. Explore concepts like macros, the use of #define, and the significance of directives such as #include and #if. Perfect for students looking to strengthen their understanding of C programming.

    More Like This

    Use Quizgecko on...
    Browser
    Browser