Why does the following line of code generate error as " pasting / and / doesnot generate a valid preprocessing token"? Can someone please help

#define comment /##/
int main()
{
comment printf("hello");
return 0;
}

Recommended Answers

All 3 Replies

Because it's illegal in C. This has been answered for you in other forums already, in detail.

@Adak I dont think so its illegal in C infact the "##" is a valid token pasting operator for preprocessor.The answer to this is that comments are replaced by preprocessor into white spaces before the macros are even seen!!

The answer to this is that comments are replaced by preprocessor into white spaces before the macros are even seen!!

That's correct. It's not the macro itself that's wrong, it's how you were using it on the assumption that it would start a comment. For example:

#include <stdio.h>

#define STR_VAL(x) #x
#define STR(x)     STR_VAL(x)

#define comment /##/

int main(void)
{
    printf("'%s'\n", STR(comment));
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.