C++ tutorial, C++ programs, C++ Exercises, C++ Example Programs

Thursday, April 12, 2012

Naming Rules For C++ Variables

What is a Variable Name


As we have come to know in a previous post on C++ Variables, that a Variable is a named memory location to store some program data temporarily. "Named Memory Location" tells us that every variable in C++ has a variable name to refer the data stored in it.

Naming Rules To Write C++ variable Names

If we use a variable in a C++ program, we will declare this variable. To declare a C++ variable we will have to use a distinct name for each and every variable. We have to obey the following naming rules to write variable names in C++ programming.

    Start With Alphabets or Underscore

  1. The variable name will start with an alphabet or an Underscore.
  2. No Blank Space

  3. There should be no blank spaces in a variable name.
  4. No Special Characters

  5. Special characters like +, *, &, ^, % cannot be used in variable names.
  6. No Reserved / Keywords

  7. Reserved Words or Keywords cannot be used as variable names. Reserved words or keywords are the words used in C++ language for specific purpose. For example, int or for or while are reserved words in C language and C++ programming language, so programmers are not allowed to use these words as variable names in a program.
  8. Up to 31 characters long

  9. Although the maximum length of a variable name is compiler dependent in C++, several C++ compilers allow up to 31 characters long variable names. If anyone uses more than 31 characters for a variable name, extra characters are ignored by the C++ compiler.
  10. Short and Precise

  11. Variable names should be short and precise. Don't use abbreviations that may not self explanatory. Use abbreviations like RollNo in spite of RNo or RN or R or Roll. Abbreviations should be meaningful that may express the purpose of the use of a variable, clearly. 
  12. Naming Conventions

  13. Use uniform naming conventions in your program. For example: Use of RollNumb as a variable name states that the programmer will use first 4 characters for each word in a variable name. Secondly, each word in a variable name will be capital letter. If you follow uniform naming conventions for variables, writing, changing and maintaining your programs will be much easier. In addition, other programmers will find it easy to understand your programs.
  14. Unique Names

  15. Variable names must be unique in a program. A variable name can be used only for one data type. 
  16. Meaningful Names

  17. Use meaningful names that can explain the purpose of using the variable and type of data it can hold. 

Variables in C++
1 Explain
Variables in Programming C++
2 Naming
Rules For C++ Variables
Share:

0 comments:

Post a Comment

We Love To Hear From You!

EasyCPPprogramming.blogspotcom

Labels