Variable Length

L

lak

I want to know what is the variable length in c.
I K&R they stated that atleast 31 character.
But I give 1 lakhs length to a variable, but my compiler doesn't say
any error.
It accepts it.Then what is the maximum length to a variable name.?
 
J

James Kuyper

lak said:
I want to know what is the variable length in c.
I K&R they stated that atleast 31 character.
But I give 1 lakhs length to a variable, but my compiler doesn't say
any error.
It accepts it.Then what is the maximum length to a variable name.?

The limit is on identifiers, not just variable names; function names,
for instance, are also identifiers.

The 31 character limit is only on external identifiers. Internal
identifiers have a limit of 63 (5.2.4p1).

That limit is a minimum, not a maximum. "There is no specific limit on
the maximum length of an identifier." (6.4.1p2).

The minimum limit is a requirement on the implementor, not on the
developer. It means that if two external identifiers differ in their
first 31 characters, an implementation is required to recognize them as
being different. If the first difference occurs after the 31st
character, an implementation is allowed, but not required, to treat the
two identifiers as being the same, which usually causes problems
somewhere during compilation, unless it was only a typo, in which case
it can cause confusion in readers of the code.

A good compiler should warn you if you have two external identifiers
that differ only after the 31st character. A picky compiler might warn
you about even using identifiers longer than 31 characters. That's a
useful warning, because human readers can be easily misled into thinking
the whole identifier is meaningful, rather than just the first 31
characters.
 
S

santosh

lak said:
I want to know what is the variable length in c.
I K&R they stated that atleast 31 character.
But I give 1 lakhs length to a variable, but my compiler doesn't say
any error.
It accepts it.Then what is the maximum length to a variable name.?

The minimum limits are given in the C Standard. Implementations may
relax these restrictions further as appropriate.

PS. An identifier of hundred thousand characters is nothing short of
mad. Was there a reason for your pointless exercise?
 
T

Tor Rustad

James Kuyper wrote:

[...]
The limit is on identifiers, not just variable names; function names,
for instance, are also identifiers.

The 31 character limit is only on external identifiers. Internal
identifiers have a limit of 63 (5.2.4p1).

I'm rather sure that limit was far less before C99.

I don't have access to C89 std. here, but if memory serves me right,
minimum of 8 initial characters was significant (external identifiers).
 
K

Kenny McCormack

The minimum limits are given in the C Standard. Implementations may
relax these restrictions further as appropriate.

PS. An identifier of hundred thousand characters is nothing short of
mad. Was there a reason for your pointless exercise?

You might want to Google the words "test" and "experiment".

You do get credit though for looking up and tranlating for us the Indian
numbering system.
 
S

santosh

Tor said:
James Kuyper wrote:

[...]
The limit is on identifiers, not just variable names; function names,
for instance, are also identifiers.

The 31 character limit is only on external identifiers. Internal
identifiers have a limit of 63 (5.2.4p1).

I'm rather sure that limit was far less before C99.

I don't have access to C89 std. here, but if memory serves me right,
minimum of 8 initial characters was significant (external
identifiers).

I think it was six initial characters and case insensitive too. A
concession to the limitations of some of the linkers back then.
 
J

James Kuyper

Tor said:
James Kuyper wrote:

[...]
The limit is on identifiers, not just variable names; function names,
for instance, are also identifiers.

The 31 character limit is only on external identifiers. Internal
identifiers have a limit of 63 (5.2.4p1).

I'm rather sure that limit was far less before C99.

I don't have access to C89 std. here, but if memory serves me right,
minimum of 8 initial characters was significant (external identifiers).

That sounds about right; the limit was expanded in C99 because it was
felt to be no longer necessary to accommodate old linkers that didn't
support names longer than 8 characters.

However, I can't verify the precise number under the old standard. When
I wanted a copy of the C90 standard, it was way too expensive. By the
time it dropped down to a reasonable price, it was no longer the current
standard, and I was therefore no longer interested in it.
 
M

Malcolm McLean

lak said:
I want to know what is the variable length in c.
I K&R they stated that atleast 31 character.
But I give 1 lakhs length to a variable, but my compiler doesn't say
any error.
It accepts it.Then what is the maximum length to a variable name.?
Typically a compiler will accept variables of any length until extreme
limits such as the amount of memory in the computer are hit.
However it is not obliged to accept more than 31 characters. That is a
concession to simple compilers. Nowadays the speed gain to be had from using
a fixed-size field is much less significant.
 
K

Keith Thompson

