C++ Variables & Constants
C++ variables are used to store any type of values within the program and these values can be changed during the program execution. The C++ variables are categorized on the basis of their memory requirements and storing capability. Variables are the name of memory locations and data type is the allocation of the name provided to the variables.
The various types of variables with a short description are listed below:
|1.||bool||Stores the value that is either true or false.|
|2.||int||Stores the exact whole number(not a fractional number).|
|3.||char||Typically a single octet (one byte). This is an integer type.|
|4.||float||A single-precision floating point value.|
|5.||double||A double-precision floating point value.|
|6.||void||An absence of return type.|
|7.||wchar_t||Represents a wide character.|
Variable Definition in C++
The variable definition in C++ is a method to indicate the compiler to assign storage in a memory location. The syntax of defining variables is given below:
There can be a number of variables. The syntax for more than one variables can be:
The data_type above is the valid C++ data types that include bool, int, char, float, double,wchar_t and variable list is the name of a variable separated by commas.
Variable Definition Examples
int x,y,z; char letter; float salary; double d;
Variable Declaration in C++
Variables have been defined above but the value of the variable is not assigned. The variable declaration means to assign the value along with the variable definition. The syntax of variable declaration in C++ is shown below:
data_type variable_name = value;
Variable Declaration Examples
int x=4,y,z=12; char letter="ABC"; float salary=225.50; double d;
Rules of Declaring Variables in C++
- Variable types can be bool,int,float,double,char,void or wchar_t.
- A variable name can consist of Capital letters A-Z, lowercase letters a-z, digits 0-9, and the underscore character.
- The first character must be a letter or underscore.
- Variable names are case-sensitive.
- C++ keywords cannot be used as variable names.
- Blank spaces are not allowed in C++ variables.
- Special characters like %,& are not allowed.
- The length of the variable should not exceed more than 31 characters. Specifying variables more than 31 characters, the compiler will ignore all characters after 31st characters.
Unlike variables, constants are the fixed values that cannot be changed during the execution of the program. Constants are initialized at the time of creating it, and remains constant and cannot be changed later. They are also called as “literals”.For example:1,9.67,”character” etc.
Constant Definition in C++
C++ constant is defined in two different ways. They are listed below:
- By using “const” keyword
- By using “#define ” preprocessor
Constant definition Using “const” keyword
const type constant_name;
const double PI = 3.14;
Constant definition Using “#define” preprocessor
#define MAX 70
Types of Constants/Literals
The different types of constants/literals are listed below:
- Integer Literals
- Floating literals
- Character Literals
- String Literals
The short description of each one of them is given below:
Integer constant defines the pure form of integer only. It doesn’t include a decimal point or a fractional number.For example:1258,-96,-56,8645 etc.
They are of three types:
- decimal constant(base 10)
- octal constant(base 8)
- hexadecimal constant(base 16)
Decimal constants: 7,-4,67 etc Octal constants: 058,032,074 etc Hexadecimal constants: 0Xa, 0x4b, 0X629 etc
A floating point constant consist of a number in the form of a decimal point or fractional part. They are also called as real constants. They can also be in the exponential form.For example:789.25,0.259,-58.37 etc.
For exponent form:
E 3=10^3 E-7=10^-7
A single character enclosed in a single quotation is used to represent a character constant.For example:’A’, ‘y’, ‘8’, ‘[‘ etc.There are some characters that cannot be typed such characters are represented by escape characters.
|Escape characters||Used For|
|\’||Single quotation mark|
A string literal is a collection of characters enclosed in a double quotation mark.
"character" //string constant "" //null string constant " " //string constant of four white space "A" //string constant having single character. "Sun is hot.\n" //prints string with newline