What is preprocessor directives? Discuss # define directive with example.

This answer is restricted. Please login to view the answer of this question.

Login Now

Preprocessor directives are lines included in a program that begin with the character #, which make them different from a typical source code text. They are invoked by the compiler to process some programs before compilation. Preprocessor directives change the text of the source code and the result is a new source code without these directives.

A preprocessor directive is usually placed in the top of the source code in a separate line beginning with the character “#”, followed by directive name and an optional white space before and after it. Because a comment on the
same line of declaration of the preprocessor directive has to be used and cannot scroll through the following line, delimited comments cannot be used. A preprocessor directive statement must not end with a semicolon ().Preprocessor directives can be defined in source code or in the common line as argument during compilation.

In the C Programming Language, the #define directive allows the definition of macros within your source code. These macro definitions allow constant values to be declared for use throughout your code.

Macro definitions are not variables and cannot be changed by your program code like variables. You generally use this syntax when creating constants that represent numbers, strings or expressions.

Syntax:
The syntax for creating a constant using #define in the C language is:

#define CNAME value
OR
#define CNAME(expression)

Where,

  • CNAME: The name of the constant. Most C programmers define their constant names in uppercase, but it is not a requirement of the C Language.
  • Value: The value of the constant.
  • Expression: Expression whose value is assigned to the constant. The expression must be enclosed in parentheses if it contains operators.

Example:

#include <stdio.h>

#define NAME "Aaray"
#define AGE 10

int main(){

    printf("%s is over %d years old. \n", NAME, AGE);
    return 0;

}

 

If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .