f



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

Dear all,

Why C and some of other languages dont allow the name of a variable
start with a digit?
0
4/11/2008 4:42:37 AM
comp.lang.c 30657 articles. 4 followers. spinoza1111 (3246) is leader. Post Follow

12 Replies
285 Views

Similar Articles

[PageSpeed] 31

Jason20005 said:

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

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
0
rjh (10791)
4/11/2008 4:59:54 AM
Richard Heathfield <rjh@see.sig.invalid> writes:

>Jason20005 said:

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

>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;

-- 
Chris.
0
chris16 (579)
4/11/2008 5:07:07 AM
On Apr 11, 12:59 pm, Richard Heathfield <r...@see.sig.invalid> wrote:
> Jason20005 said:
>
> > Dear all,
>
> > Why C and some of other languages dont allow the name of a variable
> > start with a digit?
>
> 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.
0
4/11/2008 5:10:13 AM
Chris McDonald said:

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

Yes, I know, but I figured (correctly, as it turns out) that using an 
all-digit example would bring the point home to the OP very quickly.

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

0X0 also springs to mind.

Forgive me - this is supposed to be a non-commercial channel.

0x20.

There - that's better.

-- 
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
0
rjh (10791)
4/11/2008 5:50:31 AM
Richard Heathfield wrote:
> Jason20005 said:
> > Dear all,
> >
> > Why C and some of other languages dont allow the name of
> > a variable start with a digit?
>
> 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. ;-)

--
Peter
0
airia (1802)
4/11/2008 6:08:40 AM
"Jason20005" <jason20005@gmail.com> wrote in message 
news:79d0b2ff-cd84-4121-b418-f219c086db98@h1g2000prh.googlegroups.com...
> 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 ___.

-- 
Bart 


0
bc (2337)
4/11/2008 9:23:05 AM
Chris McDonald wrote:
> Richard Heathfield <rjh@see.sig.invalid> writes:
> 
>> Jason20005 said:
> 
>>> Dear all,
>>>
>>> Why C and some of other languages dont allow the name of a variable
>>> start with a digit?
> 
>> 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;

     Try a few small adjustments to Richard's example:

	#include <stdio.h>
	int main(void) {
	    int 0x42 = 6;
	    double 42e0 = 6;
	    double 42e = 6;
	    printf ("%d %g %g\n", 0x42, 42e0, 42e-1);
	    return 0;
	}

Now what?

-- 
Eric Sosman
esosman@ieee-dot-org.invalid
0
esosman2 (3096)
4/11/2008 11:53:18 AM
On Fri, 11 Apr 2008 10:23:05 +0100, Bartc <bc@freeuk.com> wrote:
> More useful (to me anyway) would have been the ability to use $ in an
> identifier

Indeed. VAX/VMS's DCL allowed dollar-signs, e.g. F$SEVERITY.

-- 
Martin

0
4/11/2008 11:29:14 PM
On Apr 10, 9:59=A0pm, Richard Heathfield <r...@see.sig.invalid> wrote:
> Jason20005 said:
>
> > Dear all,
>
> > Why C and some of other languages dont allow the name of a variable
> > start with a digit?
>
> Assume C *did* allow it. What should the output of this program be?
>
> #include <stdio.h>
>
> int main(void)
> {
> =A0 int 42 =3D 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.
0
kkylheku (2498)
4/12/2008 12:42:17 AM
Martin wrote:
> On Fri, 11 Apr 2008 10:23:05 +0100, Bartc <bc@freeuk.com> wrote:
>> More useful (to me anyway) would have been the ability to use $ in an
>> identifier
> 
> Indeed. VAX/VMS's DCL allowed dollar-signs, e.g. F$SEVERITY.

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

-- 
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
0
markmcintyre (4555)
4/12/2008 10:01:02 PM
On Apr 12, 3:01=A0pm, Mark McIntyre <markmcint...@spamcop.net> wrote:
> Martin wrote:
> > On Fri, 11 Apr 2008 10:23:05 +0100, Bartc <b...@freeuk.com> wrote:
> >> More useful (to me anyway) would have been the ability to use $ in an
> >> identifier
>
> > Indeed. VAX/VMS's DCL allowed dollar-signs, e.g. F$SEVERITY.
>
> 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.
0
cdalten (975)
4/13/2008 2:22:55 AM
Mark McIntyre <markmcintyre@spamcop.net> writes:
> Martin wrote:
>> On Fri, 11 Apr 2008 10:23:05 +0100, Bartc <bc@freeuk.com> wrote:
>>> More useful (to me anyway) would have been the ability to use $ in an
>>> identifier
>>
>> Indeed. VAX/VMS's DCL allowed dollar-signs, e.g. F$SEVERITY.
>
> 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.

-- 
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
kst-u (21963)
4/13/2008 2:31:45 AM
Reply: