-1

I have a string: "The world is a beautiful place to live in.".

I have two source files and a global header file. I can access the string in two easy ways:-

  • Declaring and defining a constant variable as shown: I can use it using extern

    const char *str = "The world is a beautiful place to live in.";

  • Using #define STR "The world is a beautiful place to live in."

Out of the above two options, which one is preferred in C?

Community
  • 1
  • 1
pkthapa
  • 1,029
  • 1
  • 17
  • 27

1 Answers1

0

It doesn't matter (doesn't matter if you only consider of getting a string literal) how you do but in case you want to get the string constant to be traced under debugger you may want to use the const char*str = ...

With macro definition you will get all the problems of macro expansion- the side effects of the expanson, the pollution of the name space, the deviation from debugger help etc

user2736738
  • 30,591
  • 5
  • 42
  • 56