943,274 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 37808
  • C++ RSS
Nov 16th, 2003
0

C++ Identifiers and Keywords

Expand Post »

In C++ we provide names for the entities we create, the variables, functions and types in our programs. These names, or identifiers, are required to conform to some simple rules.

Valid Characters

An identifier must start with a letter and is comprised of a sequence of letters and digits. Somewhat surprisingly, in this context the underscore _ is considered to be a letter (although there are conditions associated with its use). There's no restriction on the length of an identifier.

Use of Underscore

Identifiers beginning with an underscore followed by an upper case letter are reserved for use by the implentation, as are identifiers beginning with a two consecutive underscores, so to avoid problems you should refrain from using them for your own identifiers.

Keywords

You can't use an identifier that is a C++ keyword in your programs. Keywords are:
and, and_eq, asm, auto, bitand, bitor, bool, break, case, catch, char, class, compl, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, not, not_eq, operator, or, or_eq, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while, xor, xor_eq.

You should also try to avoid using names from the C++ library, e.g. swap, max.

Case Sensitivity

C++ is case sensitive, so upper case and lower case characters are distinct. This means that the names userInput and userinput are recognised as two different identifiers.

Examples

Examples of acceptable identifiers are:
calculate_height, readWindSpeed, channel42, foo, BAR

Examples of unacceptable identifiers:
calculate height, delete, 2letters, _HELLO_

Hints and Tips

Use meaningful descriptive names to make your code more easily understood,

e.g.
int height rather than int h,
char menuSelection rather then char ch,
int patient_age rather than int number.

Avoid names that differ only in capitalisation
e.g. height, Height.

Avoid using names that are keywords but with different capitalisation
e.g. Return, Continue.

Try to maintain a consistent style
Examples are:

- Capitalising all words except the first,
e.g. readInUserData(), getMaximumSpeed().

- Using underscore to separate words,
e.g. read_in_user_data(), get_maximum_speed().

- Using a type identifier prefix,
e.g. int nGetSomeIntegerValue, char szDataString[].
Similar Threads
Bob
Team Colleague
Reputation Points: 15
Solved Threads: 2
Junior Poster
Bob is offline Offline
129 posts
since Feb 2003
Aug 5th, 2005
0

Re: C++ Identifiers and Keywords

This tutorial is very helpful for beginning programmers because it provides a summary of keywords.

Also it is nice to have a simple referance on the rules for vriable names.


-----------------
Programming ( Assignment / Project ) Help
Reputation Points: 10
Solved Threads: 0
Light Poster
proghelper is offline Offline
28 posts
since Jun 2005
Aug 16th, 2005
0

Re: C++ Identifiers and Keywords

This is one good intro to programming using C ++, and just to add please get an emphasis of using the surfixes ie

int Age_integer // age declared as an integer

or

char Name_character[10] // name as an array of 10 characters


becasue it makes it easier when doing the coding as you dont have to go back to the beggining to find out the type of your identifiers
Reputation Points: 10
Solved Threads: 0
Newbie Poster
j198116 is offline Offline
10 posts
since Jul 2005
Apr 20th, 2010
-2
Re: C++ Identifiers and Keywords
When can you use a $ in a variable?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jdumaine is offline Offline
1 posts
since Apr 2010
Apr 20th, 2010
0
Re: C++ Identifiers and Keywords
Click to Expand / Collapse  Quote originally posted by jdumaine ...
When can you use a $ in a variable?
You can't. That's why it wasn't mentioned.
Featured Poster
Reputation Points: 833
Solved Threads: 392
Posting Maven
Fbody is offline Offline
2,846 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Please help:Finding 2nd smallest number
Next Thread in C++ Forum Timeline: Pointer and functions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC