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;
}
7
  • 1
    so it's basically an eclipse IDE questions? most IDEs have a great trouble parsing C/C++ code on the fly, one of the reasons being the macros Commented Jun 20, 2018 at 11:44
  • @Jean-FrançoisFabre it is not an IDE question. It is a C question raised by seeing an IDE problem that might stem from an actual problem Commented Jun 20, 2018 at 11:45
  • Did you include the header file in the c file? Commented Jun 20, 2018 at 11:47
  • 5
    I do believe it's an IDE problem. It's perfectly valid C code. Anecdotal, but I was never able to run Eclipse's indexer to the point nothing is underlined red. Commented Jun 20, 2018 at 11:54
  • 1
    I'd recommend GNAT Programming Studio, which has a nice C mode (based on clang) which works... it also uses projects and doesn't have workspaces. Eclipse sucks so much... Code::Blocks isn't better. Commented Jun 20, 2018 at 11:55

1 Answer 1

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.


Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.