21⟩ Where define directive used?
► Defining a constant
► Defining a statement
► Defining a mathematical expression
For example
► #define PI 3.141593
► #define TRUE 1
► #define floatingpointno float
“C Preprocessor frequently Asked Questions in various C preprocessor job Interviews by interviewer. The set of questions here ensures that you offer a perfect answer posed to you. So get preparation for your new job hunting”
► Defining a constant
► Defining a statement
► Defining a mathematical expression
For example
► #define PI 3.141593
► #define TRUE 1
► #define floatingpointno float
► #define and #undef - Used for defining and undefining MACROS.
► #error - Used for Debugging
► #include - Used for combining source code files
► #if, #else, #elseif, and #endif - Used for conditional compilation similar to if - else statment.
► #ifdef and #ifndef - Conditional Compilation on basis of #define and #undef
► #line - Controls the program line and file macros.
► #pragma - Used for giving compiler instruction. Highly specific to the compiler being used.
► # and ## - Operators used for stringize and concating operation respectively.
The include directive is used to include files like as we include header files in the beginning of the program using #include directive like
► #include
► #include
# is called stringize opertor and turns the argument it precede into a quoted string. Use of # is shown in the C Source code given below and should be properly studied.
The preprocessor will include whatever file you specify in your #include statement. Therefore, if you have the line
#include
in your program, the file macros.inc will be included in your precompiled program. It is, however, unusual programming practice to put any file that does not have a .h or .hpp extension in an #include statement. You should always put a .h extension on any of your C files you are going to include. This method makes it easier for you and others to identify which files are being used for preprocessing purposes.
It is used to assign names to different constants or statements which are to be used repeatedly in a program. These defined values or statement can be used by main or in the user defined functions as well.
In modular programming, using functions is advisable when a certain code is repeated several times in a program. However, everytime a function is called the control gets transferred to that function and then back to the calling function. This consumes a lot of execution time. One way to save this time is by using macros. Macros substitute a function call by the definition of that function. This saves execution time to a great extent.
The #define directive can be used to define types, such as:
#define INT32 long int /* 32 bit signed integer type */
The typedef clause can be used in a similar manner.
typedef long int int32; /* 32 bit signed integer */
The typedef is preferred over the #define because is better integrated into the C language, and it can create more kinds of variable types than a mere define.
## is called the pasting opertor which is used to concates two tokens. Use of ## is shown in the source code.
Functions allow for modular programming. You must remember that all parameters passed into function in C are passed by value!
A macro is a preprocessor directive that provides a mechanism for token replacement in your source code. Macros are created by using the #define statement. Here is an example of a macro:
#define VERSION_STAMP "1.02"
General form of #line preprocessor is #line number "filename"
Here the file name is optional. Filename string replaces the string value of _ _FILE_ _ while the number changes the value of _ _LINE_ _.
The major use of #line is in debugging and rare programming situation.
Following C Source code shows the #line preprocessor in action -
#include
int main ()
{
printf ("n%d", __LINE__); //Prints 6
#line 100;
printf ("n%d",__LINE__); // Prints 101
printf ("n%d", __FILE__);// Prints original source file name
#line 103 "Super C"
printf ("n%d", __FILE__); //Prints Super C
return 0;
}