# and ## Operators in C program

0

In C programming, the "#" operator is used to include a file in the current source code file. For example, if you have a file named "header.h" that contains commonly used function declarations or macros, you can include that file in your main source code file using the line "#include <header.h>".


The "##" operator is known as the preprocessing operator and the "token-pasting operator," is used to concatenate two tokens together. It allows you to merge two tokens into a single token while expanding macros. For example, if you have two macro definitions:

When the macro C is expanded, it will have the value "12" which is concatenation of 1 and 2. This operator can be useful for creating unique macro or constant names based on other macro arguments or for generating unique strings based on macro arguments.


The "#" operator, also known as the "stringizing operator," is used to convert a preprocessor macro argument into a string constant. 

For example, if you have a macro definition like this:

You can use the FOO macro like this:

The output of the above code would be: "hello" (with quotation marks). This can be useful for creating string representations of macro or constant values, or for generating unique strings based on macro arguments.


The "##" operator can be used to concatenate actual macro arguments during macro expansion. It allows you to create new identifiers or strings based on the values passed to a macro.

For example, if you have a macro definition like this:

You can use the CREATE_VARIABLE macro like this:

The macro will be expanded to:

Here the ## operator is used to concatenate the macro argument "myvar" with the string "_variable" to create a new identifier "myvar_variable".


This feature can be useful for creating unique variable or constant names based on other macro arguments or for generating unique strings based on macro arguments.

Tags

Post a Comment

0Comments
Post a Comment (0)