Preprocessors in C:
As we know, “#define” is a preprocessor command, and there are several commands in the C programming language. So, we must first know about the concept of preprocessors in C programming to know why we use the “#define” command. The C Preprocessor is a distinct step in the compilation process that is not included in the compiler. The C Preprocessor will be referred to as CPP. A hash symbol (#) precedes all preprocessor commands.
The program file contains the source code generated by programmers. This file is then preprocessed, and an enlarged source code file entitled program is created. The compiler compiles this enlarged file and creates an object code file called “program. Obj”. Preprocessor programs have directives that state the compiler to preprocess the source code prior to compiling it. Each one of these preprocessing instructions begins with a ‘#’ (hash) sign. The ‘#’ sign indicates that any statement that begins with # will be sent to the preprocessor program, which will then execute it. #include, #define, #ifndef, and other preprocessor directives are examples. Remember that the # (hash) symbol just specifies a path to the preprocessor, and the preprocessor program handles commands like include. Include, for example, will add more code to your application. These preprocessing directives can be used everywhere in our software. Preprocessor instructions are divided into four categories: inclusion of macros files, compilation with conditions, and other instructions.
A macro is a chunk of code in a program that has a name. The compiler substitutes this name with the authentic code when it recognizes it. The ‘#define’ directive is written to initiate a macro. Arguments can also be sent to macros. Macros with parameters behave in the same way as functions do. When the compiler encounters a macro name, it substitutes the name with the macro’s definition. Semi-colon does not have to be used to end macro definitions (;). So, we can say that macros are an implementation of the “#define” preprocessor command as they are stated with the “#define” command. There are 3 types of macros.
- The object like Macros: A simple identifier that a code snippet will substitute is an object-like macro. It is termed object-like because, in code that utilizes it, it looks like an object. It is common to replace a symbolic name with a numerical/variable representation as constant.
- Chain macros: Chain macros are macros that are included within macros. The parent macro is expanded first in chain macros, followed by the child macro.
- Function like Macro: These macros function in the same way as a function call does. Instead of a function name, it substitutes the whole code. It is required to use a pair of parentheses following the macro name. A function-like macro’s name is only prolonged if and only if it is followed by a pair of parentheses. If we don’t do this, the function pointer will be set to the real function’s address, which will result in a syntax error.
The syntax for the “#define” command in C is written below:
We write the “#define” command for a normal variable and name it accordingly in this expression.
OR
In this expression, we write the “#define” command for a function or a prolonged-expression for one or more variables and name it accordingly.
Now that we know what the roots are of the “#define” preprocessed commands and where it is used, we can move on to the implementation part to have a more grip on the concept of the “#define” command. We will look into some examples of the “#define” command in the C programming language in the Ubuntu 20.04 environment.
The “# define” command in C in Ubuntu 20.04:
Example 1:
In this example, we will define a variable at the start of the program and use it later on. To do this, we must run the terminal from our Ubuntu desktop and type “cd Desktop,” then type “touch” to create a “.c” file with the name and extension of .c. Then go to your desktop and find and open your .c file. Now we will write a code in that file in which we will define a variable.
After hitting the save button, you may shut the file to store it in the directory. Return to the Ubuntu terminal and type “g++” followed by your file name and the “.c” extension to produce the output file. If your code is error-free, this command will generate a file with the extension “.out.” At the command prompt, type “./” followed by your “.out” extension to receive the appropriate output.
As you can see, we defined a variable named “LIMIT” at the start and used it in the for loop later.
Example 2:
In this example, we will define an expression with some arguments in it. So, for that, run the terminal from your Ubuntu desktop and type “cd Desktop,” then type “touch” to create a .c file with the name and extension of .c. Then go to your desktop and find and open your .c file. Now we will write a code in that file in which we will define an expression.
After hitting the save button, you may shut the file to store it in the directory. Return to the Ubuntu terminal and type “g++” followed by your file name and the “.c” extension to produce the output file. If your code is error-free, this command will generate a file with the extension “.out.” At the command prompt, type “./” followed by your “.out” extension to receive the appropriate output.
As you can see, we defined the area variable with its formula and used it for the given values to calculate the area.
Conclusion:
We deliberated over “#define” in the C Programming language in this article. The basis of this concept was the C preprocessor in C programming, also known as CPP; we discussed the different preprocessors in the C programming language and how they affect the life of a C programmer in this article. Then the predecessor of the “#define”, the macros were also explained along with their types. In the end, we also implemented some examples in the Ubuntu 20.04 environment of the “#define” command to have a clearer understanding of this concept.