Tuesday, 3 February 2015

C Preprocessor

C Preprocessor
Introduction :
}  The C preprocessor executes before a program is compiled.
}  Some actions it performs are
◦      the inclusion of other files in the file being compiled
◦      definition of symbolic constants and macros
◦      conditional compilation of program code and
◦      conditional execution of preprocessor directives.
}  Preprocessor directives begin with #.
Directive
Description
#define
Substitutes a preprocessor macro
#include
Inserts a particular header from another file
#undef
Undefines a preprocessor macro
#ifdef
Returns true if this macro is defined
#ifndef
Returns true if this macro is not defined
#if
Tests if a compile time condition is true
#else
The alternative for #if
#elif
#else an #if in one statement
#endif
Ends preprocessor conditional
#error
Prints error message on stderr
#pragma
Issues special commands to the compiler, using a standardized method

#include Preprocessor Directive :
}  #include preprocessor directive  causes a copy of a specified file to be included in place of the directive.
}  The two forms of the #include directive are:
–  #include <filename>
#include "filename"
}  The difference between these is the location the preprocessor begins searches for the file to be included.
}  If the file name is enclosed
◦      in quotes, the preprocessor starts searches in the same directory in which the file/program being compiled (and may search other locations, too).
◦      This method is normally used to include programmer-defined headers.
◦      If the file name is enclosed in angle brackets (< and >)—used for standard library headers—the search is performed in an implementation-dependent manner, normally through predesignated compiler and system directories.
}  The #include directive is used to include standard library headers such as stdio.h and stdlib.h and with programs consisting of several source files that are to be compiled together. Ex: #include <stdio.h>
}  A header containing declarations common to the separate program files is often created and included in the file.
◦      Ex:    #include <studentstructures.c>
}  Examples of such declarations are structure and union declarations, enumerations and function prototypes.
#define Preprocessor Directive:
Symbolic Constants :
}  The #define directive creates
§  symbolic constants—constants represented as symbols
§  Symbolic constants enable us to create a name for a constant and use the name throughout the program.
§  macros—operations are defined as symbols.
}  The #define directive format is
–  #define    identifier    replacement-text
}  When this line appears in a file, all subsequent occurrences of identifier that do not appear in string literals will be replaced by replacement-text automatically before the program is compiled.
}  For example,
–  #define   PI   3.14159
–  replaces every occurrence of the identifier PI in the program with the constant 3.14159.
}  If the constant needs to be modified throughout the program, it can be modified once in the #define directive.  System does not allow to modify in the subsequent occurrences.
}  When the program is recompiled, all occurrences of the constant in the program will be modified accordingly.
}  By convention symbolic constants are defined using only uppercase letters and underscores.
Macros :
}  A macro is an identifier defined in a #define preprocessor directive.
}  The macro-identifier is replaced in the program with the replacement-text before the program is compiled.
}  Macros may be defined with or without arguments.
}  A macro without arguments is processed like a symbolic constant.
}  In a macro with arguments, the arguments are substituted in the replacement text, then the macro is expanded—i.e., the replacement-text replaces the identifier and argument list in the program.
}  Consider the following macro definition with one argument for the area of a circle:
}  #define PI 3.14159
}  #define CIRCLE_AREA( x ) ( ( PI ) * ( x ) * ( x ) )
}  Wherever CIRCLE_AREA(x) appears in the file, the value of x is substituted for x in the replacement-text, the symbolic constant  PI is replaced by its value (defined previously) and the macro is expanded in the program.
}  For example, the statement
–  area = CIRCLE_AREA( 4 );
            is expanded to
–  area = ( ( 3.14159 ) * ( 4 ) * ( 4 ) );
            and the value of the expression is evaluated and assigned to variable area.
}  The parentheses around each x in the replacement text force the proper order of evaluation when the macro argument is an expression.
}  Symbolic constants and macros can be discarded by using the #undef preprocessor directive.
}  Directive #undef  “undefines” a symbolic constant or macro name.
}  The scope of a symbolic constant or macro is from its definition until it is undefined with #undef, or until the end of the file.
}  Once undefined, a name can be redefined with #define.
Conditional Compilation:
}  Conditional compilation enables us to control the execution of preprocessor directives and the compilation of program code.
}  Each of the conditional preprocessor directives evaluates a constant integer expression.
}  Cast expressions, sizeof expressions and enumeration constants cannot be evaluated in preprocessor directives.
}  The conditional preprocessor construct is much like the if selection statement.
}  Consider the following preprocessor code:
}  #if !defined(MY_CONSTANT)
   #define MY_CONSTANT 0
