4

I'm not sure whether this is a property of the compiler or something standard to C. I've heard of limits on the number of characters long a variable or function can be (e.g. 25characters).

I'm not there yet, but just saw a decently long function and began to think about it. Is there a limit I should know about?

tarabyte
  • 3,112
  • 10
  • 43
  • 67

3 Answers3

4

IIRC, most C compilers have a limit of 255 characters for an identifier. You usually find all the information you need in the compiler's manual. In your case it's the MPLAB XC8 C Compiler User’s Guide:

3.4.4.3 HOW LONG CAN I MAKE MY VARIABLE AND MACRO NAMES?

The C Standard indicates that a only a specific number of initial characters in an identifier are significant, but it does not actually state what this number is and it varies from compiler to compiler. For XC8, the first 255 characters are significant, but this can be reduced using the -Noption; see Section 4.8.8 “-N: Identifier Length”. The fewer characters there are in your variable names, the more portable your code. Using the -Noption allows the compiler to check that your identifiers conform to a specific length. This option affects variable and function names, as well as preprocessor macro names.

If two identifiers only differ in the non-significant part of the name, they are considered to represent the same object, which will almost certainly lead to code failure.

Note that it's not a good practice to have long identifier names. It makes your code harder to read & understand.

m.Alin
  • 10,638
  • 19
  • 62
  • 89
2

Any limitation to the length of the identifiers is implementation-dependent, but the first 31 characters should be significant in a conforming implementation.

To make that clear, it's okay to allow 4000-character identifiers, but have Pseudopseudohypoparathyroidism_1 (silently, involuntarily) refer to the same variable as Pseudopseudohypoparathyroidism_2.

Spehro Pefhany
  • 376,485
  • 21
  • 320
  • 842
2

It is compiler dependent, see the compiler's manual. There are standards, but it's better to check the compiler's manual. For GCC it says the name can be "arbitrarily long", so no limit.

Long variable names are a good idea if they are descriptive. Something like "USER_prepare_new_user_log()" tells you the function is part of the "USER" file/subsystem and the rest is descriptive and easy to read. Some people use things like Camel Case but I prefer underscores for readability.

user
  • 1,789
  • 10
  • 20