# C++ Variables & Constants

## C++ Variables

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:

 S.NO Type Description 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:

Syntax:

data_type variable_name;


There can be a number of variables. The syntax for more than one variables can be:

Syntax:

data_type variable_list;

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:

Syntax:

data_type variable_name = value;

### Variable Declaration Examples

Example:

int x=4,y,z=12;
char letter="ABC";
float salary=225.50;
double d;

## Rules of Declaring Variables in C++

1. Variable types can be bool,int,float,double,char,void or wchar_t.
2. A variable name can consist of Capital letters A-Z, lowercase letters a-z, digits 0-9, and the underscore character.
3. The first character must be a letter or underscore.
4.  Variable names are case-sensitive.
5. C++ keywords cannot be used as variable names.
6. Blank spaces are not allowed in C++ variables.
7. Special characters like %,& are not allowed.
8. 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.

## C++ Constants

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:

1. By using “const” keyword
2. By using “#define ” preprocessor

### Constant definition Using “const” keyword

Syntax:

const type constant_name;

Example:

const double PI = 3.14;

### Constant definition Using “#define” preprocessor

Syntax:

#define constant_name;

Example:

#define MAX 70

## Types of Constants/Literals

The different types of constants/literals are listed below:

1. Integer Literals
2. Floating literals
3. Character Literals
4. String Literals

The short description of each one of them is given below:

## Integer Constants

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)

For example:

Decimal constants: 7,-4,67 etc
Octal constants: 058,032,074 etc
Hexadecimal constants: 0Xa, 0x4b, 0X629 etc

## Floating Literals

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

## Character Constants

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 \b Backspace \n Newline \t Horizontal tab \’ Single quotation mark \? Question mark

## String Literals

A string literal is a collection of characters enclosed in a double quotation mark.

For example:

"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
SHARE C++ Variables & Constants