PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Why C dont allow the name of a variable start with a digit?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Why C dont allow the name of a variable start with a digit?

Réponse
 
LinkBack Outils de la discussion
Vieux 11/04/2008, 05h42   #1
Jason20005
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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?
  Réponse avec citation
Vieux 11/04/2008, 05h59   #2
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why C dont allow the name of a variable start with a digit?

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
  Réponse avec citation
Vieux 11/04/2008, 06h07   #3
Chris McDonald
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why C dont allow the name of a variable start with a digit?

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.
  Réponse avec citation
Vieux 11/04/2008, 06h10   #4
Jason20005
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why C dont allow the name of a variable start with a digit?

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.
  Réponse avec citation
Vieux 11/04/2008, 06h50   #5
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why C dont allow the name of a variable start with a digit?

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
  Réponse avec citation
Vieux 11/04/2008, 07h08   #6
Peter Nilsson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why C dont allow the name of a variable start with a digit?

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
  Réponse avec citation
Vieux 11/04/2008, 10h23   #7
Bartc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why C dont allow the name of a variable start with a digit?


"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


  Réponse avec citation
Vieux 11/04/2008, 12h53   #8
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why C dont allow the name of a variable start with a digit?

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
  Réponse avec citation
Vieux 12/04/2008, 00h29   #9
Martin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why C dont allow the name of a variable start with a digit?

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

  Réponse avec citation
Vieux 12/04/2008, 01h42   #10
Kaz Kylheku
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why C dont allow the name of a variable start with a digit?

On Apr 10, 9:59pm, 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;


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.
  Réponse avec citation
Vieux 12/04/2008, 23h01   #11
Mark McIntyre
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why C dont allow the name of a variable start with a digit?

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>
  Réponse avec citation
Vieux 13/04/2008, 03h22   #12
Chad
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why C dont allow the name of a variable start with a digit?

On Apr 12, 3:01pm, 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.
  Réponse avec citation
Vieux 13/04/2008, 03h31   #13
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why C dont allow the name of a variable start with a digit?

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"
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h52.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,25473 seconds with 21 queries