Wednesday - March 13, 2024
#Computer Science #Blog #C Program #C, C++ and C#

What is Preprocessor Directive in C

Preprocessor directive

Q) What is a C preprocessor? What are the different types of preprocessor directives?
Preprocessor

  • Some preprocessor remains a program that processes our source program before this is passed into the compiler.
  • All preprocessor directives begin with a # symbol. For e.g., #include< stdio.h>
what is preprocessor directive in c, preprocessor directives in c, types of preprocessor directives in c, preprocessor directives in c pdf, preprocessor directives are executed during, preprocessor directives are executed when, preprocessor directives are used for, preprocessor directives are used for mcq, preprocessor directives execute, preprocessor directives in c pdf, compiler directives in verilog, preprocessor directives are executed when, c# preprocessor directives best practices, types of preprocessor directives in c, when do preprocessor directives execute, preprocessor directives are executed mcq, c++ macro function, preprocessor in compiler design, preprocessor directives in c ppt, c++ define constant, c++ if,

Type of preprocessor directive

  1. Macro expansion
  2. File inclusion
  3. Conditional compilation

Macro expansion

  • The directive written using # define is called a macro. Macro substitution is the process where an identifier in a program is replaced by a predefined string.
  • The processor replaces every occurrence of an identifier in the source code with a string.

Simple Macro

Syntax;

# define identifier string

E.g 

# include PI 3.14

Macro with arguments 

  • The macro that accepts arguments is known as a macro with arguments.

Syntax:

   # define identifier (a1, a2, …, aN) string

E.g

# include cube (X) (x*x*x)

File inclusion

 The directive # include<  filename> is used to include the contents of the file to the source program.

Syntax:

  # include <FileName.h>   OR #include “FileName.h”

E.g 

#include<math.h>

  • This directive causes the contents of file math.h to be included in our program so that all functions of math.h can be used in our program.

Conditional compilation

  • A conditional compilation directive causes the preprocessor to conditionally suppress the compilation of portions of source code. These are:
  1. #if
  2. #ifdef
  3. #endif
  4. #ifndef
  5. #elif
  6. #else

E.g

void main()

{

#if(conditions)

statement 1;

#else

statement2;

# endif

}

Q) What is the difference between #include <filename>and #include “filename”?

  • There exist two ways to write a #include statement. These are:

#include “filename”

#include<filename>

#include “filename.c”

  • This command would loof for the file filename.c in the current directory as well as the specified list of directories as mentioned in the include search path that might have been set up.

#include<filename.c>

  • This command would look for the file filename.c in the specified list of directories only.
What is Preprocessor Directive in C

What is Operators in C

.