How to Program in C for the Beginner: C Reserved Words, Identifiers And Naming Conventions

How to Program in C for the Beginner: C Reserved Words, Identifiers And Naming Conventions
Page content

Reserved words

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 only in 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.

Identifiers

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:

r

R

sumOfRoots1and2

_XYZ

maxThrowsPerTurn

TURNS_PER_GAME

R2D2

root1

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.

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.

References

References: C Programming – A Beginner’s Course

Related programming references

This post is part of the series: C Programming for Beginners

A straightforward introduction to Programming in C for people with no previous programing experience.

  1. C Programming For Beginners - Part 1
  2. C Programming For Beginners - Part 2
  3. C Programming for Beginners – Part 3
  4. C Programming for Beginners – Part 4
  5. C Programming For Beginners - Part 5
  6. C Programming For Beginners - Part 6
  7. C Programming For Beginners - Data Types
  8. C Programming For Beginners - Part 8
  9. C Programming For Beginners - Part 9
  10. C Programming For Beginners - Part 10
  11. C Programming For Beginners - Part 11
  12. C Programming For Beginners - Part 12
  13. C Programming For Beginners - Part 13
  14. C Programming For Beginners - Part 14
  15. C Programming For Beginners - Integer Data Types
  16. C Programming for Beginners - Part 16
  17. C Programming For Beginners - Integer Expressions, Operators and Precedence
  18. C Programming For Beginners - Part 18
  19. C Programming For Beginners - Printing Double and Float
  20. C Programming For Beginners - Mixing double, float and int