160 likes | 341 Views
Learn how to utilize C++ preprocessor macros effectively to streamline coding processes, improve code readability, and optimize workflow. Understand macros, conditional text replacement, and predefined macros with practical examples.
E N D
PREPROCESSOR • Code replacement • Pre-compiler does this • # indicates a preprocessor command • #include <filename> includes a file before compiling • No ending semicolon ; needed, must be in a single line (to continue on the next line use backslash \) • By convention: all upper case identifier http://www.cplusplus.com/doc/tutorial/preprocessor/
MACROS #define TABLE_SIZE 100 int table1[TABLE_SIZE]; int table2[TABLE_SIZE]; ….. Produces resulting pre-processed code: int table1[100]; int table2[100]; ….. http://www.cplusplus.com/doc/tutorial/preprocessor/
MACROS One can generate code, e,g., a function with pre-processor: #define getmax(a,b) a>b ? a : b // function macro #include <iostream> using namespace std; #define getmax(a,b) ((a)>(b)?(a):(b)) int main() { int x = 5, y; y= getmax(x,2); cout << y << endl; cout << getmax(7,x) << endl; return 0; } http://www.cplusplus.com/doc/tutorial/preprocessor/
MACROS #include <iostream> using namespace std; #define getmax(a,b) ((a)>(b)?(a):(b)) int main() { intx=5, y; y= getmax(x,2); cout << y << endl; cout << getmax(7,x) << endl; return 0; } http://www.cplusplus.com/doc/tutorial/preprocessor/
MACROS #include <iostream> using namespace std; #define getmax(a,b) ((a)>(b)?(a):(b)) int main() { intx=5, y; y= getmax(x,2); cout << y << endl; cout << getmax(7,x) << endl; return 0; } Will produce the right hand side code: // after pre-processing compiler finds this as input #include <iostream> using namespace std; #define getmax(a,b) ((a)>(b)?(a):(b)) int main() { intx=5, y; y= ((x)>(2)?(x):(2)); cout << y << endl; cout << ((7)>(x)?(7):(x)) << endl; return 0; } http://www.cplusplus.com/doc/tutorial/preprocessor/
UNDEF #define TABLE_SIZE 100 int table1[TABLE_SIZE]; #undefTABLE_SIZE #define TABLE_SIZE 200 int table2[TABLE_SIZE]; Will produce: int table1[100]; int table2[200]; http://www.cplusplus.com/doc/tutorial/preprocessor/
MACRO OPERATOR # creates double quotes around #define str(x) #x ….. cout<< str(test); Will produce the right hand side : // after pre-processing compiler finds this as input #define str(x) #x ….. cout << “test” http://www.cplusplus.com/doc/tutorial/preprocessor/
MACRO OPERATOR ##concatenates #define glue(a,b) a##b ….. glue(c, out) << “test” Will produce the right hand side : // after pre-processing compiler finds this as input #define str(x) #x ….. cout<< “test” http://www.cplusplus.com/doc/tutorial/preprocessor/
CONDITIONAL TEXT REPLACEMENT • #ifdef • #ifndef • #if • #endif • #else • #elif http://www.cplusplus.com/doc/tutorial/preprocessor/
IFDEF #ifdefTABLE_SIZE // compile if TABLE_SIZE is defined previously with #define otherwise the next statement is ignored by compiler int table[TABLE_SIZE]; #endif http://www.cplusplus.com/doc/tutorial/preprocessor/
IFNDEF ….. code ….. #ifndefTABLE_SIZE // compile if TABLE_SIZE is not yet defined with the following #define #define TABLE_SIZE 100 #endif int table[TABLE_SIZE]; // if TABLE_SIZE was already defined that value will be used instead http://www.cplusplus.com/doc/tutorial/preprocessor/
PREDEFINED MACROS Note, the object code file name may be obtained from argv[0] http://www.cplusplus.com/doc/tutorial/preprocessor/
Pragma directives #include <stdio.h> void func1(); void func2(); #pragma startup func1 // runs before main #pragma exit func2 // runs after main void func1() { printf("Inside func1()\n"); } void func2() { printf("Inside func2()\n"); } int main() { printf("Inside main()\n"); return 0; } Inside func1() Inside main() Inside func2() https://www.geeksforgeeks.org/pragma-directive-in-c-c/
Pragma directives #include<stdio.h> void func1(); void func2(); void __attribute__((constructor)) func1(); // on gcc void __attribute__((destructor)) func2(); void func1() { printf("Inside func1()\n"); } void func2() { printf("Inside func2()\n"); } int main() { printf("Inside main()\n"); return 0; } Inside func1() ok on gcc Inside main() Inside func2() ok on gcc https://www.geeksforgeeks.org/pragma-directive-in-c-c/
Pragma directives #pragma warn -rvl: This directive hides those warning which are raised when a function which is supposed to return a value does not return a value. #pragma warn -par: This directive hides those warning which are raised when a function does not uses the parameters passed to it. #pragma warn -rch: This directive hides those warning which are raised when a code is unreachable. For example: any code written after the return statement in a function is unreachable. + in front forces the option, and . toggles between hide and show https://www.geeksforgeeks.org/pragma-directive-in-c-c/
CUDA directive for GPU(Not in syllabus!) #include "stdio.h“ __global__ void add(int a, int b, int *c) { *c = a + b; } int main() { int a,b,c; int *dev_c; a=3; b=4; cudaMalloc((void**)&dev_c, sizeof(int)); add<<<1,1>>>(a,b,dev_c); cudaMemcpy(&c, dev_c, sizeof(int), cudaMemcpyDeviceToHost); printf("%d + %d is %d\n", a, b, c); cudaFree(dev_c); return 0; }