2

Given the following code:

#include <stdio.h>

#ifndef STR
    #define STR "HELLO"
#endif

int main() {
    printf(STR "WORLD \n");
    return 0;
}

which says: if STR was not defined, then define it to be "HELLO", so the output will be

HELLO WORLD

How can I modify the value of STR when compiling using gcc?

I've tried

gcc -Wall program.c -DSTR="HI" -o program

but it didn't produce the expected output.

4
  • 2
    try gcc -Wall program.c -DSTR=\"HI\" -o year Commented Oct 31, 2014 at 9:17
  • @BLUEPIXY, this is an answer Commented Oct 31, 2014 at 9:19
  • 1
    @BLUEPIXY: Those backslashes were the problem, now it works like a charm! Thanks a lot! Commented Oct 31, 2014 at 9:19
  • 1
    "didn't produce the expected output", it must have given you a compiler error, no? Commented Oct 31, 2014 at 9:39

1 Answer 1

1

Try in the form of:

-DSTR=\"MyString\"
Sign up to request clarification or add additional context in comments.

1 Comment

It might be worth mentioning that this is a shell issue. If the command would be exec*()ed for example the back shlashes wouldn't be necessary.

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.