Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

Variables, Literals and Constants

In this tutorial, we will learn about variables, declaration of variables, variable rules, literals, literal-types, and constants in C++ with the help of examples.


C++ Variables

A variable is a name given to a memory location. It is used to store data.
The value stored in a variable can be changed or reused during the program execution.

Let’s see the syntax to declare a variable

// Declaring a Single variable 
type variable_list; // Declaring Multiple variables  type 
variable1_list, variable2_list, variable2_list;

To indicate the storage area, each variable should be given a unique name (identifier).
For example,

int apple = 10;

Here, apple is a variable of the int data type, and we have assigned an integer value 10 to it.

Note: The int data type suggests that the variable can only hold integer value. Similarly, if we have to store decimals and exponentials we can use the double data type.

We will learn about all the data types in detail in the later tutorial.

The value of a variable can be changed, hence the name variable.

Example:-

int num = 10;   // number is 10
num = 14;       // number is 14 

Rules for naming a variable

1) A variable name can consist of alphabets (both uppercase and lowercase), numbers and the underscore _.
2) A variable name cannot begin with a number.
3) No spaces or special characters are allowed.
4) Variable names cannot begin with an uppercase letter.
5) Variable names are case-sensitive (uppercase and lowercase letters are different).
6) A variable name cannot be a keyword. For example, int is a keyword that is used to denote integers.
7) A variable name can start with an underscore. However, it’s not considered a good practice.

Note: We should try to give meaningful names to variables. For example, first_name is a better variable name than fn/fname.


C++ Literals

Literals is an object that representing fixed values. They can be used directly in the code. For example: 1, 5.9, 'a' etc.
Here, 1, 5.9 and 'a' are literals. Why? You cannot assign different values to these terms.

1) The constant value can be named differently as literal.
2) for Example:- Const int value=15 assigns constant integer value 15 to value and it is an integer leteral.
3) In this tutorial, we will go through an explanation of each of the different types of literals in C++.

Here’s a list of different literals in C++ programming.

  1. Integer literal
  2. Float literal
  3. Character literal
  4. Escape Sequences
  5. String literal
  6. Boolean literal

1. Integers literal

An integer is a numeric literal(associated with numbers) without any fractional or exponential part. There are three types of integer literals in C++ programming:

1) decimal (base 10)
2) octal (base 8)
3) hexadecimal (base 16)

For example:

Decimal: 0, -7, 22 etc
Octal: 041, 087, 043 etc
Hexadecimal: 0x2f, 0x5a, 0x261 etc

In C++ programming, octal starts with a 0, and hexadecimal starts with a 0x.


2. Floating-point Literals

A floating-point literal is a numeric literal that has either a fractional form or an exponent form. For example:

-5.1, 0.000432, -0.62E-2

Note: E-2 = 10-2


3. Characters literal

A character literals are used to represent and store a character inside single quotes.
A character literal is created by enclosing a single character inside single quotation marks. For example: 'a', 's', 'L', '2', ')' etc.


4. Escape Sequences

Sometimes, it is necessary to use characters that cannot be typed or has special meaning in C++ programming. For example, newline (enter), tab, question mark, etc.

In order to use these characters, escape sequences are used.

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:

"God" string constant
"" null string constant
" " string constant of six white space
"x" string constant having a single character
"We love the Earthn" prints string with a newline

We will learn about strings in detail in the C++ string tutorial.


6. Boolean Literals

    • Boolean literal represents Boolean constant value.
    • This literal type can take only two Boolean values.

1. True
2. False

// For representing boolean literal

const bool bool_literal1 = True;
const bool bool_literal2 = False;

C++ Constants

1) In C++, we can create variables whose value cannot be changed.
2) Constant must have to be initialized at the time of creating it.
3) For that, we use the const keyword.
4) Constants can be any of the data types.
5) It is considered best practice to define constants using only upper-case names.


Constant Definition in C++

There are two different ways to define constants in C++.

1) By using const keyword.
2) By using #define preprocessor.

For Example:-

const int NUMBER = 299;
NUMBER = 250 // Error! NUMBER is a constant.

Here, we have used the keyword const to declare a constant named NUMBER. If we try to change the value of NUMBER, we will get an error.

A constant can also be created using the #define preprocessor directive. We will learn about it in detail in the C++ Macros tutorial.

error: Content is protected !!