using define label in a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hschew00
    New Member
    • Aug 2009
    • 12

    using define label in a string

    Is there a way to declare a string where inside the string, a predefined label is included? I.e.,

    #define MY_CODE 0x1000
    char str[]="Here is MY_CODE"; //will not work I know

    Any way to get around this instead of hard-coding it?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by hschew00
    Is there a way to declare a string where inside the string, a predefined label is included? I.e.,

    #define MY_CODE 0x1000
    char str[]="Here is MY_CODE"; //will not work I know

    Any way to get around this instead of hard-coding it?
    You can use a bit of preprocessor wizardry:

    Code:
    #define str(x) #x
    #define concat(a, b) a##str(b)
    
    #define MY_CODE 0x1000
    char str[]=concat("Here is ",  MY_CODE);
    kind regards,

    Jos

    Comment

    • hschew00
      New Member
      • Aug 2009
      • 12

      #3
      Jos,
      Thanks for the quick reply. However, although it works, can you explain to me how it works in preprocessor? Intuitively I thought the above 2 defines is equivalent to sth like this
      #define concat(a,b) a###b

      Of course the above didn't work.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Copied right from the Standard:

        Originally posted by C Standard

        3.8.3.1 Argument substitution

        After the arguments for the invocation of a function-like macro
        have been identified, argument substitution takes place. A parameter in the replacement list, unless preceded by a # or ## preprocessing token or followed by a ## preprocessing token (see below), is replaced by the corresponding argument after all macros contained therein have been expanded. Before being substituted, each argument's preprocessing tokens are completely macro replaced as if they formed the rest of the source file; no other preprocessing tokens are available.


        3.8.3.2 The # operator

        Constraints

        Each # preprocessing token in the replacement list for a function-like macro shall be followed by a parameter as the next preprocessing token in the replacement list.

        Semantics

        If, in the replacement list, a parameter is immediately preceded by a # preprocessing token, both are replaced by a single character string literal preprocessing token that contains the spelling of the preprocessing token sequence for the corresponding argument. Each occurrence of white space between the argument's preprocessing tokens becomes a single space character in the character string literal. White space before the first preprocessing token and after the last preprocessing token comprising the argument is deleted. Otherwise, the original spelling of each preprocessing token in the argument is retained in the character string literal, except for special handling for producing the spelling of string literals and character constants: a \ character is inserted before each and \ character of a character
        constant or string literal (including the delimiting characters). If the replacement that results is not a valid character string literal, the behavior is undefined. The order of evaluation of # and ## operators is unspecified.


        3.8.3.3 The ## operator

        Constraints

        A ## preprocessing token shall not occur at the beginning or at the end of a replacement list for either form of macro definition.

        Semantics

        If, in the replacement list, a parameter is immediately preceded or followed by a ## preprocessing token, the parameter is replaced by the corresponding argument's preprocessing token sequence. For both object-like and function-like macro invocations, before the replacement list is reexamined for more macro names to replace, each instance of a ## preprocessing token in the replacement list (not from an argument) is deleted and the preceding preprocessing token is concatenated with the following preprocessing token. If the result is not a valid preprocessing token, the behavior is undefined. The
        resulting token is available for further macro replacement. The order of evaluation of ## operators is unspecified.
        kind regards,

        Jos

        Comment

        Working...