Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are string literals const?

Both GCC and Clang do not complain if I assign a string literal to a char*, even when using lots of pedantic options (-Wall -W -pedantic -std=c99):

char *foo = "bar"; 

while they (of course) do complain if I assign a const char* to a char*.

Does this mean that string literals are considered to be of char* type? Shouldn't they be const char*? It's not defined behavior if they get modified!

And (an uncorrelated question) what about command line parameters (ie: argv): is it considered to be an array of string literals?

like image 477
peoro Avatar asked Dec 20 '10 19:12

peoro


People also ask

Is string literal Const in C?

They are not const in C, but are in C++. They aren't generally writable as noted by others, but they don't behave as constants. There are some weird things you can't do, like 'case "abc"[2]:' in C, but you can in C++ where string literals are constant.

Are literals constants?

Constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal.

Can a string be const?

Const is a wrapper for strings that can only be created from program constants (i.e., string literals).

Is string constant or variable?

A string can be a constant or variable . If it is a constant, it is usually written as a sequence of characters enclosed in single or double quotation marks, ie 'hello' or "hello".


2 Answers

They are of type char[N] where N is the number of characters including the terminating \0. So yes you can assign them to char*, but you still cannot write to them (the effect will be undefined).

Wrt argv: It points to an array of pointers to strings. Those strings are explicitly modifiable. You can change them and they are required to hold the last stored value.

like image 68
Johannes Schaub - litb Avatar answered Sep 21 '22 08:09

Johannes Schaub - litb


For completeness sake the C99 draft standard(C89 and C11 have similar wording) in section 6.4.5 String literals paragraph 5 says:

[...]a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals. The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. For character string literals, the array elements have type char, and are initialized with the individual bytes of the multibyte character sequence;[...]

So this says a string literal has static storage duration(lasts the lifetime of the program) and it's type is char[](not char *) and its length is the size of the string literal with an appended zero. *Paragraph 6` says:

If the program attempts to modify such an array, the behavior is undefined.

So attempting to modify a string literal is undefined behavior regardless of the fact that they are not const.

With respect to argv in section 5.1.2.2.1 Program startup paragraph 2 says:

If they are declared, the parameters to the main function shall obey the following constraints:

[...]

-The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.

So argv is not considered an array of string literals and it is ok to modify the contents of argv.

like image 25
Shafik Yaghmour Avatar answered Sep 22 '22 08:09

Shafik Yaghmour