Can someone help me understand why A$ compiles with Visual Studio 2005 as an int variable with no compiler error?
Thanks,
Jeff
Code:main() { int A$; A$ = 17; }
Can someone help me understand why A$ compiles with Visual Studio 2005 as an int variable with no compiler error?
Thanks,
Jeff
Code:main() { int A$; A$ = 17; }
Well heck Jeff... Why not?
(And BTW... that version of VS is like 4 generations behind the times... VC++ 2010 is the big deal now)
You're maybe thinking of BASIC where A$ would be a string variable. But C has no string data type. Every string in C is a composite of a char variable, with an end of string marker char. This: BOB is just three char's, not a string. BOB'\0' is a string, but you don't see the '\0' char at all.
So a variable with a $ at the end of it's name is not indicative of a string or any certain type of variable, at all.
Thanks for the quick reply Tater.
The site C Programming/Variables - Wikibooks, open books for an open world states the following:
Naming VariablesVariable names in C are made up of letters (upper and lower case) and digits. The underscore character ("_") is also permitted. Names must not begin with a digit. Unlike some languages (such as Perl and some BASIC dialects), C does not use any special prefix characters on variable names.
A Book on C by Al Kelley and Ira Pohl states
A variable name, also called an identifier, consists of a seqence of letters, digits, and underscores, but may not start with a digit.
Nobody seems to talk about '$' being allowed in a C variable name.
I have VS 2010 on another computer but the one with 2005 has a nicer screen
Thanks,
Jeff
Excellent question! I'm taking a C Programming class and the instructor asked in a programming exercise. Technically, I just have to asnwer if the variable compiles correctly. It just bugs me that it compiles correctly, and I can assign values to it like any other variable, but the experts don't mention '$' in variable names.
Thanks,
Jeff
C99 actually defines what is an acceptable variable name. Look at these:
The C99 Draft (N869, 18 January, 1999)
The C99 Draft (N869, 18 January, 1999)
Technically, with the identifier A$, A is a nondigit and $ is a character that your environment must support. I also used C99 only because I have easy access to its standard and it is recent enough for me. Other versions of the standard have also explained the syntax in similar ways.
edit - OK, well I want you to look at sections A.1.3 and 5.2.1. I'm not sure why my links are ........ed.
Last edited by whiteflags; 09-10-2011 at 04:19 PM.
WhiteFlags,
I found section Annex J 5.2 of the C99 specification that states:
"Characters other than the underscore _, letters, and digits, that are not part of the basic
source character set (such as the dollar sign $, or characters in national character sets)
may appear in an identifier (6.4.2)."
This seems to answer the question. Thanks for the help!
Jeff