views:

82

answers:

5

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

This question is similar to another: http://stackoverflow.com/questions/25746/whats-the-difference-between-a-string-constant-and-a-string-literal ...except that one was regarding Objective-C (using NSString) and not C.

+1  A: 

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
So basically, either the phrase String Literal or String Constant can be used to mean the same thing?
Dave Gallagher
@Dave: Yes, that's what I said (slightly caveated) - and what other people said too.
Jonathan Leffler
+3  A: 

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

Alex
You mean which *string* is used to describe the string :)
ArunSaha
A: 
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
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
Point was that literals are actual text not hidden via a varible label. But they are both constant character arrays.
mikek3332002
+3  A: 

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).

pst
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 Gallagher
+1  A: 

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

codaddict