written by: Noel Kalicharan•edited by: J. F. Amprimoz•updated: 5/23/2011
In this article, we continue to explain some basic concepts required in C programming. First, we describe the C reserved words. We then give the rules to follow in making up "user identifiers" in C. We end with some naming conventions used in this series.
slide 1 of 4
The C language uses a number of keywords such as int, char and while. A keyword has a special meaning in the context of a C program and can be used for that purpose only. For example, int can be used onlyin those places where we need to specify that the type of some item is integer.
All keywords are written in lowercase letters only. Thus int is a keyword but Int and INT are not. Keywords are reserved, that is, you cannot use them as your identifiers. As such, they are usually called reserved words. A list of C keywords is given here.
slide 2 of 4
The C programmer needs to make up names for things such as variables, function names and symbolic constants (see next section). A name that he makes up is called a user identifier. There are a few simple rules to follow in naming an identifier:
•it must start with a letter or underscore;
•if other characters are required, they can be any combination of letters, digits or underscore;
• the length of an identifier cannot exceed 63 characters. Note: earlier versions of C only allowed up to 31 characters.
Examples of valid identifiers:
Examples of invalid identifiers:
2hotToHandle// does not start with a letter
Net Pay// contains a space
ALPHA;BETA// contains an invalid character ;
Important points to note:
•Spaces are not allowed in an identifier. If you need one which consists of two or more words, use a combination of uppercase and lowercase letters (as in numThrowsThisTurn) or use the underscore to separate the words (as in num_throws_this_turn). We prefer the uppercase/lowercase combination.
•In general, C is case-sensitive (an uppercase letter is considered different from the corresponding lowercase letter). Thus r is a different identifier from R. And sum is different from Sum is different from SUM is different from SuM.
•You cannot use a C reserved word as one of your identifiers.
slide 3 of 4
Some naming conventions
Other than the rules for creating identifiers, C imposes no restriction on what names to use, or what format (uppercase or lowercase, for instance) to use. However, good programming practice dictates that some common-sense rules should be followed.
An identifier should be meaningful. For example, if it’s a variable, it should reflect the value being stored in the variable; netPay is a much better variable than x for storing someone’s net pay, even though both are valid. If it’s a function, it should give some indication of what the method is supposed to do; playGame is a better identifier than plg.
It is a good idea to use upper and lower case combinations to indicate the kind of item named by the identifier. In this series, we use the following conventions:
•A variable is normally written in lowercase, for example, sum. If we need a variable consisting of two or more words, we start the second and subsequent words with an uppercase letter, for example, voteCount or sumOfSeries.
•A symbolic (or named) constant is an identifier which can be used in place of a constant such as 100. Suppose 100 represents the maximum number of items we wish to process in some program. We would probably need to use the number 100 in various places in the program. But suppose we change our mind and want to cater for 500 items. We would have to change all occurrences of 100 to 500. However, we would have to make sure that we do not change an occurrence of 100 used for some purpose other than the maximum number of items (in a calculation like principal*rate/100, say).
To make it easy to change our mind, we can set the identifier MaxItems to 100 and use MaxItems whenever we need to refer to the maximum number of items. If we change our mind, we would just need to set MaxItems to the new value. We will begin a symbolic constant with an uppercase letter. If it consists of more than one word, we will begin each word with uppercase, as in MaxThrowsPerTurn.