C programming
Preprocessor
The C preprocessors is a tool which filters your source code before it is compiled.The preprocessor allows constant to be named using the #define notation.It particularly uses full for selection machine dependent pieces of code for different computer types, allowing a single program to be compiled and run on several computers. The main advantage of the preprocessor is easier to read and modify and develop.
Preprocessor is given the ability to
the inclusion of a header file
the inclusion of macros expansions
conditional compilation and line control.
Type of preprocessor
#define and #undef is used to define and undefining the macros
#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 Use 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.
1. #define directivesare used in
Defining a constant (#define PI 3.141)
Defining a mathematical expression
Defining a statement
Defining a type (#define INT32 long int)
Example
#define pi 3.14
#define true 1
#define floatingpointno float.
2. #error Use of this preprocessor is in debugging whenever this preprocessor is
encountered during the compilation the compiler will stop the compilation and it will display
the error message with preprocessor compilation output results in the window.
General form of #error is
This example source code display the use of #error preprocessor
#include
int main()
{
#error
return 0;
}
3.#line preprocessor
General form of #line preprocessor is #line Number “filename”
file name string replaces the string value of __filename__
while number changes the value of __line__
The main use of #line is in debugging and rare programming situation
this example will show the #line preprocessor
int main()
{
printf(“n%d”,__LINE__);//print 9
#line 100;
printf(“n%d”,__LINE__);//print101
#line 103 “super C”
printf(“n%d”,__FILE__);
printf(“n%d”,__FILE__);
4.#pragma
The #pragma Directives are used to turn ON or OFF certain features. They vary from compiler to compiler.
5. Typedef
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.
Example
#include<stdio.h>
int main()
{
typedef int Number;
Number num1 = 40,num2 = 20;
Number answer;
answer = num1 + num2;
printf("Answer : %d",answer);
return(0);
}.
6.#ifdef
#ifdef directive checks whether particular macro is defined or not. If it is defined, “If” clause statements are included in source file Otherwise, “else” clause statements are included in source file for compilation and execution.
#include <stdio.h>
#define RAJU 100
int main()
{
#ifdef RAJU
print("RAJU is defined. So, this line will be added in " \
"this C file\n");
#else
printf("RAJU is not defined\n");
#endif
return 0;
}
Output: RAJU is defined So,this line will added.
7.#ifndef
#ifndef exactly acts as reverse as a #ifdef directive. If a particular macro is not defined, “If” clause statements are included in the source file.
#define
Use this to define constants or any macro substitution. Use as follows:
#define <macro> <replacement name>
For Example
#define FALSE 0
#define TRUE !FALSE
We can also define small "functions" using #define. For example max. of two variables:
#define max(A,B) ( (A) > (B) ? (A):(B))
So if in our C code we typed something like:
x = max(q+r,s+t);
after preprocessing, if we were able to look at the code it would appear like this:
x = ( (q+r) > (r+s) ? (q+r) : (s+t));
Other examples of #define could be:
#define Deg_to_Rad(X) (X*M_PI/180.0)
/* converts degrees to radians, M_PI is the value
of pi and is defined in math.h library */
#define LEFT_SHIFT_8 <<8
NOTE: The last macro LEFT_SHIFT_8 is only
valid so long as replacement context is valid i.e.
x = y LEFT_SHIFT_8.
#undef
This commands undefined a macro. A macro must be undefined before being redefined to a different value.
#include
This directive includes a file into code.
It has two possible forms:
#include <file>
or
#include "file"
<file> tells the compiler to look where system include files are held. Usually UNIX systems store files in $\backslash$usr$\backslash$include$\backslash$ directory.
"file" looks for a file in the current directory (where program was run from)
Included files usually contain C prototypes and declarations from header files and not (algorithmic) C code (SEE next Chapter for reasons)
#if -- Conditional inclusion
#if evaluates a constant integer expression. You always need a #endif to delimit end of statement.
We can have else etc. as well by using #else and #elif -- else if.
Another common use of #if is with:
#ifdef
-- if defined and
#ifndef
-- if not defined
These are useful for checking if macros are set -- perhaps from different program modules and header files.
For example, to set integer size for a portable C program between TurboC (on MSDOS) and Unix (or other) Operating systems. Recall that TurboC uses 16 bits/integer and UNIX 32 bits/integer.
Assume that if TurboC is running a macro TURBOC will be defined. So we just need to check for this:
#ifdef TURBOC
#define INT_SIZE 16
#else
#define INT_SIZE 32
#endif
As another example if running program on MSDOS machine we want to include file msdos.h otherwise a default.h file. A macro SYSTEM is set (by OS) to type of system so check for this:
#if SYSTEM == MSDOS
#include <msdos.h>
#else
#include "default.h"
#endif
Other Preprocessor Commands
There are few other preprocessor directives available:
#error
the text of error message -- generates an appropriate compiler error message. e.g
#ifdef OS_MSDOS
#include <msdos.h>
#elifdef OS_UNIX
#include "default.h"
#else
#error Wrong OS!!
#endif
# line
number "string" -- informs the preprocessor that the number is the next number of line of input. "string" is optional and names the next line of input. This is most often used with programs that translate other languages to C. For example, error messages produced by the C compiler can reference the file name and line numbers of the original source files instead of the intermediate C (translated) source files.
Preprocessor
The C preprocessors is a tool which filters your source code before it is compiled.The preprocessor allows constant to be named using the #define notation.It particularly uses full for selection machine dependent pieces of code for different computer types, allowing a single program to be compiled and run on several computers. The main advantage of the preprocessor is easier to read and modify and develop.
Preprocessor is given the ability to
the inclusion of a header file
the inclusion of macros expansions
conditional compilation and line control.
Type of preprocessor
#define and #undef is used to define and undefining the macros
#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 Use 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.
1. #define directivesare used in
Defining a constant (#define PI 3.141)
Defining a mathematical expression
Defining a statement
Defining a type (#define INT32 long int)
Example
#define pi 3.14
#define true 1
#define floatingpointno float.
2. #error Use of this preprocessor is in debugging whenever this preprocessor is
encountered during the compilation the compiler will stop the compilation and it will display
the error message with preprocessor compilation output results in the window.
General form of #error is
This example source code display the use of #error preprocessor
#include
int main()
{
#error
return 0;
}
3.#line preprocessor
General form of #line preprocessor is #line Number “filename”
file name string replaces the string value of __filename__
while number changes the value of __line__
The main use of #line is in debugging and rare programming situation
this example will show the #line preprocessor
int main()
{
printf(“n%d”,__LINE__);//print 9
#line 100;
printf(“n%d”,__LINE__);//print101
#line 103 “super C”
printf(“n%d”,__FILE__);
printf(“n%d”,__FILE__);
4.#pragma
The #pragma Directives are used to turn ON or OFF certain features. They vary from compiler to compiler.
5. Typedef
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.
Example
#include<stdio.h>
int main()
{
typedef int Number;
Number num1 = 40,num2 = 20;
Number answer;
answer = num1 + num2;
printf("Answer : %d",answer);
return(0);
}.
6.#ifdef
#ifdef directive checks whether particular macro is defined or not. If it is defined, “If” clause statements are included in source file Otherwise, “else” clause statements are included in source file for compilation and execution.
#include <stdio.h>
#define RAJU 100
int main()
{
#ifdef RAJU
print("RAJU is defined. So, this line will be added in " \
"this C file\n");
#else
printf("RAJU is not defined\n");
#endif
return 0;
}
Output: RAJU is defined So,this line will added.
7.#ifndef
#ifndef exactly acts as reverse as a #ifdef directive. If a particular macro is not defined, “If” clause statements are included in the source file.
#define
Use this to define constants or any macro substitution. Use as follows:
#define <macro> <replacement name>
For Example
#define FALSE 0
#define TRUE !FALSE
We can also define small "functions" using #define. For example max. of two variables:
#define max(A,B) ( (A) > (B) ? (A):(B))
So if in our C code we typed something like:
x = max(q+r,s+t);
after preprocessing, if we were able to look at the code it would appear like this:
x = ( (q+r) > (r+s) ? (q+r) : (s+t));
Other examples of #define could be:
#define Deg_to_Rad(X) (X*M_PI/180.0)
/* converts degrees to radians, M_PI is the value
of pi and is defined in math.h library */
#define LEFT_SHIFT_8 <<8
NOTE: The last macro LEFT_SHIFT_8 is only
valid so long as replacement context is valid i.e.
x = y LEFT_SHIFT_8.
#undef
This commands undefined a macro. A macro must be undefined before being redefined to a different value.
#include
This directive includes a file into code.
It has two possible forms:
#include <file>
or
#include "file"
<file> tells the compiler to look where system include files are held. Usually UNIX systems store files in $\backslash$usr$\backslash$include$\backslash$ directory.
"file" looks for a file in the current directory (where program was run from)
Included files usually contain C prototypes and declarations from header files and not (algorithmic) C code (SEE next Chapter for reasons)
#if -- Conditional inclusion
#if evaluates a constant integer expression. You always need a #endif to delimit end of statement.
We can have else etc. as well by using #else and #elif -- else if.
Another common use of #if is with:
#ifdef
-- if defined and
#ifndef
-- if not defined
These are useful for checking if macros are set -- perhaps from different program modules and header files.
For example, to set integer size for a portable C program between TurboC (on MSDOS) and Unix (or other) Operating systems. Recall that TurboC uses 16 bits/integer and UNIX 32 bits/integer.
Assume that if TurboC is running a macro TURBOC will be defined. So we just need to check for this:
#ifdef TURBOC
#define INT_SIZE 16
#else
#define INT_SIZE 32
#endif
As another example if running program on MSDOS machine we want to include file msdos.h otherwise a default.h file. A macro SYSTEM is set (by OS) to type of system so check for this:
#if SYSTEM == MSDOS
#include <msdos.h>
#else
#include "default.h"
#endif
Other Preprocessor Commands
There are few other preprocessor directives available:
#error
the text of error message -- generates an appropriate compiler error message. e.g
#ifdef OS_MSDOS
#include <msdos.h>
#elifdef OS_UNIX
#include "default.h"
#else
#error Wrong OS!!
#endif
# line
number "string" -- informs the preprocessor that the number is the next number of line of input. "string" is optional and names the next line of input. This is most often used with programs that translate other languages to C. For example, error messages produced by the C compiler can reference the file name and line numbers of the original source files instead of the intermediate C (translated) source files.
Comments