#endif
}  These directives determine if MY_CONSTANT is defined.
}  The expression defined( MY_CONSTANT) evaluates to 1 if MY_CONSTANT is defined; 0 otherwise.
}  If the result is 0, !defined(MY_CONSTANT) evaluates to 1 and MY_CONSTANT is defined.
}  Otherwise, the #define directive is skipped.
}  Every #if construct ends with #endif.
}  Directives #ifdef and #ifndef are shorthand for #if defined(name) and #if !defined(name).
}  A multiple-part conditional preprocessor construct may be tested by using the #elif (the equivalent of else if in an if statement) and the #else (the equivalent of else in an if statement) directives.
}  These directives are frequently used to prevent header files from being included multiple times in the same source file.
 #error and #pragma Preprocessor Directives:
}  The #error directive 
–  #error tokens
            prints an implementation-dependent message including the tokens specified in the directive.
}  The tokens are sequences of characters separated by spaces.
}  For example,
–  #error 1 - Out of range error
            contains 6 tokens.
}  When a #error directive is processed on some systems, the tokens in the directive are displayed as an error message, preprocessing stops and the program does not compile.
}  The #pragma directive
–  #pragma tokens
            causes an implementation-defined action.
}  A pragma not recognized by the implementation is ignored.

 # and ## Operators:
}  The # and ## preprocessor operators are available in Standard C.
}  The # operator causes a replacement text token to be converted to a string surrounded by quotes.
}  Consider the following macro definition:
–  #define HELLO(x) printf( "Hello, " #x "\n" );
}  When HELLO(John) appears in a program file, it is expanded to
–  printf( "Hello, " "John" "\n" );
}  The string "John" replaces #x in the replacement text.
}  Strings separated by white space are concatenated during preprocessing, so the preceding statement is equivalent to
–  printf( "Hello, John\n" );
}  The # operator must be used in a macro with arguments because the operand of # refers to an argument of the macro.
}  The ## operator concatenates two tokens.
}  Consider the following macro definition:
–  #define TOKENCONCAT(x, y)  x ## y
}  When TOKENCONCAT appears in the program, its arguments are concatenated and used to replace the macro.
}  For example, TOKENCONCAT(O, K) is replaced by OK in the program.
}  The ## operator must have two operands.
Line Numbers:
}  The #line preprocessor directive causes the subsequent source code lines to be renumbered starting with the specified constant integer value.
}  The directive
–  #line 100
            starts line numbering from 100 beginning with the next source code line.
}  A file name can be included in the #line directive.
Predefined Symbolic Constants :
}  Standard C provides predefined symbolic constants, several of which are shown in Fig.
}  The identifiers for each of the predefined symbolic constants begin and end with two underscores.
}  These identifiers and the defined identifier (used in Section 13.5) cannot be used in #define or #undef directives.
}  Pre defined symbolic constants are :






Bitwise Manipulation

Bitwise Manipulation:
Bit manipulation is the act of algorithmically manipulating bits or other pieces of data shorter than a byte. C language is very efficient in manipulating bits.
Here are following operators to perform bit manipulation:
Bitwise Operators:
C provides six operators for bit manipulation.

Symbol
Operator
&
bitwise AND
|
bitwise inclusive OR
^
bitwise exclusive OR
<< 
left shift
>> 
right shift
~
bitwise NOT (one's complement) (unary)
Bitwise operator works on bits and perform bit by bit operation.
Assume if B = 60; and B = 13; Now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A  = 1100 0011



Bitwise operators supported by C language with Description and Example

Operator
Description
Example
&
Binary AND Operator copies a bit to the result if it exists in both operands.
(A & B) will give 12 which is 0000 1100
|
Binary OR Operator copies a bit if it exists in eather operand.
(A | B) will give 61 which is 0011 1101
^
Binary XOR Operator copies the bit if it is set in one operand but not both.
(A ^ B) will give 49 which is 0011 0001
~
Binary Ones Complement Operator is unary and has the efect of 'flipping' bits.
(~A ) will give -60 which is 1100 0011
<< 
Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.
A << 2 will give 240 which is 1111 0000
>> 
Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.
A >> 2 will give 15 which is 0000 1111

The shift operators perform appropriate shift by operator on the right to the operator on the left. The right operator must be positive. The vacated bits are filled with zero.
For example: x << 2 shifts the bits in x by 2 places to the left.

if x = 00000010 (binary) or 2 (decimal)

then:
x >>= 2 => x = 00000000 or just 0 (decimal)

Also: if x = 00000010 (binary) or 2 (decimal)
then
x <<= 2 => x = 00001000 or 8 (decimal)

Uses of Bitwise Operators
Therefore a shift left is equivalent to a multiplication by 2. Similarly a shift right is equal to division by 2. Shifting is much faster than actual multiplication (*) or division (/) by 2. So if you want fast multiplications or division by 2 use shifts.

Bitwise AND is often used to test whether a particular bit is 1 or 0.
Bitwise OR is often used to set a particular bit to 1.
Bitwise COMPLIMENT is often combined with AND  to turn off a particular bit.
We can also use bits to represent elements of a (small) set. If a bit is 1, then element i is in the set, otherwise it's not.
We can use bitwise AND to implement set intersection, bitwise OR to implement set union.