> ~ Online tutorial

Preprocessor


C is unusual in that it has a pre-processor. This comes from its Unix origins. As its name might suggest, the preprocessor is a phase which occurs prior to compilation of a program.
The preprocessor has two main uses:
it allows external files, such as header files, to be included and it allows macros to be defined. This useful feature traditionally allowed constant values to be defined in Kernighan and Ritchie C, which had no constants in the language.
Pre-processor commands are distinguished by the hash (number) symbol #. One example of this has already been encountered for the standard header file stdio.h.
#include <stdio.h>

is a command which tells the preprocessor to treat the file stdio.h as if it were the actually part of the program text, in other words to include it as part of the program to be compiled.
Macros are words which can be defined to stand in place of something complicated: they are a way of reducing the amount of typing in a program and a way of making long ungainly pieces of code into short words. For example, the simplest use of macros is to give constant values meaningful names: e.g.
#define TELEPHNUM 720663

This allows us to use the word TELEPHNUM in the program to mean the number 720663

Macro Functions


A more advanced use of macros is also permitted by the preprocessor. This involves macros which accept parameters and hand back values. This works by defining a macro with some dummy parameter, say x.

For example: a macro which is usually defined in one of the standard libraries is abs() which means the absolute or unsigned value of a number.
It is defined below:
#define ABS(x) ((x) < 0) ? -(x) : (x)

The result of this is to give the positive (or unsigned) part of any number or variable. This would be no problem for a function which could accept parameters, and it is, in fact, no problem for macros. Macros can also be made to take parameters. Consider the ABS() example. If a programmer were to write ABS(4) then the preprocessor would substitute 4 for x. If a program read ABS(i) then the preprocessor would substitute i for x and so on. (There is no reason why macros can't take more than one parameter too. The programmer just includes two dummy parameters with different names.

 ABS can be used to display the positive number only. that is if you define the x=-100 the produce the x=100
that produce the positive value only
Program

main()
{
int x=-100,y;
y=abs(x);
printf("the value is %d",y);
}


output
 the value is 100

Please Give Us Your 1 Minute In Sharing This Post!
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Powered By: BloggerYard.Com

0 comments: