twitter
    Find out what I'm doing, Follow Me :)

Defines And Macros Are Aids To Clear Programming

Load and display the file named define.c for your first look at some defines and macros.

#define START 0 /* Starting point of loop */
#define ENDING 9 /* Ending point of loop */
#define MAX(A,B) ((A)>(B)?(A):(B)) /* Max macro definition */
#define MIN(A,B) ((A)>(B)?(B):(A)) /* Min macro definition */

main( )
{
int index,mn,mx;
int count = 5;
for (index = START;index <= ENDING;index++) {
mx = MAX(index,count);
mn = MIN(index,count);
printf("Max is %d and min is %d\n",mx,mn);
}
}



Notice the first four lines of the program each starting with the word "#define". This is the way all defines and macros are defined. Before the actual compilation starts, the compiler goes through a preprocessor pass to resolve all of the defines. In the present case, it will find every place in the program where the combination "START" is found and it will simply replace it with the 0 since that is the definition. The compiler itself will never see the word "START", so as far as the compiler is concerned, the zeros were always there. It should be clear to you by now that putting the word "START" in your program instead of the numeral 0 is only a convenience to you and actually acts like a comment since the word "START" helps you to understand what
the zero is used for.

In the case of a very small program, such as that before you, it doesn’t really matter what you use. If, however, you had a 2000 line program before you with 27 references to the START, it would be a completely different matter. If you wanted to change all of the STARTS in the program to a new number, it would be simple to change the one #define, but difficult, and possible disastrous if you missed one or two of the references.

In the same manner, the preprocessor will find all occurrence of the word "ENDING" and change them to 9, then the compiler will operate on the changed file with no knowledge that "ENDING" ever existed. It is a fairly common practice in C programming to use all capital letters for a symbolic constant such as "START" and "ENDING" and use all lower case letters for variable names. You can use any method you choose since it is mostly a matter of personal taste.