Literals, Variables and Constants in C++

In this tutorial, let us learn about variables, literals, and constants in C++ with help of some examples. So let us learn first what a variable is in C++.

C++ Variables

In programming, a variable is defined as a container that is used to store data. So it is used to store some information that we can later on reference and manipulate in our code.

So to create a variable, we first need a unique name for the variable. This unique name for the variable is also called as the identifier. So let us see some examples on how we can create and initialize variables in C++.

int age = 23; 
char b = 'a'

Here, age is a variable of the int data type, and to it we have assigned a value of 23. Next we have b which is a variable of the char data type and to it we have assigned some character data which in this case is ‘a’.

Note: One thing to note here is that a variable with the int data type can only hold integers. Similarly, a variable with the char data type can only hold data of character type. If we want to store decimals and exponentials, we can make use of the double data type.

Another thing to note here is that the variables are mutable in the sense that you can reassign the variable to difference values and hence this implies that you can change the content of a variable.

Let us also see an example for this :

int age = 23;   // initially age is set to 23
age = 29;       // reassigning the age to a new value of 29

Rules for naming a variable

  • A variable name can only have alphabets, numbers, and the underscore _.
  • A variable name cannot begin with a number.
  • It is a preferred practice to begin variable names with a lowercase character. For example, name is preferable to Name.
  • A keyword cannot be used as a variable name. For example, a keyword like int cannot be used as a variable name.
  • A variable name can start with an underscore. Also ensure that you give meaningful names to your variables which semantically makes sense.

C++ Literals

Literals are data used for representing fixed values. They can be used directly in the code. So in essence, these are source code representation of a fixed value or a sequence of characters which represents a constant value that is to be stored inside a variable.

For example: 156.5'z' etc.

Now these are called literals because you cannot assign a different value to these terms. Here is a list of different literals that we have in C++ Programming :

1. Integers

According to Wikipedia,

 An integer is a datum of integral data type, a data type that represents some range of mathematical integers. Integral data types may be of different sizes and may or may not be allowed to contain negative values. Integers are commonly represented in a computer as a group of binary digits (bits)

This means that an integer is a numerical literal that does not have any fractional or exponent within it. There are three types of integer literals in C Programming :

  • decimal (base 10)
  • octal (base 8)
  • hexadecimal (base 16)

For example:

Decimal: 0, -21, 46 etc
Octal: 02, 786 etc
Hexadecimal: 0x3f,  0x421 etc

A thing to note here is that an octal starts with a 0 and a hexadecimal starts with a 0x.

2. Floating-point Literals

Floating-point literals are numbers that have a decimal point or an exponential part. They can be represented as: Real literals. Binary floating-point literals.

-2.0

0.0000234

-0.22E-5

Note: E-5 = 10-5

3. Characters

A character literal is a type of literal in programming for the representation of a single character’s value within the source code of a computer program. It is represented by enclosing a single character inside single quotes. For example : 'b', 'f', 'G', '21', '}' etc.

4. Escape Sequences

 An escape sequence is a combination of characters that has a meaning other than the literal characters contained therein; it is marked by one or more preceding characters

Here is a lit of escape sequences for the given characters :

Escape Sequences Characters
b Backspace
f Form feed
n Newline
r Return
t Horizontal tab
v Vertical tab
Backslash
' Single quotation mark
" Double quotation mark
? Question mark
Null Character

5. String Literals

A string literal is a sequence of characters enclosed in double-quote marks. For example:

"This is a string" create a string
"" empty string
" " string with a whitespace in quotes.
"x" a single character enclosed within quotes.
"This is a stringn" prints a string with a newline

We will also learn about Strings in a separate article as well. Let us now move forward to constants in C++.

C++ Constants

The value of a constant never changes once defined, so kind of it is immutable and cannot be re-assigned once a value has been fixed for a constant.

const int PI = 3.14;
PI = 3.233 // Error! PI is a constant.

See the above example here we have used the const keyword to declare a constant which we have named PI. Now if we try to mutate or change the value of PI,we will get an error because we cannot reassign a variable that we have declared using the const keyword. If we do so, we will get an error.

We can also create a constant using the #define preprocessor directive, something that we will learn about in a separate article.

Previous Post
Next Post