|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Dear all,
Why C and some of other languages dont allow the name of a variable start with a digit? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
"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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
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> |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
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" |
|
![]() |
| Outils de la discussion | |
|
|