Why C dont allow the name of a variable start with a digit?

J

Jason20005

Dear all,

Why C and some of other languages dont allow the name of a variable
start with a digit?
 
C

Chris McDonald

Richard Heathfield said:
Jason20005 said:
Assume C *did* allow it. What should the output of this program be?
#include <stdio.h>
int main(void)
{
int 42 = 6;
printf("%d\n", 42);
return 0;
}
What should be printed, and why?


The question was actually about *starting* with a digit,
which doesn't necessarily imply consisting *only* of digits.

But what about: long int 42L = 6;
 
J

Jason20005

Jason20005 said:



Assume C *did* allow it. What should the output of this program be?

#include <stdio.h>

int main(void)
{
int 42 = 6;
printf("%d\n", 42);
return 0;

}

What should be printed, and why?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


Thanks very much.

Now I understand the reason behind the variable naming restriction.
 
P

Peter Nilsson

Richard said:
Jason20005 said:

Assume C *did* allow it. What should the output of this program be?

#include <stdio.h>

int main(void)
{
int 42 = 6;
printf("%d\n", 42);
return 0;
}

What should be printed, and why?

6. Because numeric tokens would be considered as identifiers
before being considered as numbers. [This is how Forth operates.]

Of course, I'm not saying C _should_ do this. It would make
for interesting ioccc entries though. ;-)
 
B

Bartc

Jason20005 said:
Dear all,

Why C and some of other languages dont allow the name of a variable
start with a digit?

It's been explained there could be ambiguity between integer constants and
variables.

It might have been feasible where there was no ambiguity (so 0XABC must be a
constant, but 0XABG must an identifier), but it's unsatisfactory. Some other
way of distinguishing constants and identifiers would be needed, and this
would offset some advantage of starting with a digit.

More useful (to me anyway) would have been the ability to use $ in an
identifier (available on some Cs but not standard). Then it could have been
used in place of _, which is difficult to see and can be confused with __
and ___.
 
K

Kaz Kylheku

Jason20005 said:



Assume C *did* allow it. What should the output of this program be?

#include <stdio.h>

int main(void)
{
  int 42 = 6;

If C allowed digits at the beginning of an identifier, the above
program could obviously not be the interface the feature by any
reasonable stretch.

Someone sane would have to consider that requirement and come up with
a way of extending the language to support it.

Common Lisp allows arbitrary symbols in identifiers. Not all possible
identifiers can simply be written flat out, however. An escape
notation may have to be used to express some of them. For instance, a
symbol whose name is the digits 12345 would not just be written 12345,
because a token of all-digits is considered an integer literal.
However, only unescaped digits have the meaning of ``digit''. So
escaping just one of the digits makes it into a symbol: \12345, or
1\2345 et cetera, because in all these instances, the token is no
longer made up entirely of digits. Sequences of two or more characters
can be escaped as a group with vertical bars, rather than individually
with the backslash: |1|2345 or just |12345|. Most Lispers would write
it as |12345|, and that's probably how most Lisp would print the
symbol whose name is the string "12345".

A C dialect that allows arbitrary characters in symbols could also
make use of some such escaping convention, without breaking
compatibility.
 
C

Chad

Still does allow it. So does VaxC if I recall correctly.

--


I thought VMS was the actual operating system and that the VAX was the
corresponding hardware. I vaguely remember one of the labs at HP
having FreeBSD running on a VAX.
 
K

Keith Thompson

Mark McIntyre said:
Still does allow it. So does VaxC if I recall correctly.

Last time I used it, yes.

Whether DCL allows dollar signs isn't particularly relevant; that's
the command language, equivalent to a shell on Unix-like systems. But
the VMS (now OpenVMS) dialect(s) of C does allow dollar signs in
identifiers, used mostly for calling native system routines that have
dollar signs in their names. gcc also supports this extension.

(The two major C compilers for VAX/VMS are VAXC and DECC; the latter
is also supported on Alpha/VMS.)

Of course this is non-standard.
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top