Chapter 4: Preprocessor and Objects
PART 1: Preprocessor
As the name suggests Preprocessors are programs that process our source code before compilation. There are a number of steps involved between writing a program and executing a program in C / C++.
The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation. It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs.
?The C preprocessor provides four separate facilities:
?
Preprocessor Directives
The preprocessor directives are divided into four different categories which are as follows:?
1.?Macros:
Macros are a piece of code in a program which is given some name. Whenever this name is encountered by the compiler the compiler replaces the name with the actual piece of code. The ‘#define’ directive is used to define a macro. Macros with arguments: We can also pass arguments to macros. Macros defined with arguments works similarly as functions.
There are two types of macros - One which takes the argument & another which does not take any argument.
Values are passed so that we can use the same macro for a wide range of values.
Syntax:
# define name replacement text
Where, name – it is known as the micro template. Replacement text – it is known as the macro expansion.
Example: Simple macro
# define LOWER 30
void main()
{
????int i;
????for (i=1;i<=LOWER; i++)
????{
?????????printf("\n%d", i);
????}
}?
Some of the predefined macros which are readily available are as follows:
___LINE___ It contains a current line number as a decimal constant.
___FILE___ It contains the current filename as a string literal.
___DATE___ It shows the current date as a character literal in the “MMM DD YYYY” format.
___TIME___ It shows the current time as a character literal in “HH:MM:SS” format.
___STDC___ It is defined as 1 when the compiler complies with the ANSI standard.
___TIMESTAMP___ It is a sing literal in the form of “DDD MM??YYYY Date HH:MM:SS”. It is used to specify the date and time of the last modification of the current source file.
2.?File Inclusion:
The file inclusion uses the #include .
There are two ways of the file inclusion statement:
i) #include “file-name”
ii) #include <file-name>
Header File or Standard files: These files contains definition of pre-defined functions like printf(), scanf() etc. These files must be included for working with these functions. Different function are declared in different header files.
For example - standard I/O functions are in ‘iostream’ file whereas functions which perform string operations are in ‘string’ file.
Syntax:
#include< file_name >
where file_name is the name of file to be included. The ‘<‘ and ‘>’ brackets tells the compiler to look for the file in standard directory.
User defined files: When a program becomes very large, it is good practice to divide it into smaller files and include whenever needed. These types of files are user defined files. These files can be included as:
#include"filename"
?
3.?Conditional Compilation: ?
The conditional compilation is used when we want certain lines of code to be compiled or not. It uses directives like #if , #elif , #else , #endif
Conditional Compilation directives are type of directives which helps to compile a specific portion of the program or to skip compilation of some specific part of the program based on some conditions. This can be done with the help of two preprocessing commands ‘ifdef‘ and ‘endif‘.
Syntax:
?# ifdef macro_name
??? statement1;
??? statement2;
??? statement3;
??? .
??? .
??? .
??? statementN;
#endif
If the macro with name as ‘macroname‘ is defined then the block of statements will execute normally but if it is not defined, the compiler will simply skip this block of statements.
?
4.?Other directive:
There are some directives which do not fall in any of the above mentioned categories. Apart from the above directives there are two more directives which are not commonly used.
There are two directives:
i) #undef Directive: This directive is used in relation to the #define directive. It is used to undefine a defined macro. The #undef directive is used to undefine an existing macro. This directive works as:
# undef LIMIT
Using this statement will undefine the existing macro LIMIT. After this statement every “#ifdef LIMIT” statement will evaluate to false.
ii) #pragma Directive: It is a specialized and rarely used directive. They are used for turning on and off certain features. This directive is a special purpose directive and is used to turn on or off some features. This type of directives are compiler-specific, i.e., they vary from compiler to compiler. Some of the #pragma directives are discussed below:
?Some commonly used preprocessor in c are as follow:
#define It substitutes a preprocessor macro.
#include It inserts a particular header file from another file.
#undef A preprocessor macro is undefined.
#ifdef It returns true if the macro is defined.
#ifndef It returns true if the macro is not defined.
#if It tests if the compile time condition is true.
#endif The conditional preprocessor is ended.
#error It prints the error message on stderr.
#pragma It issues special commands to the compiler by using a standardized method.
PART 2: OBJECTS & Expressions
An object is a manipulate-able region of storage; an lvalue is an expression referring to an object.
There are two kinds of expressions in C ?
L-value ? Expressions that refer to a memory location are called "lvalue" expressions. An lvalue may appear as either the left-hand or right-hand side of an assignment.
R-value ? The term rvalue refers to a data value that is stored at some address in memory. An rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right-hand side but not on the left-hand side of an assignment.
An obvious example of an 1value expression is an identifier. There are operators which yield lvalues: for example, if E is an expression of pointer type, then *E is an lvalue expression referring to the object to which E points. The name ‘‘lvalue’’ comes from the assignment expression ‘‘E1 = E2’’ in which the left operand E1 must be an lvalue expression.
The discussion of each operator below indicates whether it expects lvalue operands and whether it yields an lvalue.
?C supports four fundamental types of objects:
?
Besides the four fundamental types there is a conceptually infinite class of derived types constructed from the fundamental types in the following ways:
arrays of objects of most types;
functions which return objects of a given type;
pointers to objects of a given type;
structures containing objects of various types.
In general these methods of constructing objects can be applied recursively.
List of articles from the series:
CH 4) Preprocessor & Objects
References:?Wikipedia, CMan(Bell Labs), GeeksforGeeks, guru99 and various other blogs, videos and sites on available internet.
If you find any errors or disinformation in this article, then please inform me.