lak said:
I want to know what is the variable length in c.
I K&R they stated that atleast 31 character.
But I give 1 lakhs length to a variable, but my compiler doesn't say
any error.
It accepts it.Then what is the maximum length to a variable name.?

The C90 standard requires compilers to support a minumum of 31
significant initial characters in an internal identifier or macro
name, 6 in an external identifier. I recall that external identifiers
that differ only in case ("foo" vs. "Foo") are not required to be
treated as distinct, but in a quick look at the C90 standard I didn't
find a reference to that.

C99 increases this to 63 characters for internal identifiers, 31 for
external identifiers. I believe that identifiers differing only in
case are required to be treated as distinct, but again, I didn't find
a reference to that in the standard (I'm sure it's there, I just
didn't take the time to find it).

However, compilers aren't required to impose these restrictions; these
are just the minimum they're required to support. A compiler that
allows identifiers of any arbitrary length meets the standard's
requirements. Furthermore, even if the compiler imposes limits, the
limit is on the initial significant characters, not on the full length
of the identifier. For example, a compiler might allow both
this_is_a_very_very_long_identifier
and
this_is_a_very_very_long_identical identifier
as external identifiers, but treat them as the same (because they
match in the first 31 characters). A compiler may (but need not)
impose a limit of at least 509 characters (C90) or 4095 (C99)
characters in a logical source line, and an identifier can't span more
than one source line -- but again, a compiler isn't required to impose
any limit at all.

Incidentally, most people here aren't likely to know that "lakh" means
one hundred thousand; the word is rarely used outside India.
 
T

Tor Rustad

santosh said:
Tor Rustad wrote:

I think it was six initial characters and case insensitive too. A
concession to the limitations of some of the linkers back then.

Yes, 6 significant initial characters it was.

I beleave one of our major production systems, still have that
limitation. This limit resulted in some terrible naming conventions.
 
M

Malcolm McLean

Tor Rustad said:
Yes, 6 significant initial characters it was.

I beleave one of our major production systems, still have that
limitation. This limit resulted in some terrible naming conventions.
The rule of six.
 
C

CBFalconer

James said:
The limit is on identifiers, not just variable names; function
names, for instance, are also identifiers.

The 31 character limit is only on external identifiers. Internal
identifiers have a limit of 63 (5.2.4p1).

That limit is a minimum, not a maximum. "There is no specific
limit on the maximum length of an identifier." (6.4.1p2).

That minimum, in C, depends on the standard in effect. For C89,
C90, C95 I believe it is smaller. However, in practice you can
always depend on 10 to 16 chars barring the use of ancient
compilers and linkers. Use of longer names is generally foolish.
 
J

James Kuyper

CBFalconer said:
James Kuyper wrote: ....

That minimum, in C, depends on the standard in effect. For C89,
C90, C95 I believe it is smaller. However, in practice you can
always depend on 10 to 16 chars barring the use of ancient
compilers and linkers. Use of longer names is generally foolish.

All of my statements about the standard default to being about the
current version, unless specified otherwise (which I seldom do). I only
worry about what older versions said when I'm required to write code
which conforms to them. Even when programming for C90, I only worry
about identifiers longer than the older limit because they're a pain to
type and make the code hard to read, not because I'm worried about their
uniqueness on modern compilers. If it causes problems, I'll switch
compilers, not my source code.

I'm very concerned about portability, but only in the forward sense;
insofar as future versions of the standard remain backward compatible
with the version of the C standard I'm currently writing for, I intend
to write my code to be compatible with those future versions. I intend
to make that true, no matter how much advantage they take of the things
that are unspecified in the current standard in order to claim backward
compatibility. I realize that backward compatibility can be equally
important in some contexts, but for my code it's just not worth the trouble.
 
D

David Thompson

The C90 standard requires compilers to support a minumum of 31
significant initial characters in an internal identifier or macro
name, 6 in an external identifier. I recall that external identifiers
that differ only in case ("foo" vs. "Foo") are not required to be
treated as distinct, but in a quick look at the C90 standard I didn't
find a reference to that.
6.1.2 under Implementation Limits
C99 increases this to 63 characters for internal identifiers, 31 for
external identifiers. I believe that identifiers differing only in
case are required to be treated as distinct, but again, I didn't find
a reference to that in the standard (I'm sure it's there, I just
didn't take the time to find it).
It's no longer there explicitly; just the general statement in 6.4.2.1
that any difference within the limit (31 or 63) is a different
identifier, WITHOUT an exception for case-folding.
However, compilers aren't required to impose these restrictions; <snip>

- formerly david.thompson1 || achar(64) || worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top