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.NOTypeDescription
1.boolStores the value that is either true or false.
2.intStores the exact whole number(not a fractional number).
3.charTypically a single octet (one byte). This is an integer type.
4.floatA single-precision floating point value.
5.doubleA double-precision floating point value.
6.voidAn absence of return type.
7.wchar_tRepresents 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:

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

Syntax:

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

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:

Variable Declaration Examples

Example:

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:

Example:

Constant definition Using “#define” preprocessor

Syntax:

Example:

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)
  • hexadecimal constant(base 16)

For example:

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:

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 charactersUsed For
\bBackspace
\nNewline
\tHorizontal tab
\’Single quotation mark
\?Question mark

String Literals

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

For example:

SHARE C++ Variables & Constants

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Share