Learn C Language 

Learn C Programming Language Step By Step. It’s easy to learn c programming language with real-life examples and hands-on. We are trying our best to deliver quality content.

PreProcessors In C 

[adinserter name="Block 7"]

« Previous Page                                                                                                                     Next Page »

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. We’ll refer to the C Preprocessor as CPP.

All preprocessor commands begin with a hash symbol (#). It must be the first nonblank character, and for readability, a preprocessor directive should begin in the first column. The following section lists down all the important preprocessor directives −

Sr.No. Directive & Description
1

#define

Substitutes a preprocessor macro.

2

#include

Inserts a particular header from another file.

3

#undef

Undefines a preprocessor macro.

4

#ifdef

Returns true if this macro is defined.

5

#ifndef

Returns true if this macro is not defined.

6

#if

Tests if a compile time condition is true.

7

#else

The alternative for #if.

8

#elif

#else and #if in one statement.

9

#endif

Ends preprocessor conditional.

10

#error

Prints error message on stderr.

11

#pragma

Issues special commands to the compiler, using a standardized method.

Preprocessors Examples

Analyze the following examples to understand various directives.

[et_pb_dmb_code_snippet code=”I2RlZmluZSBNQVhfQVJSQVlfTEVOR1RIIDIwCg==” _builder_version=”4.0.9″]I2RlZmluZSBNQVhfQVJSQVlfTEVOR1RIIDIwCg==[/et_pb_dmb_code_snippet]

This directive tells the CPP to replace instances of MAX_ARRAY_LENGTH with 20. Use #define for constants to increase readability.

[et_pb_dmb_code_snippet code=”I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlICJteWhlYWRlci5oIg==” _builder_version=”4.0.9″]I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlICJteWhlYWRlci5oIg==[/et_pb_dmb_code_snippet]

These directives tell the CPP to get stdio.h from System Libraries and add the text to the current source file. The next line tells CPP to get myheader.h from the local directory and add the content to the current source file.

[et_pb_dmb_code_snippet code=”I3VuZGVmICBGSUxFX1NJWkUKI2RlZmluZSBGSUxFX1NJWkUgNDI=” _builder_version=”4.0.9″]I3VuZGVmICBGSUxFX1NJWkUKI2RlZmluZSBGSUxFX1NJWkUgNDI=[/et_pb_dmb_code_snippet]

It tells the CPP to undefine existing FILE_SIZE and define it as 42.

[et_pb_dmb_code_snippet code=”I2lmbmRlZiBNRVNTQUdFCiAgICNkZWZpbmUgTUVTU0FHRSAiWW91IHdpc2ghIgojZW5kaWY=” _builder_version=”4.0.9″]I2lmbmRlZiBNRVNTQUdFCiAgICNkZWZpbmUgTUVTU0FHRSAiWW91IHdpc2ghIgojZW5kaWY=[/et_pb_dmb_code_snippet]

It tells the CPP to define MESSAGE only if MESSAGE isn’t already defined.

[et_pb_dmb_code_snippet code=”I2lmZGVmIERFQlVHCiAgIC8qIFlvdXIgZGVidWdnaW5nIHN0YXRlbWVudHMgaGVyZSAqLwojZW5kaWY=” _builder_version=”4.0.9″]I2lmZGVmIERFQlVHCiAgIC8qIFlvdXIgZGVidWdnaW5nIHN0YXRlbWVudHMgaGVyZSAqLwojZW5kaWY=[/et_pb_dmb_code_snippet]

It tells the CPP to define MESSAGE only if MESSAGE isn’t already defined.

It tells the CPP to process the statements enclosed if DEBUG is defined. This is useful if you pass the -DDEBUG flag to the gcc compiler at the time of compilation. This will define DEBUG, so you can turn debugging on and off on the fly during compilation.

Predefined Macros

ANSI C defines a number of macros. Although each one is available for use in programming, the predefined macros should not be directly modified.

Sr.No. Macro & Description
1

__DATE__

The current date as a character literal in “MMM DD YYYY” format.

2

__TIME__

The current time as a character literal in “HH:MM:SS” format.

3

__FILE__

This contains the current filename as a string literal.

4

__LINE__

This contains the current line number as a decimal constant.

5

__STDC__

Defined as 1 when the compiler complies with the ANSI standard.

[et_pb_dmb_code_snippet title=”EXAMPLE” code=”I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbigpIHsKCiAgIHByaW50ZigiRmlsZSA6JXNcbiIsIF9fRklMRV9fICk7CiAgIHByaW50ZigiRGF0ZSA6JXNcbiIsIF9fREFURV9fICk7CiAgIHByaW50ZigiVGltZSA6JXNcbiIsIF9fVElNRV9fICk7CiAgIHByaW50ZigiTGluZSA6JWRcbiIsIF9fTElORV9fICk7CiAgIHByaW50ZigiQU5TSSA6JWRcbiIsIF9fU1REQ19fICk7Cgp9″ _builder_version=”4.0.9″]I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbigpIHsKCiAgIHByaW50ZigiRmlsZSA6JXNcbiIsIF9fRklMRV9fICk7CiAgIHByaW50ZigiRGF0ZSA6JXNcbiIsIF9fREFURV9fICk7CiAgIHByaW50ZigiVGltZSA6JXNcbiIsIF9fVElNRV9fICk7CiAgIHByaW50ZigiTGluZSA6JWRcbiIsIF9fTElORV9fICk7CiAgIHByaW50ZigiQU5TSSA6JWRcbiIsIF9fU1REQ19fICk7Cgp9[/et_pb_dmb_code_snippet]

When the above code in a file test.c is compiled and executed, it produces the following result 

[et_pb_dmb_code_snippet code=”RmlsZSA6dGVzdC5jCkRhdGUgOkp1biAyIDIwMTIKVGltZSA6MDM6MzY6MjQKTGluZSA6OApBTlNJIDox” _builder_version=”4.0.9″]RmlsZSA6dGVzdC5jCkRhdGUgOkp1biAyIDIwMTIKVGltZSA6MDM6MzY6MjQKTGluZSA6OApBTlNJIDox[/et_pb_dmb_code_snippet]

Preprocessor Operators

The C preprocessor offers the following operators to help create macros −

The Macro Continuation (\) Operator

A macro is normally confined to a single line. The macro continuation operator (\) is used to continue a macro that is too long for a single line. For example 

[et_pb_dmb_code_snippet code=”I2RlZmluZSAgbWVzc2FnZV9mb3IoYSwgYikgIFwKICAgcHJpbnRmKCNhICIgYW5kICIgI2IgIjogSG93IEFyZSBZb3U/XG4iKQ==” _builder_version=”4.0.9″]I2RlZmluZSAgbWVzc2FnZV9mb3IoYSwgYikgIFwKICAgcHJpbnRmKCNhICIgYW5kICIgI2IgIjogSG93IEFyZSBZb3U/XG4iKQ==[/et_pb_dmb_code_snippet]

The Stringize (#) Operator

The stringize or number-sign operator ( ‘#’ ), when used within a macro definition, converts a macro parameter into a string constant. This operator may be used only in a macro having a specified argument or parameter list. For example

[et_pb_dmb_code_snippet code=”I2luY2x1ZGUgPHN0ZGlvLmg+CgojZGVmaW5lICBtZXNzYWdlX2ZvcihhLCBiKSAgXAogICBwcmludGYoI2EgIiBhbmQgIiAjYiAiOiBIb3cgQXJlIFlvdT9cbiIpCgppbnQgbWFpbih2b2lkKSB7CiAgIG1lc3NhZ2VfZm9yKEpvaG4sIEtpYSk7CiAgIHJldHVybiAwOwp9″ _builder_version=”4.0.9″]I2luY2x1ZGUgPHN0ZGlvLmg+CgojZGVmaW5lICBtZXNzYWdlX2ZvcihhLCBiKSAgXAogICBwcmludGYoI2EgIiBhbmQgIiAjYiAiOiBIb3cgQXJlIFlvdT9cbiIpCgppbnQgbWFpbih2b2lkKSB7CiAgIG1lc3NhZ2VfZm9yKEpvaG4sIEtpYSk7CiAgIHJldHVybiAwOwp9[/et_pb_dmb_code_snippet]

When the above code is compiled and executed, it produces the following result 

[et_pb_dmb_code_snippet code=”Sm9obiBhbmQgS2lhOiBIb3cgQXJlIFlvdT8K” _builder_version=”4.0.9″]Sm9obiBhbmQgS2lhOiBIb3cgQXJlIFlvdT8K[/et_pb_dmb_code_snippet]

The Token Pasting (##) Operator

The token-pasting operator (##) within a macro definition combines two arguments. It permits two separate tokens in the macro definition to be joined into a single token. For example

[et_pb_dmb_code_snippet code=”I2luY2x1ZGUgPHN0ZGlvLmg+CgojZGVmaW5lIHRva2VucGFzdGVyKG4pIHByaW50ZiAoInRva2VuIiAjbiAiID0gJWQiLCB0b2tlbiMjbikKCmludCBtYWluKHZvaWQpIHsKICAgaW50IHRva2VuMzQgPSA0MDsKICAgdG9rZW5wYXN0ZXIoMzQpOwogICByZXR1cm4gMDsKfQ==” _builder_version=”4.0.9″]I2luY2x1ZGUgPHN0ZGlvLmg+CgojZGVmaW5lIHRva2VucGFzdGVyKG4pIHByaW50ZiAoInRva2VuIiAjbiAiID0gJWQiLCB0b2tlbiMjbikKCmludCBtYWluKHZvaWQpIHsKICAgaW50IHRva2VuMzQgPSA0MDsKICAgdG9rZW5wYXN0ZXIoMzQpOwogICByZXR1cm4gMDsKfQ==[/et_pb_dmb_code_snippet]

When the above code is compiled and executed, it produces the following result 

[et_pb_dmb_code_snippet code=”dG9rZW4zNCA9IDQwCg==” _builder_version=”4.0.9″]dG9rZW4zNCA9IDQwCg==[/et_pb_dmb_code_snippet]

It happened so because this example results in the following actual output from the preprocessor 

[et_pb_dmb_code_snippet code=”cHJpbnRmICgidG9rZW4zNCA9ICVkIiwgdG9rZW4zNCk7Cg==” _builder_version=”4.0.9″]cHJpbnRmICgidG9rZW4zNCA9ICVkIiwgdG9rZW4zNCk7Cg==[/et_pb_dmb_code_snippet]

[adinserter block=”3″]

This example shows the concatenation of token##n into token34 and here we have used both stringize and token-pasting.

The Defined() Operator

The preprocessor defined operator is used in constant expressions to determine if an identifier is defined using #define. If the specified identifier is defined, the value is true (non-zero). If the symbol is not defined, the value is false (zero). The defined operator is specified as follows

[et_pb_dmb_code_snippet code=”I2luY2x1ZGUgPHN0ZGlvLmg+CgojaWYgIWRlZmluZWQgKE1FU1NBR0UpCiAgICNkZWZpbmUgTUVTU0FHRSAiWW91IHdpc2ghIgojZW5kaWYKCmludCBtYWluKHZvaWQpIHsKICAgcHJpbnRmKCJIZXJlIGlzIHRoZSBtZXNzYWdlOiAlc1xuIiwgTUVTU0FHRSk7ICAKICAgcmV0dXJuIDA7Cn0=” _builder_version=”4.0.9″]I2luY2x1ZGUgPHN0ZGlvLmg+CgojaWYgIWRlZmluZWQgKE1FU1NBR0UpCiAgICNkZWZpbmUgTUVTU0FHRSAiWW91IHdpc2ghIgojZW5kaWYKCmludCBtYWluKHZvaWQpIHsKICAgcHJpbnRmKCJIZXJlIGlzIHRoZSBtZXNzYWdlOiAlc1xuIiwgTUVTU0FHRSk7ICAKICAgcmV0dXJuIDA7Cn0=[/et_pb_dmb_code_snippet]

When the above code is compiled and executed, it produces the following result 

[et_pb_dmb_code_snippet code=”SGVyZSBpcyB0aGUgbWVzc2FnZTogWW91IHdpc2ghCg==” _builder_version=”4.0.9″]SGVyZSBpcyB0aGUgbWVzc2FnZTogWW91IHdpc2ghCg==[/et_pb_dmb_code_snippet]

Parameterized Macros

One of the powerful functions of the CPP is the ability to simulate functions using parameterized macros. For example, we might have some code to square a number as follows

[et_pb_dmb_code_snippet code=”aW50IHNxdWFyZShpbnQgeCkgewogICByZXR1cm4geCAqIHg7Cn0=” _builder_version=”4.0.9″]aW50IHNxdWFyZShpbnQgeCkgewogICByZXR1cm4geCAqIHg7Cn0=[/et_pb_dmb_code_snippet]

We can rewrite above the code using a macro as follows 

[et_pb_dmb_code_snippet code=”I2RlZmluZSBzcXVhcmUoeCkgKCh4KSAqICh4KSkK” _builder_version=”4.0.9″]I2RlZmluZSBzcXVhcmUoeCkgKCh4KSAqICh4KSkK[/et_pb_dmb_code_snippet]

Macros with arguments must be defined using the #define directive before they can be used. The argument list is enclosed in parentheses and must immediately follow the macro name. Spaces are not allowed between the macro name and open parenthesis. For example

[et_pb_dmb_code_snippet code=”I2luY2x1ZGUgPHN0ZGlvLmg+CgojZGVmaW5lIE1BWCh4LHkpICgoeCkgPiAoeSkgPyAoeCkgOiAoeSkpCgppbnQgbWFpbih2b2lkKSB7CiAgIHByaW50ZigiTWF4IGJldHdlZW4gMjAgYW5kIDEwIGlzICVkXG4iLCBNQVgoMTAsIDIwKSk7ICAKICAgcmV0dXJuIDA7Cn0=” _builder_version=”4.0.9″]I2luY2x1ZGUgPHN0ZGlvLmg+CgojZGVmaW5lIE1BWCh4LHkpICgoeCkgPiAoeSkgPyAoeCkgOiAoeSkpCgppbnQgbWFpbih2b2lkKSB7CiAgIHByaW50ZigiTWF4IGJldHdlZW4gMjAgYW5kIDEwIGlzICVkXG4iLCBNQVgoMTAsIDIwKSk7ICAKICAgcmV0dXJuIDA7Cn0=[/et_pb_dmb_code_snippet]

When the above code is compiled and executed, it produces the following result

[et_pb_dmb_code_snippet code=”TWF4IGJldHdlZW4gMjAgYW5kIDEwIGlzIDIwCg==” _builder_version=”4.0.9″]TWF4IGJldHdlZW4gMjAgYW5kIDEwIGlzIDIwCg==[/et_pb_dmb_code_snippet]

[adinserter name=”Block 7″]

Top Book Suggestion

The two books on C Programming Language that I Personally Recommend. I really loved reading these books. These books are listed for quality content, easy steps, and affordable price. You can get it from Amazon and Flipkart. 

 

Check The Price On Amazon:

1. C Programming Beginner’s Guide

2. Head First C: A Brain-Friendly Guide

Check The Price On Flipkart:

1. Introduction to C Programming 

2. Head First C: A Brain-Friendly Guide

« Previous Page                                                                                                                     Next Page »

[adinserter block=”6″]

Buy Premium Courses At Lowest Price. Become A Certified Developer Today!

Best Books Selected by Our Experts

Buy Premium Projects At Lowest Price

Ebooks Written by Our Experts

CoursePot.Com

Buy Premium Courses At Lowest Price. Grab Golden Opportunity To Become A Certified Developer Today!

Useful Links

[adinserter block=”6″]
[adinserter block=”6″]
[adinserter block=”6″]
[adinserter block=”6″]

Pin It on Pinterest

Share This