variable names question

Jun 15, 2011 at 11:15pm
closed account (jhpz3TCk)


I was wondering if something in all caps is a valid variable name
like

AONE or PRINT

Also, if periods can be put in something like

a.2.1

Thanks!
Last edited on Jun 15, 2011 at 11:33pm
Jun 16, 2011 at 12:00am
Yes and no - variables can be in all caps, though it's convention that they stand for constant variables (const). Periods have other uses (i.e. object members).
Jun 16, 2011 at 5:41am
By convention, something in all caps is either:
1. a macro,
#define ABS(x) ((x)>=0?(x):-(x))
2. less often, an enum member,
1
2
3
4
enum Bool{
    TRUE,
    FALSE
};
3. or, even less often, an actual const.
const double SQRT2=1.4142135623730950488016887242097;

It's not possible to use periods in identifiers (an identifier is a name for a variable, a function, a type, etc.). In fact, valid identifiers contain only Latin alphabet letters in upper or lower case, Arabic numerals 0-9, or underscores (_), and do not begin with a numeral.
By convention, identifiers beginning with one or more underscores are used by the language implementation (for example, C symbols are traditionally exported by prepending an underscore to the name), so avoid naming things beginning with an underscore.
Jun 16, 2011 at 9:15am
Doesn't C++ allow variable identifiers to contain '$' characters also? As in int $_; or double x$[10];
Last edited on Jun 16, 2011 at 9:15am
Jun 16, 2011 at 1:57pm
This might be a little more than you asked but for the sake of completeness, there are various standards in use, none more (in)famous than Hungarian Notation.

More info here: http://en.wikipedia.org/wiki/Hungarian_notation

The basic premise is that the first few characters of the variable denote it's type (among other things).

Also companies and academic institutions may distribute a Programming Standard that outlines various conventions on variable naming.

So as personak so succinctly put it:
personak wrote:
Yes and no
Jun 16, 2011 at 2:53pm
Doesn't C++ allow variable identifiers to contain '$' characters also?


No. See helios's reply.
Last edited on Jun 16, 2011 at 2:54pm
Topic archived. No new replies allowed.