Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
32 views

C Programming Preprocessor and Macros

The preprocessor extends C programming with directives like #include and #define. #include inserts the contents of a header file. #define can define symbolic constants or macros. Macros with arguments allow parameters to be passed, and the token string is replaced wherever the macro name with arguments appears. Predefined macros provide information like the date, time, file name and line number.

Uploaded by

slspa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

C Programming Preprocessor and Macros

The preprocessor extends C programming with directives like #include and #define. #include inserts the contents of a header file. #define can define symbolic constants or macros. Macros with arguments allow parameters to be passed, and the token string is replaced wherever the macro name with arguments appears. Predefined macros provide information like the date, time, file name and line number.

Uploaded by

slspa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

C Programming Preprocessor and Macros

Preprocessor extends the power of C programming language. Line that beg in with # are called preprocessing
directives.

Use of #include
Let us consider very common preprocessing directive as below:
#include <stdio.h>

Here, "stdio.h" is a header file and the preprocessor replace the above line with the contents of header file.

Use of #define
Preprocessing directive #define has two forms. The first form is:
#define identifier token_string
token_string part is optional but, are used almost every time in program.

Example of #define
#define c 299792458 /*speed of light in m/s */

The token string in above line 2299792458 is replaced in every occurance of symbolic constant c.
C Program to find area of a cricle. [Area of circle=r 2]
#include <stdio.h>
#define PI 3.1415
int main(){
int radius;
float area;
printf("Enter the radius: ");
scanf("%d",&radius);
area=PI*radius*radius;
printf("Area=%.2f",area);
return 0;
}

Output
Enter the radius: 3
Area=28.27

Syntactic Sugar
Syntactic sugar is the alteration of programming syntax according to the will of programmer. For example:
#define LT <

Every time the program encounters LT, it will be replaced by <.

Second form of preprocessing directive with #define is:

Macros with argument


Preprocessing directive #define can be used to write macro definitions with parameters as well in the form
below:
#define identifier(identifier 1,.....identifier n) token_string

Again, the token string is optional but, are used in almost every case. Le t us consider an example of macro
definition with argument.
#define area(r) (3.1415*(r)*(r))

Here, the argument passed is r. Every time the program encounters area(argument) , it will be replace
by (3.1415*(argument)*(argument)) . Suppose, we passed (r1+5) as argument then, it expands as below:
area(r1+5) expands to (3.1415*(r1+5)*(r1+5))

C Program to find area of a circle, passing arguments to macros. [Area of circle=r 2 ]


#include <stdio.h>
#define PI 3.1415
#define area(r) (PI*(r)*(r))
int main(){
int radius;
float area;
printf("Enter the radius: ");
scanf("%d",&radius);
area=area(radius);
printf("Area=%.2f",area);
return 0;
}

Predefined Macros in C language


Predefined macro

Value

__DATE__

String containing the current date

__FILE__

String containing the file name

__LINE__

Integer representing the current line number

__STDC__

If follows ANSI standard C, then value is a nonzero


integer

__TIME__

String containing the current date.

How to use predefined Macros?


C Program to find the current time
#include <stdio.h>
int main(){

printf("Current time: %s",__TIME__); //calculate the current time


}

Output
Current time: 19:54:39

You might also like