Identifiers, File Nomenclature, Keywords, Constants, Comments
The following topics are covered in this section:
Identifiers are very important in C++ and are used in all programs. What is an identifier? In the previous program ‘check’ was declared as a character variable. The identifier in this case is ‘check’. Basically identifiers are names that are given to variables, constants or functions. In the previous program there are two identifiers: ‘check’ and ‘i’.
C++ permits the use of a huge variety of names for identifiers like:
Almost any name you can think of can be used as an identifier but there are some restrictions. An identifier should not start with a number. The first character of an identifier should be an alphabet or an underscore ( _ ). After the first character, numbers can be used in the identifier. Remember the following:
Keywords are reserved words in C++ programming. They should not be used as identifiers and all keywords should be in lower case. The 63 keywords are tabulated below.
Asm | Auto | bool | break | case | Catch | char |
Class | 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 | Operator | 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 |
A few points regarding file nomenclature are:
File Type |
Extension used |
C++ source file |
*.cpp |
C++/C header file |
*.h |
C source file |
*.c |
Executable file |
*.exe |
Object Code |
*.obj |
Text file |
*.txt |
Word document |
*.doc |
Image/graphics file |
*.jpg/ *.gif/ *.bmp |
Constants are used in expressions and their values cannot be changed. They are used in the program code without using any variable name to refer to them. For example consider the statements:
int x = 5;
float y = 3.33;
In this, 5 is an integer constant and 3.33 is a floating type constant. You cannot change 5 or 3.33 but you can assign these values to variables (the variables in the above code fragment are ‘x’ and ‘y’).
Constants can be classified based on their data type as follows:
Numeric Constants:
Numeric constants consist of a series of digits. Integer constants can be written in different number systems: hexadecimal (base 16), octal (base 8) or in decimal (base 10).
2,34, 100, 900, 1456 etc.
023, 0567, 0214 etc.
0x10, 0x1FF etc.
Character and String Constants:
‘a’
‘b’
‘x’
‘1’
‘A’
‘*’
A single character enclosed within single quotes is a character constant. All character constants will have an integer value (determined from the ASCII table). A C++ statement:
char ch = ‘B’;
will assign the character constant ‘B’ to the character variable ch.
"hello"
"This is a string"
"x"
Even "x" is a string because it is enclosed in double quotes. "x" is different from ‘x’ (this is a character constant). The reason is because "x" actually consists of ‘x’ and ‘\0’ (the null character which will be explained later).
Beware: The constants described in this section are different from variables using the ‘const’ keyword (this is discussed in Unit 6).
Comments are not meant for the compiler to read. They are meant to make the program easier to understand for humans who might read the coding later. It is a good practice to write comments (even for small programs). What should be written in the comments? First of all, a couple of lines about the aim of the program should be mentioned. You should also mention the name of the programmer and the date when the program has been modified. Comments should be updated every time you modify the code. As the program gets larger, it is advisable to write comments for the various functions that are used in the program. For instance, if a function is used to calculate the sum of numbers, then this could be mentioned in the comments. Basically you are permitted to write anything within comments because it will not affect the program.
There are two methods used to denote comments:
For single line comments use double slashes (//) followed by the comments (but it should be on the same line as the double slash). Example:
//this is a single line comment
Multiple line commenting permits you to write line after line of comments. It will be as follows:
/* Multiple line commenting…
…this is also part of the comment…
……comment continued……
*/
Everything in between /* and */ will be commented. By using multiple line commenting the programmer needn’t use double slashes for each and every line.
Recap:
Copyright © 2004 Sethu Subramanian All rights reserved.