Search This Blog

Thursday, January 2, 2020

c programming preprocessor directives | codeswithabhi

c programming preprocessor directives c programming language c programming for prime number c programming basics c programming structure c programming

c programming preprocessor directives

In this tutorial, you will learn:

Preprocessor Directives:-

 

Preprocessor directives is a program that processes the code before compilation.
It represents by start with symbol [#] processor directives is a line of code which is terminated by a semicolon(;) 
The source code written by programmer is stored in the file program.c . This file is then processed by preprocessor and and expanded source code file general named program.
Preprocessor program provide processor directive which tell the compiler to process the source code before compilation

Types of preprocessor  directives:-

There are main three preprocessor directives :-

1. File Inclusion.
2. Macro substitution.
3. compilation control.

1. File Inclusion:-

File inclusion means including or using a header file in a program by using include keyword with preprocessor directives 
For e.g:-        #include<header  file>

* Header file or standard file:-

These file contain definition of predefined function like, printf( ); ,scanf(); ,etc.

User defined function:-

When a program became very large, it's good practise to divide it into smallar files and include whenever 
for example:- #include<filename>

 2. Macro substitution:- 

Macro substitution is the shortcut that is always or name which is replaced by a value & string.
Macro substitution creates by using a defined keyword which also begins with its symbol.
Macro substitution holds any kind of value we can not update the value of macro during the program.
For example:-
#incluede<stdio.h>
#include<conio.h>
# define Line 5
void main()
{
int i ;
for(i=1;i<=line;i++)
{
printf("%d",i);
}
getch();
}

OUTPUT:-

1
2
3
4
5

To chech which variable may be update or not:-

#include<stdio.h>
#include<conio.h>
#define x 100
void main()
{
int z=10;
printf("%d",z);
printf("%d",x);
z=z+1;  ---------------------------------[it is possible because it is local variable]
x=x+1;----------------------------------[it is not possible because it is macro]
getch();

3. Compilation Control:-

The conditional directives are a type of directives that helps to compile a specific portion of the program or to skip compilation of some specific part of the program based on some condition.
This can be done with the help of two preprocessing commands ['ifdef'  and  'endif']

Syntax

#ifdef  macro_name
statement 1:
statement 2:
statement 3:
.
.
.
.
.
.
statement n;
#endif

Explaination of some preprocessor diectives :

* #undef

To undefine a macro means to cancel its definition. This is done with the #undef directives.
syntax:  #undef token

* #ifdef

The ifdef preprocessor directives checks if macro is defined by #define . If yes , it excutes the code.

syntax:

#ifdef MACRO
//code
#endif

* #ifndef

The #ifndef preprocessor directives checksif macro isn't defined by #define. If yes, it executes the code.

syntax:-

#ifndef  MACRO
//code
#endif

* #if

The #if preprocessor directivs evalutes the expressiion  or condition if condition of #if is true, it execute the code.

Syntax:

#if expression
//code
#endif

* #else

The #else preprocessor directive evalutes the expression or condition if condition is #if is false.

Syntax:

#if expression
// if code
 #else
//else code
#endif

* #Error

The #error preprocessor directive indicates error. The compiler gives fatal error if #error directive is found.

c programming language

c programming for prime number

c programming basics

c programming structure

c programming tutorial

c programming array

c programming questions for interview

c programming pdf

for c programming example

c programming examples

c programming questions

c programming interview questions

c programming hello world

what c programming

c programming online compiler

c programming functions

c programming string

c programming online

c programming com…

c programming language

c programming for prime number

c programming basics

c programming structure

c programming tutorial

c programming questions for interview

c programming pdf

for c programming example

c programming examples

c programming questions

c programming interview questions

c programming hello world

what c programming

c programming online compiler

c programming functions

c programming string

c programming online

c programming compiler

c programming in vs code

how learn c programming

c programming software

c programming reverse number

c programming for loop

c programming practice

c programming for beginners

c programming examples with output

c programming recursion

Mr. abhijeet dwivedi

Developer

Hey, I'm Abhijeet

2 comments:

If you have any doubt, please let me know