-1

How can I declare constant strings and what use are they in programming? I don't know If I'm doing this right but would this be the correct way of doing it?:

    const char my_string="Hello";
tadm123
  • 8,294
  • 7
  • 28
  • 44

3 Answers3

1
const char my_string="Hello";

This is not valid C. A string literal like "hello" is of type char[n], not const char. The correct declaration is

const char my_string[] = "Hello";

Also, its a good idea to name your constants in ALLCAPS.

lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75
0

here's the answer:

  • Method 1

    const char my_string[] = "Hello";

  • Method 2

    const char *my_string="Hello";

as you ask why it is used in C Programming the answer is when we want the value to remain the same and to protect the value from being overwritten we declare the variable as constant.

Chris Tang
  • 567
  • 7
  • 18
tryKuldeepTanwar
  • 3,490
  • 2
  • 19
  • 49
  • And yet, here, `*my_string` is a non-constant variable that points to constant data, so that variable is *not* protected from (over)writing. – underscore_d Nov 08 '17 at 09:28
-3

In the C programming language, the meaning of const is much less powerful than it is in other, more-recent, languages. In fact, it could well be said that the meaning is not the same at all.

To quote http://www.geeksforgeeks.org/const-qualifier-in-c/ ... (emphasis and omissions mine):

The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed [...] The outcome is implementation-defined(!) if an attempt is made to change a const.

The key thing to notice here is that, in C (which, in its defense, dates from the 1970's), the keyword const is a qualifier. It is not a "compile-time constant!"

The C language used #define declarations as its closest approximation to the behavior that other languages use when presented with the keyword, const.

Consider the following web-page, "The C++ 'const' declaration, Why & How," where author Andrew Hardwick frankly discusses the treatment of this keyword in both the C and the C++ languages: http://duramecho.com/ComputerInformation/WhyHowCppConst.html

Contrast both of these languages to other languages' treatment of this word, which they consider to be a declaration (not a qualifier), and which they universally treat in an entirely different way.

Mike Robinson
  • 8,490
  • 5
  • 28
  • 41
  • 1
    You discuss the meaning of `const`, but you don't actually answer the question. And there's a simpler explanation of what `const` means: it means *read-only*. – Keith Thompson Jul 14 '16 at 22:50
  • It seems abundantly clear to me ... and of course, I could be utterly mistaken ... that the OP "has *[almost every ...] other languages' meaning" of the word `const` in mind. Hence, my (long-winded) post. "While you are absolutely correct, Keith, *in the context of 'C' as well-understood by those of us who use it regularly,"* I see in front of us a source-string "that would be entirely at home" in almost every *other* language." That's the point-of-view that guided my response. – Mike Robinson Jul 14 '16 at 23:04
  • The OP asks how to declare "constant strings", and shows an example that would be correct with the addition of a `*`. The question isn't 100% clear, but I'm pretty sure you haven't actually answered it. – Keith Thompson Jul 14 '16 at 23:06
  • ***You and I(!)*** know what would be *"correct with the addition of a `*`!" But, does the *OP* know that? My guess(!) was that, "just maybe, he *doesn't."* (Maybe he's relatively new to C/C++, in which case: "the rules *here* are altogether different.) It was, in the end, a "just trying to be helpful" *guess.* – Mike Robinson Jul 14 '16 at 23:29
  • Bottom line: You didn't answer the question. – Keith Thompson Jul 14 '16 at 23:35
  • I yield to your opinion without further comment, and openly **encourage you** to down-vote *(if you have not done so already)* at your pleasure. – Mike Robinson Jul 15 '16 at 03:03