开发者

What is The Use Of # in C or C++? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. 开发者_如何学运维 Closed 11 years ago.

i would like to know the usage of # in C or C++..... please can u inform me what is the purpose of # or Why it is used?


In C/C++, the # sign marks preprocessor directives.

If you're not familiar with the preprocessor, it works as part of the compilation process, handling includes, macros, and more. It actually adds code to the source file before the final compilation. If you're using gcc/g++, you can see what the preprocessor generates by using the -E flag.

Example preprocessor directives:

Includes:

#include <iostream>

Includes are used to insert the contents of the included file into the current file at the location of the directive.

Constants:

#define RANDOM_NUMBER 4

During processing, every instance of RANDOM_NUMBER in the file will be textually replaced with 4

Conditional compilation:

#ifdef DEBUG
printf( "Random number: %d", RANDOM_NUMBER );
#endif

In this case, the print statement will only be a part of the compiled program if the DEBUG macro has been defined.


It is used for all the preprocessor directives, like #include, #ifdef and all the others


# can be used to represent the mesh character:

char mesh = '#';

It can also be misused to generate syntax errors:

char#mesh; // error: stray '#' in program
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