C Programming The C Preprocessor and Some Advanced Topics June 03, 2005 Learn More about #define Define a macro name Create function-like macros to avoid the time might be longer #define SUM(i, j) i+j int sum; sum = SUM(10, 20); printf("%d\n", sum);
Precedence of All the C Operators ( ) [ ] -> >.! ~ + - ++ -- (type cast) * & sizeof * / % + - << >> < <= > >= ==!= & ^ &&? : = += -= = *= /= etc. Highest Lowest Sample : #define #define MAX(i, j) ((i)>(j))? (i) : (j) printf("max(1, 2) = %d\n", MAX(1, 2)); printf("max(1, -1) = %d\n", MAX(1, -1)); printf("max(100 && -1, 0) = %d\n", MAX(100 && -1, 0));
Result : #define Learn More about #include #include <filename> search a special directory devoted to the standard header files #include filename searching a current working directory header files that you create #include "stdlib.h" printf("this is a random number: %d\n", rand());
Understand Conditional Compilation #if #if #else #elif #ifdef #ifndef parts of the source code of a program to be selectively compiled constant-expression: no variables may be used!! #if constant-expression #if constant-expression #else #if constant-expression-1 #elif constant-expression-2 #elif constant-expression-3.. #ifdef macro-name #ifndef macro-name Sample : #ifdef, #include <string.h> #define DEBUG int i, j; char buf[256], buf2[256]; printf("enter a string: "); scanf("%s", buf); #ifdef DEBUG printf("buf: %s\n", buf); i = strlen(buf); for(j=0; j<i; j++) buf2[j]=buf[i-1-j]; buf2[j]=0; #ifdef DEBUG printf("buf2: %s\n", buf2);
Result : #ifdef, Learn about #error #error error-message Stop compilation and issue the error-message number of line, name of file, not enclosed between quotes principal use debugging int i; i = 10; #error This is an error message. printf("%d\n", i); This line will not be compiled
Learn about #undef undefine a macro name #undef macro-name #define DEBUG #ifdef DEBUG printf("debug is defined.\n"); #undef DEBUG #ifdef DEBUG printf("this line is not compiled.\n"); Learn about #line #line line-num filename change the number of line and the name of the source file currently being compiled principal use debugging and managing large projects #line 1000 "myporg.c" #error Check the line number and file name.
Examine C s Built-In Macros Five predefined macro names at least LINE : (integer value) line number of the source line currently being complied FILE : (string) name of file currently being complied DATE : (string) current system date month/day/year TIME : (string) time the compilation of a program began hours:minutes:seconds STDC : (0 or 1) value 1 if the compiler conforms to the ANSI standard Sample : C s Built-In Macros printf("compiling %s, line %d, on %s, at %s\n", FILE, LINE, DATE, TIME );
Sample : #line #line 1000 "myporg.c" printf("compiling %s, line %d, on %s, at %s\n", FILE, LINE, DATE, TIME ); Use the # Operator Turn the argument of a function-like macro into a quoted string #define MKSTRING(str) # str int value; value = 10; printf("%s is %d\n", MKSTRING(value), value);
Use the ## Operator Concatenate two identifiers #define OUTPUT(i) printf("%d %d\n", i ## 1, i ## 2) int count1, count2; int i1, i2; count1 = 10; count2 = 20; i1 = 99; i2 = -10; OUTPUT(count); OUTPUT(i); Sample: ## Operator #define JOIN(a, b) a ## b printf("%s\n", JOIN("one ", "two"));
Understand Function Pointers Variable that contains the address of the entry point to a function int sum(int a, int b); int (*p)(int x, int y); int result; p = sum; result = (*p)(10, 20); // result = p(10, 20); printf("result = %d\n", result); int sum(int a, int b) return a+b; Sample: Function Pointers (1) float add(float a, float b); float subtract(float a, float b); float multiply(float a, float b); float divide(float a, float b); float (*op[4])(float x, float y) = add, subtract, multiply, divide ; float i, j, result; int k; printf("enter two numbers: "); scanf("%f%f", &i, &j); printf("[0] Add, [1] Subtract, [2] Multiply, [3] Divide\n"); do printf("enter number of operation: "); scanf("%d", &k); while( k<0 k>3 );
Sample: Function Pointers (2) result = (*op[k])(i, j); printf("result = %f\n", result); float add(float a, float b) return a+b; float subtract(float a, float b) return a-b; float multiply(float a, float b) return a*b; float divide(float a, float b) if(b) return a/b; Result: Function Pointers