2

What's the difference between a String Constant and String Literal in plain C?

This question is similar to another: What's the difference between a string constant and a string literal? ...except that one was regarding Objective-C (using NSString) and not C.

Community
  • 1
  • 1
Dave
  • 12,408
  • 12
  • 64
  • 67

5 Answers5

7

In the C99 Draft from 2007 the term sting literal is used. The term string constant does not appear in the draft at all.

I find string literal to be a good term choice when talking about "foo" (just as 42 is a literal number, "foo" is a literal string).

  • Thanks for the clarification. :) What confused me was that many books use the term String Constant, but C99 uses String Literal as you said. – Dave Oct 12 '10 at 03:46
2

They are one and the same. Merely a preference in which word you use to describe the string.

Alex
  • 64,178
  • 48
  • 151
  • 180
1

The spelling of the second word used to describe the same idea?

I would regard them as the same thing - alternative terms for the same construct.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0
const char * strConst;
strConst = "Hello World";

In this example

  • strConst is a string constant.
  • "Hello World" is a string literal, and is typically stored in the read only area of the program.
mikek3332002
  • 3,546
  • 4
  • 37
  • 47
  • 3
    Interesting analysis, but it doesn't really stand up, I'm afraid. The variable `strConst` is, indeed, a variable; it may be changed to point to other strings than the string literal, but you will not be able to modify the string pointed at via `strConst`. If you had written `const char * const strConst = "Hello World";`, then you might have a stronger case, but `strConst` is still a pointer rather than a string constant; a constant pointer to a constant string, but still a pointer, not a string constant. – Jonathan Leffler Oct 12 '10 at 03:44
  • Point was that literals are actual text not hidden via a varible label. But they are both constant character arrays. – mikek3332002 Oct 12 '10 at 04:10
0

Literal and constant mean the same which is notation for representing a fixed value in source code.

codaddict
  • 445,704
  • 82
  • 492
  • 529