Join us Now |  Forgot Password? | Forgot UserName?


Learn step by step

Variables and types

Rules on variable name:

  • First character should not be digit.
  • Can consist of letter, alphabets and must start with underscore or character.
  • Special characters not allowed.
  • Blank spaces not allowed.
  • As C++ is case sensitive language so if we declare a variable 'name' and another 'NAME' both are different variables.
  • C++ keywords cannot be used for variable name.
  • A variable name can be consist of maximum 31 characters only if we declare a variable more than 1 characters compiler will ignore after 31 characters.


An Identifier is nothing but a user given name to a variable, it can be letters from a to z or letters A to Z or numbers 0 to 9 or it can be a combination of them. All rules apply on variable declaration apply on identifier declaration because identifier is a name to the variable. Keyword cannot be an identifier, it should start with letter of an underscore sign.

Variable Declaration

To use a variable in our program, we need to firstly declare it.

Syntax Example
// To declare a variable of a specified type
type identifier;
// Multiple variables of same type
type identifier1, identifier2, identifier3, ..., identifier n;
// To declare a variable & assign initial value
type identifier = value;
// To declare multiple variables with initial values
type identifier1 = value1, ..., identifier n = value n;
int x;
double sum, x, y, z;
int n = 88;
double sum = 0.0, x = 1.0;

int mark1; // Declare an int variable called mark1
mark1 = 76; // Use mark1
int mark2; // Declare int variable mark2
mark2 = mark1 + 10; // Use mark2 and mark1
double average; // Declare double variable average
average = (mark1 + mark2) / 2.0; // Use average, mark1 and mark2
int mark1; // Error: Declare twice
mark2 = "Hello"; // Error: Assign value of a different type

Fundamental data types

Integers: C++ supports these integer types: char, short, int, long, long long (in C++11) in a non-decreasing order of size. Actual size depends on the implementation. The integers (except char) are signed number (that can hold zero, positive and negative numbers). We could use the keyword unsigned [char|short|int|long|long long] to declare an unsigned integers (which can hold zero and positive numbers). There are a total 10 types of integers - signed|unsigned combined with char|short|int|long|long long.

Characters: Characters (e.g., 'a', 'Z', '0', '9') are encoded in ASCII, and kept in type char. Example, character '0' is 48 (decimal) or 30H (hexadecimal); character 'A' is 65 (decimal) or 41H (hexadecimal); character 'a' is 97 (decimal) or 61H (hexadecimal). Note: the type char can be interpreted as character in ASCII code, or an 8-bit integer. Unlike long or int, which is signed, char could be signed or unsigned, depending on the implementation. We can use signed char or unsigned char to explicitly declare signed or unsigned char.

Floating-point Numbers: There are 3 floating point types: float, double & long double, for single, double & long double precision floating point numbers. float & double are represented as specified by IEEE 754 standard. A float can represent a number between ±1.40239846×10^-45 and ±3.40282347×10^38, approximated. A double can represented a number between ±4.94065645841246544×10^-324 and ±1.79769313486231570×10^308, approximated. Note: not all real numbers can be represented by float & double, because there are infinite real numbers.

Boolean Numbers: A special type called bool (for boolean), which takes a value of either true or false.

The table below shows the typical size and different ranges for the primitive types.

Category Type Description Bytes
Integers int
(or signed int)
Signed integer (of at least 16 bits) 4 (2) -2147483648 2147483647
unsigned int Unsigned integer (of at least 16 bits) 4 (2) 0 4294967295
char Character
(can be either signed or unsigned depends on implementation)
signed char Character or signed tiny integer
(guarantee to be signed)
1 -128 127
unsigned char Character or unsigned tiny integer
(guarantee to be unsigned)
1 0 255
(or short int)
(or signed short)
(or signed short int)
Short signed integer (of at least 16 bits) 2 -32768 32767
unsigned short
(or unsigned shot int)
Unsigned short integer (of at least 16 bits) 2 0 65535
(or long int)
(or signed long)
(or signed long int)
Long signed integer (of at least 32 bits) 4 (8) -2147483648 2147483647
unsigned long
(or unsigned long int)
Unsigned long integer (of at least 32 bits) 4 (8) 0 same as above
long long
(or long long int)
(or signed long long)
(or signed long long int) (C++11)
Very long signed integer (of at least 64 bits) 8 -263 263-1
unsigned long long
(or unsigned long long int) (C++11)
Unsigned very long integer (of at least 64 bits) 8 0 264-1
Real Numbers float Floating-point number, ≈7 digits
(IEEE 754 single-precision floating point format)
4 3.4e38 3.4e-38
double Double precision floating-point number, ≈15 digits
(IEEE 754 double-precision floating point format)
8 1.7e308 1.7e-308
long double Long double precision floating-point number, ≈19 digits
(IEEE 754 quadruple-precision floating point format)
12 (8)    
bool Boolean value of either true or false 1 false (0) true (1 or non-zero)
char16_t (C++11)
char32_t (C++11)
Wide (double-byte) character 2 (4)    

Related Videos