[go: up one dir, main page]

0% found this document useful (0 votes)
55 views15 pages

Macros

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 15

C++ 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));

• Why (X) or (Y) and not simple X or simple Y?


• What will happen if min(4+3, 5-2)?

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.

• #define foo(x) x, "x"

• foo(bar) ==> bar, "x"

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

cout <<"The minimum is " << MIN(i, j) << endl;

#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

• // all #define macros go here


• // all function prototype declarations go here
• // this will ensure that the header file “filename.h” is included only once
• // this is because in the first call of #include “filename.h” in the source .cpp file
_FILENAME_H will be defined.
• // Therefore in the second call of #include the piece of code between #ifndef and
#endif will not be included in the source file.

• #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

myprog.cpp file myfile.h file

You might also like