#define in C

For Complete YouTube Video: Click Here

In this class, we will understand #define in C.

We have already discussed defining constat’s using const.

The other way to define a constant is by using the #define preprocessing directive.

Table of Contents

#define in C

Example

In our previous classes, we have discussed #include preprocessing directive.

Using #include, we instruct the compiler to add the header files.

Similarly, by using the #define, we define the constants in the program.

The image below is the program that uses #define.

#define in C Example
#define in C Example

In the above program, we defined two constants by ROW and COLS with values 3 and 4, respectively.

The program in the example is used to scan the values in a two-dimensional array.

Usually, it is good practice to name the constants in uppercase.

Because with that, we can differentiate the variable and constants.

The use of defining constants is when we want to change the size of the variables, it is sufficient to change the values in the #define statement.

No need to change all the places where the ROWS and COLS appear in the program.