Macro operators: # and ##

Chapter 8:Preprocessor And Complex Declarations

    Macro operators: # and ##

  • The stringize or number-sign operator (#)

              If it is used within a macro definition, alters a macro parameter into a string constant. It is used only with macros that take arguments. When it instantly precedes the name of one of the macro arguments, the argument sent to the macro is enclosed within quotation symbols and is considered as a string literal. Then each occurrence of a combination of the stringizing operator and argument within the macro definition is replaced by the string literal.

    For example:

    #define stringizer(s)  printf (#s"\n")
    Output from the preprocessor:

              printf ("good\n")

              The expansion explains that the parameter is changed literally as if it were a string. When the preprocessor stringizes the s parameter, the result is:  

              printf ("good" "\n")

              As strings split by whitespace are concatenated at compile time, these 2 strings are combined into "good\n". If the string sent as a parameter holds characters that must be literalized or escaped (e.g, " and \), the needed \ character is inserted automatically.

    The Token-pasting or double-number-sign operator (##)

              ANSI has introduced a well-defined token-pasting operator (##), which is sometimes referred as the merging operator, is used in both object-like and function-like macros. It simply eliminates any white space around it and concatenates separate tokens into a single token and therefore cannot be the first or last token in the macro definition.

              The formal argument is replaced by the unexpanded actual argument when a formal argument in a macro definition is preceded or followed by ##. Macro expansion is not done on the argument before replacement. After that, each occurrence of the token-pasting operator in token string is eliminated, and the tokens preceding and following it are joined together.

              The resulting token must be a valid one. The identifiers (variable names, function names), keywords (int, while, volatile), literals (strings, numbers, characters, true or false), operators and punctuators (+, -, *, (,) are valid tokens. If it indicates a macro name, then it is checked for replacement. The identifier signifies the name by which the joined tokens will be identified in the program prior to replacement. The token may be defined within the program or on the compiler command line. White space preceding or following the operator is optional.

               The order of evaluation is undefined if various merging operators (##) and / or stringizing operators (#) are used. The performance is undefined when the answer of using token-pasting operator is an invalid token.


    #include < stdio.h >
    #define f(m) printf( "IDNO" #m " = %d", IDNO ##m)
    int IDNO5 = 5;
    int main()
    The result:

              IDNO5 = 5

© 2015 by Learncertification All Rights Reserved. The certification names are the trademarks of their respective owners. Terms & Privacy Policy