#define and #include in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

All the statements starting with # (hash) symbol are known as preprocessor directives/commands therefore, #define and #include are also known as preprocessor directives. Preprocessor directives are executed before any other command in our program. In a C Program, we generally write all the preprocessor directives outside the main() function at the top of our C program. The #define directive is used to define constants or an expression in our C Program, while #include directive is used to include the content of header files in our C program.

Introduction

There are three major types of preprocessor directives that are used in a C program: macros, file inclusion, conditional compilation.

Macros

It is some constant value or an expression that can be defined using the #define command in our C Program. Examples :

  • Defining a value
  • Defining an expression

file inclusion

It is adding defined as content of a header file into our C Program, and it can be done using the #include command. Examples :

  • Including standard input output header file
  • Including standard library functions header file

conditional compilation

It is running or skipping a piece of code at some macros condition (a constant value or an expression defined using #define), and it can be performed using commands like #ifdef, #endif, #ifndef, #if, #else and #elif in a C Program. Example :

  • printing age if macro is defined, else printing not defined
    Output :

Now, To understand how and why preprocessor directives are executed before compilation, let us look at the process of how the whole compilation process works in a C Program.

Let's suppose we have written a hello.c program to print Hello, World! in the output. The compilation process will generate an executable file, hello.exe from our hello.c program file.

conditional compilation

Compilation Process

It is a process of converting Human Understandable (High Level) Code into Machine Understandable (Low Level) Code. Let us look at the steps involved in the compilation process.

  • Step 1, We have a written C Program file with an extension of .c i.e. hello.c file.
  • Step 2 is preprocessing of header files, all the statements starting with # (hash symbol) are replaced during the compilation process with the help of a pre-processor. It generates an intermediate file with .i file extension i.e. a hello.i file.
  • Step 3 is a compilation of hello.i file, compiler software translates the hello.i file to hello.s file having assembly-level instructions (low-level code).
  • Step 4, assembly-level code instructions are converted into a machine-understandable code (binary/hexadecimal form) by the assembler, and the file generated is known as the object file with an extension of .obj i.e. hello.obj file.
  • Step 5, Linker is used to link the library files with the object file to define the unknown statements. It generates an executable file with .exe extension i.e. a hello.exe file.
  • Next, we can run the hello.exe executable file to get the desired output on our output window.

The below diagram shows all the steps involved in the compilation process.

Compilation Process

Now, let us see the definitions, syntax, and examples of #define and #include.

What is #define in C?

  • #define is a preprocessor directive that is used to define macros in a C program.
  • #define is also known as a macros directive.
  • #define directive is used to declare some constant values or an expression with a name that can be used throughout our C program.
  • Whenever a #define directive is encountered, the defined macros name replaces it with some defined constant value or an expression.

What is #include in C?

  • #include is a preprocessor directive that is used for file inclusion in a C program.
  • #include is also known as a file inclusion directive.
  • #include directive is used to add the content/piece of code from a reserved header file into our code file before the compilation of our C program.
  • These header files include definitions of many pre-defined functions like printf(), scanf(), getch(), etc.

Syntax of #define in C

OR

CNAME : Name of the constant value or the expression. Generally, programmers define it in uppercase letters but it is not necessary like LIMIT, AREA(l,b), etc.

value : It can be any constant value and can be of any data type int, char, float, string etc.

expression: It can be any piece of code or any mathematical expression like (length * breadth), (a * a), etc.

Example Syntax :

Note : #define directive doesn't require a ; (semi-colon) at the end of the statement.

Syntax of #include in C

OR

filename : It is the header file name that is required in our C Program.

Example Syntax :

Examples of #define in C

We will see two examples of #define, first with a constant value and second with an expression.

Area of a circle using #define CNAME value.

We are defining the value of PI to be 3.14 in the below example using the #define directive, we are using the PI value in calculating the area of circle i.e. PI * r * r.

C Program :

Custom Input :

Output :

You can run and check your code here.

Explanation :

  • We have included a standard input output header file using the #include <stdio.h> directive.
  • We have defined the value of PI to be 3.14 using the #define directive.
  • In the main() function, we are using an input float variable radius and an area variable to store the area value.
  • area = PI * radius * radius, in this statement, PI is replaced by 3.14 as we have defined it using the #define command.
  • printf("\nArea of Circle : %0.2f", area); will print the area of circle with precision of 2 decimal places.
  • return 0; will exit the program successfully.

Square of a given number using #define CNAME expression.

We are defining a mathematical expression (a * a) to the cname SQR(a) to calculate the square of a number using the #define directive.

C Program :

Custom Input :

Output:

You can run and check your code here.

Explanation :

  • We have included a standard input output header file using the #include <stdio.h> directive.
  • We have defined an expression SQR(a) to be (a * a) using the #define directive.
  • In the main() function, we are using an input integer variable num.
  • printf("\nSquare of %d : %d", num, SQR(num));, in this statement SQR(num) is replaced by (num * num) and square of num is printed in the output.
  • return 0; will exit the program successfully.

Example of #include in C

In this example, we calculating the sum of two input numbers using the scanf() and printf() functions, functionalities / definition of these functions are included in the program using the #include <stdio.h> directive.

C Program :

Custom Input :

Output :

You can run and check your code here.

Explanation :

  • We have included a standard input output header file using the #include <stdio.h> directive.
  • Now, we can use standard input output functions like printf() for output and scanf() for input.
  • In the main() function, we are using three input integer variables num1, num2 and sum.
  • printf("Enter two numbers to find their sum : "); prints the Enter two numbers to find their sum : in the output.
  • scanf("%d %d", &num1, &num2);, this statement is used get two input integers from the user i.e. num1 and num2.
  • printf("\nSum of %d and %d is : %d", num1, num2, sum); will print sum of two input integer numbers num1 and num2.
  • printf() and scanf() functions are pre-defined in the stdio.h header file and that is why we are able to use them in our program.
  • return 0; will exit the program successfully.

Conclusion

  • #define and #include are preprocessor directives that are written outside the main() function.
  • Preprocessor commands are executed before the compilation of our program.
  • #define is used to declare a constant value or expression with a CNAME that can be used throughout the program.
  • #include is used to include the content of a header file in our C program.