0

Is there a problem doing something like this

#define A "world"
#define B "hello "A  // or adding a whitespace --> #define B "hello " A

and then using the B in printf(B"!"); (added another concatenation...)?

BTW, using #define B "hello "A without white-space is OK in C, but less so in C++11 - "invalid suffix on literal; C++11 requires a space between literal and identifier [-Wliteral-suffix]"

By what I know, this should be OK, since the compiler concatenates the strings (as said here), but when writing this on eclipse, with the #define's in one header file, and the printf in another, I get no errors, but the eclipse can't seem to recognize the define's

I know eclipse is evil, but is there a C problem in this (including misuse of language features, if this is one)?


header.h

#define A "world"
#define B "hello "A 

c.c

#include "header.h"
#include <stdio.h>
int main(){
   printf("%s", B"!");
   return 0;
}
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124

1 Answers1

1

I tried to run your code (header.h and c.c) on the Eclipse, it outputted hello world! successfully.
My execution environment is as follows.

  • OS: macOS 10.13.5
  • IDE: Eclipse IDE for C/C++ Developers, Oxygen.1a Release (4.7.1a), Build id 20171005-1200
  • Compiler(tool chain): Linux GCC

Your code is probably correct.
If you are running Eclipse with default settings, it seems that there is a problem with eclipse as you said.


tstn3357
  • 136
  • 4