Chapter 4: Preprocessor and Objects
C Language(Basics & Fundamentals) - Chapter 4

Chapter 4: Preprocessor and Objects


PART 1: Preprocessor


  • A program which processes the source code before it passes through the compiler is known as preprocessor.
  • The commands of the preprocessor are known as preprocessor directives.
  • It is generally placed before the main() for readability.
  • It begins with a # symbol.
  • They are never terminated with a semicolon.

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++.

  1. The source code written by programmers is stored in the file program.c.
  2. This file is then processed by preprocessors and an expanded source code file is generated named program.
  3. This expanded file is compiled by the compiler and an object code file is generated named program .obj.
  4. Finally, the linker links this object code file to the object code of the library functions to generate the executable file program.exe.

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.

  • Preprocessor programs provide preprocessors directives which tell the compiler to preprocess the source code before compiling. All of these preprocessor directives begin with a ‘#’ (hash) symbol.
  • The ‘#’ symbol indicates that, whatever statement starts with #, is going to the preprocessor program, and preprocessor program will execute this statement.
  • Examples of some preprocessor directives are: #include , #define , #ifndef etc. Remember that # symbol only provides a path that it will go to the preprocessor, and command such as include is processed by preprocessor program.
  • We can place these preprocessor directives anywhere in our program but for readability, a preprocessor directive should begin in the first column. It must be the first nonblank character.
  • The C Preprocessor is not a part of the compiler, but is a separate step in the compilation process.
  • In simple terms, a C Preprocessor is just a text substitution tool and it instructs the compiler to do required pre-processing before the actual compilation.

?The C preprocessor provides four separate facilities:

  • Inclusion of header files: These are files of declarations that can be substituted into your program.
  • Macro expansion: You can define macros, which are abbreviations for arbitrary fragments of C code, and then the C preprocessor will replace the macros with their definitions throughout the program.
  • Conditional compilation: Using special preprocessing directives, you can include or exclude parts of the program according to various conditions.
  • Line control: If you use a program to combine or rearrange source files into an intermediate file which is then compiled, you can use line control to inform the compiler of where each source line originally came from.

?

Preprocessor Directives

The preprocessor directives are divided into four different categories which are as follows:?

  1. Macros
  2. File inclusion
  3. Conditional compilation
  4. Miscellaneous directive


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.

  • A macro name is generally written in capital letters.
  • If suitable and relevant names are given macros increase the readability.
  • If a macro is used in any program and we need to make some changes throughout the program we can just change the macro and the changes will be reflected everywhere in the program.

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 .

  • This type of preprocessor directive tells the compiler to include a file in the source code program.
  • There are two types of files which can be included by the user in the program:
  • The content that is included in the filename will be replaced at the point where the directive is written.
  • By using the file inclusive directive, we can include the header files in the programs.
  • Macros, function declarations, declaration of the external variables can all be combined in the header file instead of repeating them in each of the program.
  • The stdio.h header file contains the function declarations and all the information regarding the input and output.

There are two ways of the file inclusion statement:

i) #include “file-name”

ii) #include <file-name>

  • If the first way is used, the file and then the filename in the current working directory and the specified list of directories would be searched.
  • If the second way, is used the file and then the filename in the specified list of directories would be searched.

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:

  • #pragma startup and #pragma exit: These directives helps us to specify the functions that are needed to run??before program startup( before the control passes to main()) and just before program exit (just before the control??returns from main()).

?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.

#else It is an alternative for #if .

#elif It has #else and #if in one statement.

#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:

  1. Characters (char) - Characters (declared, and hereinafter called, char) are chosen from the ASCII set; they occupy the rightmost seven bits of an 8bit byte. It is also possible to interpret chars as signed, 2’s complement 8bit numbers.
  2. Integers (int) – this are represented in 16-bit 2’s complement notation.
  3. Single precision floating point (float) - Single precision floating point (float) quantities have magnitude in the range approximately 10±38 or 0;their precision is 24 bits or about seven decimal digits.
  4. Double-precision floating-point (double) quantities have the same range as floats and a precision of 56 bits or about 17 decimal digits.

?

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 1) Programming environment

CH 2) Syntax and Semantics

CH 3) Libraries & Header files

CH 4) Preprocessor & Objects

CH 5) Commands & Tokens

CH 6) Declaration & Statements

CH 7) Flow control & Functions

CH 8) Errors & Habits


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.

要查看或添加评论,请登录

Parth Dobariya的更多文章

  • Chapter 8: Errors and Habits

    Chapter 8: Errors and Habits

    PART 1: ERRORS Errors are the problems or the faults that occur in the program, which makes the behavior of the program…

  • Chapter 7: Flow control and Functions

    Chapter 7: Flow control and Functions

    PART 1: FLOW CONTROL Flow control statements determine the next statement to execute. In this sense, the statements…

  • Chapter 6: Declaration and Statements

    Chapter 6: Declaration and Statements

    PART 1: DECLARATION In programming, a declaration is a statement describing an identifier, such as the name of a…

  • Chapter 5: Commands and Tokens

    Chapter 5: Commands and Tokens

    PART 1: COMMANDS What are Commands? Commands are orders to computer program to perform particular task. It is used to…

  • Chapter 3: Header files and Libraries

    Chapter 3: Header files and Libraries

    PART 1: LIBRARIES The Standard C Library provides a huge wealth of built-in functions and functionality that is…

  • Chapter 2: Syntax and Semantics

    Chapter 2: Syntax and Semantics

    PART 1: SYNTAX (Form of expressions, statements and program units) What is Syntax for C Language? Any language, be it…

  • Chapter 1: Programming Environment

    Chapter 1: Programming Environment

    Programming Environment: “A Programming Environment is the collection of tools used in the development of software.” In…

  • C Language: Basics & Fundamentals

    C Language: Basics & Fundamentals

    Through this series of articles, I hope to provide helpful information about the fundamentals of the C programming…

    2 条评论