6

Is there a maximum length for a statement defined in C?

compiler is gcc

4
  • 2
    I realize you asked for C; the C++ spec offers a guideline that a logical line should support at least 65535 characters.
    – Joe
    Jul 23, 2012 at 14:39
  • Did you encounter a long statement that was rejected by gcc? It's probably a bug, then.
    – bitmask
    Jul 23, 2012 at 14:39
  • According to C11 standard, it's 4095 characters. But I hope that you never reach this limit (this is not readable at all).
    – md5
    Jul 23, 2012 at 14:42
  • @md5: there's a 4095 character limit on a logical line, but a statement is not limited to being on a single logical line. Nov 7, 2023 at 21:33

2 Answers 2

11

The standard specifies a minimum translation limit:

5.2.4.1 Translation limits

1 The implementation shall be able to translate and execute at least one program that contains at least one instance of every one of the following limits:

[...]

— 4095 characters in a logical source line

The standard recommends however that "implementations should avoid imposing fixed translation limits whenever possible".

A logical source line is the result of backslash-newline splicing (5.1.1.2:1.2), so this doesn't directly cover the minimum limit for the number of characters in a statement.

However, because another translation limit requirement is:

— 4095 characters in a string literal (after concatenation)

and as string literals may only occur in statements, it follows that the implementation must accept at least one 4095-character statement.

Ignoring literals, we also see:

— 127 arguments in one function call

and as a function call must include at least 2 characters per argument, plus 4 for the invocation (f(0,0,...);) we arrive at a minimum line length limit of 260.

2
  • 2
    "string literals may only occur in statements" - that isn't true, for example const char *foo = "hello, world"; is a declaration, not a statement. But it doesn't really matter, since the lower bound on statement length that you derived from it is nominal anyway. Jul 23, 2012 at 15:07
  • Guessing a sequence of statements with continuations must also be 4096 bytes (or less) in toto, not each. Dec 28, 2016 at 11:54
7

The C standard lays down a variety of lower bounds on the complexity of the language that must be accepted by a compiler, but not a limit on the length of a statement per se.

§5.2.4.1 Translation limits

The implementation shall be able to translate and execute at least one program that contains at least one instance of every one of the following limits:18)

  • 127 nesting levels of blocks
  • 63 nesting levels of conditional inclusion
  • 12 pointer, array, and function declarators (in any combinations) modifying an arithmetic, structure, union, or void type in a declaration
  • 63 nesting levels of parenthesized declarators within a full declarator
  • 63 nesting levels of parenthesized expressions within a full expression
  • 63 significant initial characters in an internal identifier or a macro name (each universal character name or extended source character is considered a single character)
  • 31 significant initial characters in an external identifier (each universal character name specifying a short identifier of 0000FFFF or less is considered 6 characters, each universal character name specifying a short identifier of 00010000 or more is considered 10 characters, and each extended source character is considered the same number of characters as the corresponding universal character name, if any)19)
  • 4095 external identifiers in one translation unit
  • 511 identifiers with block scope declared in one block
  • 4095 macro identifiers simultaneously defined in one preprocessing translation unit
  • 127 parameters in one function definition
  • 127 arguments in one function call
  • 127 parameters in one macro definition
  • 127 arguments in one macro invocation
  • 4095 characters in a logical source line
  • 4095 characters in a string literal (after concatenation)
  • 65535 bytes in an object (in a hosted environment only)
  • 15 nesting levels for #included files
  • 1023 case labels for a switch statement (excluding those for any nested switch statements)
  • 1023 members in a single structure or union
  • 1023 enumeration constants in a single enumeration
  • 63 levels of nested structure or union definitions in a single struct-declaration-list

18) Implementations should avoid imposing fixed translation limits whenever possible.

19) See ‘‘future language directions’’ (6.11.3).

§6.11.3 External names

Restriction of the significance of an external name to fewer than 255 characters (considering each universal character name or extended source character as a single character) is an obsolescent feature that is a concession to existing implementations.

As you can see, footnote 18 would discourage the imposition of such a restriction.

Having said all that, I'd hate to see a single statement that encroached on the limits. It had better be machine-generated and not human written.

3
  • 4
    /me goes to write a program that reaches each of these limits.
    – lynks
    Jul 23, 2012 at 14:49
  • 1
    @lynks I want to see the source code of that! You've been at it for nearly 6 years by now! ;-)
    – Fabby
    May 3, 2018 at 22:01
  • 1
    @Fabby legend has it he's still going
    – GuillemVS
    Sep 28, 2023 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.