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

匿名 (未验证) 提交于 2019-12-03 02:30:02

问题:

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);  

回答1:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!