Book Your slot
X
ONLINE BOOKING
BOOK NOW
OFFLINE BOOKING
Call or WhatsApp 7993732682 (WhatsApp Now), 9177341827 (WhatsApp Now)
search
Menu Login home
  • Questions

  • Library

  • University Updates

  • Informatives

  • Technology Lines

  • Training & Internships

  • X
    Menu
  • Home
  • Privacy Policy
  • Legal Disclaimer
  • Terms & Conditions
  • Return Policy
  • About Us
  • Need any help?? write to us at

    support@engineershub.co

    Follow Us

    X
    LOGIN
    Login to access posts, links, updates, question papers, materials, one liners!
    Use Your Email Address/Mobile and Password to Login
    Forgot Password?
    Not a member? Sign Up
    LOGIN WITH EMAIL/MOBILE
    Forgot Password?
    Go Back
    FORGOT PASSWORD
    Go Back
    RESET PASSWORD
    Go Back
    Continue with LinkedIn
    OR
    Fill Up a Simple Form
    Already a Member? Login
    SIGN UP
    Fill all the below details correctly and click on Next
    Go Back
    Explain how a preprocessor works in C? - EngineersHub
    Go Back
    Question
    Anvesh Kanchibhotla
    4 months ago
    1 Answer(s) posted Write an answer 233
    Answer
    Read Mode
    Answer posted by Anandam Lokesh
    4 months ago

    A Preprocessor is system software (a computer program that is designed to run on a computer's hardware and application programs). It performs preprocessing of the High-Level Language(HLL).

    The preprocessor doesn't know about the scope rules of C. Preprocessor directives like #define come into effect as soon as they are seen and remain in effect until the end of the file that contains them the program's block structure is irrelevant.

    We can place these preprocessor directives anywhere in our program. Examples of some preprocessor directives are:  #include, #define, #ifndef etc.

    There are 4 main types of preprocessor directives:

      1. File Inclusion

      2. Macros

      3. Conditional Compilation

      4. Other directives

    1. File Inclusion:

      i. Header File or Standard files: These files contains the definition of pre-defined functions like printf(), scanf(), etc. These files must be included for working with these functions. Different functions are declared in different header files.

      Eg: #include < stdio.h>

      ii. User-defined files: When a program becomes very large, it is good practice to divide it into smaller files and include them whenever needed. These types of files are user-defined files.

      Eg: #include “filename”

    2. Macros: Macros are a piece of code in a program that is given some name. Whenever this name is encountered by the compiler the compiler replaces the name with the actual piece of code. The ‘#define’ directive is used to define a macro.

      Eg: #define  LIMIT  5

    Note: There is no semi-colon(‘;’) at the end of the macro definition. Macro definitions do not need a semi-colon to end.

    3. Conditional Compilation: Conditional Compilation directives are a type of directives that help to compile a specific portion of the program or to skip compilation of some specific part of the program based on some conditions. This can be done with the help of two preprocessing commands ‘ ifdef ’ and ‘ endif ’.

      Eg:

      # ifdef macro_name

      statement 1;

      statement 2;

      statement 3;

       . . .

      statement N;

      #endif

      If the macro with the name as ‘macro_name’ is defined then the block of statements will execute normally but if it is not defined, the compiler will simply skip this block of statements.

    4. Other directives:

    #undef is used to destroy a macro that was already created using #define.

    #ifdef returns TRUE if the macro is defined and returns FALSE if the macro is not defined.

    #ifndef returns TRUE if the specified macro is not defined otherwise returns FALSE.

    #if uses the value of specified macro for conditional compilation.

    #else is an alternative for #if.

    #elif is a #else followed by #if in one statement.

    #endif is used to terminate preprocessor conditional macro.

    #error is used to print an error message on stderr.

    #pragma Directive: This directive is a special purpose directive and is used to turn on or off some features. This type of directives are compiler-specific i.e., they vary from compiler to compiler. 

      #pragma startup and #pragma exit: These directives help us to specify the functions that are needed to run before program startup( before the control passes to main()) and just before program exit (just before the control returns from main()).

    Users Joined

    Manish Kumar
    2 days ago
    Shruthi
    2 days ago
    sathvika
    5 days ago
    Ajay Kumar
    5 days ago
    Samarth Kulkarni
    6 days ago
    X
    Explain how a preprocessor works in C?
    X
    Explain how a preprocessor works in C?

    A Preprocessor is system software (a computer program that is designed to run on a computer's hardware and application programs). It performs preprocessing of the High-Level Language(HLL).

    The preprocessor doesn't know about the scope rules of C. Preprocessor directives like #define come into effect as soon as they are seen and remain in effect until the end of the file that contains them the program's block structure is irrelevant.

    We can place these preprocessor directives anywhere in our program. Examples of some preprocessor directives are:  #include, #define, #ifndef etc.

    There are 4 main types of preprocessor directives:

      1. File Inclusion

      2. Macros

      3. Conditional Compilation

      4. Other directives

    1. File Inclusion:

      i. Header File or Standard files: These files contains the definition of pre-defined functions like printf(), scanf(), etc. These files must be included for working with these functions. Different functions are declared in different header files.

      Eg: #include < stdio.h>

      ii. User-defined files: When a program becomes very large, it is good practice to divide it into smaller files and include them whenever needed. These types of files are user-defined files.

      Eg: #include “filename”

    2. Macros: Macros are a piece of code in a program that is given some name. Whenever this name is encountered by the compiler the compiler replaces the name with the actual piece of code. The ‘#define’ directive is used to define a macro.

      Eg: #define  LIMIT  5

    Note: There is no semi-colon(‘;’) at the end of the macro definition. Macro definitions do not need a semi-colon to end.

    3. Conditional Compilation: Conditional Compilation directives are a type of directives that help to compile a specific portion of the program or to skip compilation of some specific part of the program based on some conditions. This can be done with the help of two preprocessing commands ‘ ifdef ’ and ‘ endif ’.

      Eg:

      # ifdef macro_name

      statement 1;

      statement 2;

      statement 3;

       . . .

      statement N;

      #endif

      If the macro with the name as ‘macro_name’ is defined then the block of statements will execute normally but if it is not defined, the compiler will simply skip this block of statements.

    4. Other directives:

    #undef is used to destroy a macro that was already created using #define.

    #ifdef returns TRUE if the macro is defined and returns FALSE if the macro is not defined.

    #ifndef returns TRUE if the specified macro is not defined otherwise returns FALSE.

    #if uses the value of specified macro for conditional compilation.

    #else is an alternative for #if.

    #elif is a #else followed by #if in one statement.

    #endif is used to terminate preprocessor conditional macro.

    #error is used to print an error message on stderr.

    #pragma Directive: This directive is a special purpose directive and is used to turn on or off some features. This type of directives are compiler-specific i.e., they vary from compiler to compiler. 

      #pragma startup and #pragma exit: These directives help us to specify the functions that are needed to run before program startup( before the control passes to main()) and just before program exit (just before the control returns from main()).

    EngineersHub Logo
    x
    Loading...