Learn C : What are Preprocessor Directive , Introduction , Examples ,Interview Questions and Many More.

Learn c

C Preprocessor Directives :

The C Preprocessor is a micro processor which is used in the pre-compilation process to check the program before the actual compilation can be done.
It is called microprocessor because it allows us to add macros in our program.
Each preprocessor directive can start with # symbol.
directive graph
Use of preprocessor Directives :                                
It makes the developer to read the program easily.                 
It makes modification of program easy.
It makes the program architecture independent.                 







Types of Preprocessor Directives:

type of preprocessor directives


File Inclusive Directive:
The files which are declared inside the Header Files can be used if once declared in the program.

Example: To print the sum of two numbers.

example of directives


Macro Substitution directive:
This directive can be used to denote constant values, the constant values can be of any data type.
The #define is a macro.

Example: Print the largest of the two numbers : The macro works internally as ternary operator.

macro substitution directives



Conditional Directive:
In this directive set of statements can be included or excluded on the basis of conditions in the main program.

Example:    ➣ Below is the example of  if, else, endif.
conditional directives


             ifdef, else, endif
conditional directives example


                        if, elif, else, endif : It is similar to if-else ladder statement.
else ladder statements



                             ➣ indef : It is used to check whether the macro name is previously defined or not.
indef




Undefine Symbol Directive:
Undefines a symbol specified by <identifier> which was previously defined with a #define directive.


  • #undef Directive is used to undefine any Macro Symbol.
  • #undef can undefine only “User Defined” Macro’s.
  • #undef cannot undefine “Global Macro Identifiers“.
  • #undef is used where we have to redefine any Macro Identifier.
    Example:  # undef

undifine directives


Miscellaneous Directive:
                 ➣ # Line : It is used to overwrite the default value of pre-defined macro- names __LINE__ and __FILE__. By default __LINE__ displays the current line number and __FILE__ displays the current filename.

            Example 1: Default value of #Line
directives

                           Example 2: Overwrite the default value of #Line
 Miscellaneous Directive example

     But note that the printf line must be in the the same line of void main( ) for the correct output    otherwise the line number output will be different from your macro definition as shown below:
 Miscellaneous Directive exam[ple





                       ➣ # error : It is used to force the compile to stop compiling the source code. In other words we                                                can manually force the compiler to stop compiling and give fatal error.
 Miscellaneous Directive exam[ple











































                        ➣ #pragma : #pragma once is used to include the header file only once.




C Preprocessor Interview Questions:


  1. What is the difference between #include "filename" vs #include <filename>?
  2. What is the use of #define?
  3. Can a #define use a string character as replacement text?
  4. Can you give an example of #define?
  5. Can you give an example of #define with a Macro?
  6. What are Preprocessor Directives?
  7. How can you avoid including a header more than once?
  8. Can a file other than a .h file be included with #include?
    The preprocessor will include whatever file you specify in your #include statement. Therefore, if you have the line
    #include <macros.inc>
    in your program, the file macros.inc will be included in your precompiled program. It is, however, unusual programming practice to put any file that does not have a .h or .hpp extension in an #include statement. You should always put a .h extension on any of your C files you are going to include. This method makes it easier for you and others to identify which files are being used for preprocessing purposes.
        Previous                                                                                                                            Next


For any query related to the content mail to devkumar082001@gmail.com

No comments:

Powered by Blogger.