[C / C ] Only preprocessing – Preprocessor directives

Opening this article, I'll show you a picture and you spend about 1 minutes to read the following program, Consider carefully to recognize the difference…

I LOVE YOU

Content
1. #define
2. Operator # and ##

Now you realize the difference is not ??? Initially when this photograph taken out people only pay attention to its contents (What about love something@@) or rather than a catch was his tẹo is written from “this” Misspelled (the right to be “yeu”) =)). We wish that you did not like you, but if that's okay, is important to recognize the difference really (not any of the above)? If realized, then it is very good, even if it is not recognized, please note, … program without main function.

First I will talk about #define

Sort of a simple, it is used to define constants. For example, you can:

#define x 100;
#define s "nguyenvanquan7826"

When the program runs, This line will be processed first in the entire program and its value will be replaced by values ​​defined and can not be changed. In this example, x is the value 100, s the value is a string “nguyenvanquan7826”.

We can also use it to define a certain function. For example defines find the largest value between 2 number.

#define getmax(a,b) a>b?a:b

Observe the program running, it fully comply.

/*
C/C++ program - code by nguyenvanquan7826
Home
*/ #include <iostream> #define getmax(a,b) a>b?a:b using namespace std; int main() { int m = getmax(5, 7); cout << m << endl; return 0; }


Operator # and operators ##

Math test # when used to replace the function is defined by the parameters in function string.

#define str(x) #x
cout << str(nguyenvanquan7826);

Equivalent to the command

cout << "nguyenvanquan7826";

Operator ## will connect 2 argument without leaving gaps between them.

define glue(a,b) a ## b
glue(c,out) << "nguyenvanquan7826";

Equivalent to the command

cout << "nguyenvanquan7826";

Now you try to explain the initial stages programs offline.

Reference Articles in: cplusplus.com