Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error pasting ""HELLO"" and ""WORLD"" does not give a valid preprocessing token

This is the faulty code

#include<stdio.h>

#define CAT_I(A, B)         A ## B
#define CAT(A, B)           CAT_I(A,B)

void main (void)
{
        printf(CAT("HELLO","WORLD"));
}

Why it gives that error? How could I fix it?

EDIT

This is what I am trying to do

#define TAG                   "TAG"
#define PRE                   CAT(CAT("<",TAG),">")  
#define POS                   CAT(CAT("</",TAG),">") 

#define XML      CAT(CAT(PRE,"XML SOMETHING"),POS)   

then

printf(XML); 
like image 320
trucos Avatar asked Jun 22 '12 12:06

trucos


1 Answers

The result of ## must be a single token, and "HELLO""WORLD" is not a single token. To concatenate strings, simply leave them beside each other:

printf("HELLO" "WORLD");

Or change your macro to remove the ##.

#define CAT(A, B) A B

String literals are concatenated together when there are no intervening tokens between them.

like image 78
Seth Carnegie Avatar answered Nov 15 '22 15:11

Seth Carnegie