1 / 16

PREPROCESSOR

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 )

dhaskins
Télécharger la présentation

PREPROCESSOR

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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/

  2. 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/

  3. 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/

  4. 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/

  5. 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/

  6. 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/

  7. 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/

  8. 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/

  9. CONDITIONAL TEXT REPLACEMENT • #ifdef • #ifndef • #if • #endif • #else • #elif http://www.cplusplus.com/doc/tutorial/preprocessor/

  10. 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/

  11. 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/

  12. PREDEFINED MACROS Note, the object code file name may be obtained from argv[0] http://www.cplusplus.com/doc/tutorial/preprocessor/

  13. 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/

  14. 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/

  15. 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/

  16. 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; }

More Related