Macros
Macros
Macros
Object-like Macros
• #define BUFSIZE 1020 // defines BUFSIZE as 1020
• #define TABLESIZE BUFSIZE // defines TABLESIZE as BUFSIZE
• #undef BUFSIZE // un-defines BUFSIZE
• #define BUFSIZE 37 // redefines BUFSIZE to 37
• // now TABLESIZE will be 37
https://gcc.gnu.org/onlinedocs/gcc-5.1.0/cpp/Object-like-Macros.html#Object-like-Macros
Function-like Macros
• #define lang_init() c_init()
• Replaces
• lang_init() with c_init()
https://gcc.gnu.org/onlinedocs/gcc-5.1.0/cpp/Object-like-Macros.html#Object-like-Macros
Macro Arguments
• #define min(X, Y) ((X) < (Y) ? (X) : (Y))
• x = min(a, b); ==> x = ((a) < (b) ? (a) : (b));
• y = min(1, 2); ==> y = ((1) < (2) ? (1) : (2));
• z = min(a + 28, *p); ==> z = ((a + 28) < (*p) ? (a + 28) : (*p));
https://gcc.gnu.org/onlinedocs/gcc-5.1.0/cpp/Object-like-Macros.html#Object-like-Macros
• Macro parameters appearing inside string literals are not replaced by
their corresponding actual arguments.
https://gcc.gnu.org/onlinedocs/gcc-5.1.0/cpp/Object-like-Macros.html#Object-like-Macros
• #ifndef TABLE_SIZE
• #define TABLE_SIZE 100
• #endif
• int table[TABLE_SIZE];
http://www.cplusplus.com/doc/tutorial/preprocessor/
#if TABLE_SIZE>200
#undef TABLE_SIZE
#define TABLE_SIZE 200
#elif TABLE_SIZE<50
#undef TABLE_SIZE
#define TABLE_SIZE 50
#else
#undef TABLE_SIZE
#define TABLE_SIZE 100
#endif
int table[TABLE_SIZE];
http://www.cplusplus.com/doc/tutorial/preprocessor/
• #if defined ARRAY_SIZE
• #define TABLE_SIZE ARRAY_SIZE
• #elif !defined BUFFER_SIZE
• #define TABLE_SIZE 128
• #else
• #define TABLE_SIZE BUFFER_SIZE
• #endif
http://www.cplusplus.com/doc/tutorial/preprocessor/
Standard Macro Names
#include <iostream>
using namespace std;
int main()
{
cout << "This is the line number " << __LINE__;
cout << " of file " << __FILE__ << ".\n";
cout << "Its compilation began " << __DATE__;
cout << " at " << __TIME__ << ".\n";
cout << "The compiler gives a __cplusplus value of " << __cplusplus;
return 0;
}
http://www.cplusplus.com/doc/tutorial/preprocessor/
• #if 0
• code prevented from compiling
• #endif
https://www.tutorialspoint.com/cplusplus/cpp_preprocessor.htm
int main ()
Conditional Compilation {
int i, j;
i = 100;
• #include <iostream> j = 30;
#ifdef DEBUG
• using namespace std;
cerr <<"Trace: Inside main function" << endl;
• #define DEBUG #endif
#if 0
/* This is commented part */
• #define MIN(a,b) (((a)<(b)) ? a : b)
cout << MKSTR(HELLO C++) << endl;
#endif
#ifdef DEBUG
cerr <<"Trace: Coming out of main function" << endl;
#endif
return 0;
}
https://www.tutorialspoint.com/cplusplus/cpp_preprocessor.htm
Header Files
• #include <iostream> // iostream.h file is in the PATH directories
• #include “myfile.h” // myfile.h is in the current directory
https://www.tutorialspoint.com/cplusplus/cpp_preprocessor.htm
Header File Structures
• #ifndef _FILENAME_H
• #define _FILENAME_H
• #endif // _FILENAME_H
#ifndef _MYFILE_H
• #include “myfile.h” Text of header file #define _MYFILE_H
included here by
the pre-processor
before …
•… compilation
…
• ... ...
• ...
#endif // _MYFILE_H