Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is max length of C string literal different from max char[]?

Tags:

Clarification: Given that a string literal can be rewritten as a const char[] (see below), imposing a lower max length on literals than on char[]s is just a syntactic inconvenience. Why does the C standard encourage this?


The C89 standard has a translation limit for string literals:

509 characters in a character string literal or wide string literal (after concatenation)

There isn't a limit for a char arrays; perhaps

32767 bytes in an object (in a hosted environment only)

applies (I'm not sure what object or hosted environment means), but at any rate it's a much higher limit.

My understanding is that a string literal is equivalent to char array containing characters, ie: it's always possible to rewrite something like this:

const char* str = "foo";

into this

static const char __THE_LITERAL[] = { 'f', 'o', 'o', '\0' };
const char* str = __THE_LITERAL;

So why such a hard limit on literals?

like image 959
npostavs Avatar asked Jul 15 '12 01:07

npostavs


People also ask

What is the maximum length of C string in character?

The maximum length of a string literal allowed in Microsoft C is approximately 2,048 bytes.

What is the maximum length of a string variable?

A character-string value is a sequence of characters. The number of characters in a sequence is called the length of the sequence. In Open PL/I, the maximum length of a string value is 32767 bytes or characters. A character-string of zero length is called a null string.

What is the maximum length of C string Mcq?

10) What is the maximum length of a C String.? Explanation: Maximum size of a C String is dependent on implemented PC memory. C does not restrict C array size or String Length.


2 Answers

The limit on string literals is a compile-time requirement; there's a similar limit on the length of a logical source line. A compiler might use a fixed-size data structure to hold source lines and string literals.

(C99 increases these particular limits from 509 to 4095 characters.)

On the other hand, an object (such as an array of char) can be built at run time. The limits are likely imposed by the target machine architecture, not by the design of the compiler.

Note that these are not upper bounds imposed on programs. A compiler is not required to impose any finite limits at all. If a compiler does impose a limit on line length, it must be at least 509 or 4095 characters. (Most actual compilers, I think, don't impose fixed limits; rather they allocate memory dynamically.)

like image 90
Keith Thompson Avatar answered Sep 18 '22 16:09

Keith Thompson


It's not that 509 characters is the limit for a string, it's the minimum required for ANSI compatibility, as explained here.

I think that the makers of the standard pulled the number 509 out of their ass, but unless we get some official documentation from this, there is no way for us to know.

As far as how many characters can actually be in a string literal, that is compiler-dependent.

Here are some examples:

  • MSVC: 2048
  • GCC: No Limit (up to 100,000 characters), but gives warning after 510 characters:

    String literal of length 100000 exceeds maximum length 509 that C90 compilers are required to support

like image 39
Richard J. Ross III Avatar answered Sep 20 '22 16:09

Richard J. Ross III