C++ Variables, Literals, and Constants #2

we will understand c++ variables, literals, and constants in C++ with the use of examples. Constants guide to fixed values that the program may not alter and they are called; strong literals strong. Constants can be of any of the basic data types and can be divided into Integer Numerals, Floating-Point Numerals, Characters, Strings, and Boolean Values.

In this tutorial, we will understand variables, literals, and constants in C++ with the use of examples.

Constants guide to fixed values that the program may not alter and they are called literals.

Constants can be of any of the basic data types and can be divided into Integer Numerals, Floating-Point Numerals, Characters, Strings, and Boolean Values.

C++ Full Course Free enrolling

Introduction to Standard C++ : In a Nutshell – lesson 1

C++ Variables

In programming, a variable is a value storing area (container) to hold data.

To indicate the storage area, each variable should have a unique identifier.

e.g.-

int age = 14;

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

Note: The int datatype suggests that the variable can only hold integers. Also, we can use the double data type if we have to hold decimals and exponentials.

We will learn about all the data types in detail in the upcoming article tutorials.

The value of a variable can be changed, so we do here the naming process for it.

int age = 14;   // age is 14
age = 17;       // age is 17

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 best practice to start variable names with lowercase characters.
    e.g.- the name is preferable to Name.
  • A variable name cannot be a keyword.
    e.g.- we use int keyword to donate intigers.
  • A variable name can start with an underscore. but, it’s not supposed to be adequate practice.
  • We should try to give consequential names to variables.
    e.g.- first_name is a better variable name than fn.

C++ Literals

Literals are data used for characterizing fixed values. They can be used directly in the code. e.g. – 12.5'c' etc.

So, 12.5 and 'c' are literals.


Why? You cannot assign different values to these terms.

Next, we will see a list of different literals in C++ programming.

Integers

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:

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

An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order.

Following are other examples of various types of Integer literals −

85 // decimal
0213 // octal
0x4b // hexadecimal
30 // int
30u // unsigned int
30l // long
30ul // unsigned long

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

Floating-point Literals

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

While representing using the decimal form, you must include the decimal point, the exponent, or both and while representing using the exponential form, you must include the integer part, the fractional part, or both. The signed exponent is introduced by e or E.

Here are some examples of floating-point literal −

3.14159 // Legal
314159E-5L // Legal
510E // Illegal: incomplete exponent
210f // Illegal: no decimal or exponent
.e55 // Illegal: missing integer or fraction

Characters

A character literal is created by enclosing a single character inside single quotation marks.

e.g. – 'a''m''F''2''}' etc.

Boolean Literals

There are two Boolean literals and they are part of standard C++ keywords −

  • A value of true represents true.
  • A value of false represents false.

You should not consider the value of true equal to 1 and the value of false equal to 0.

Escape Sequences

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

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

Escape SequencesCharacters
\bBackspace
\fForm feed
\nNewline
\rReturn
\tHorizontal tab
\vVertical tab
\\Backslash
\'Single quotation mark
\"Double quotation mark
\?Question mark
\0Null Character

Following is the example to show a few escape sequence characters −

#include <iostream>
using namespace std;

int main() {
   cout << "Hello\tWorld\n\n";
   return 0;
}

When the above code is compiled and executed, it produces the following result −

Hello World

String Literals

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

e.g. –

"good"string constant
""null string constant
" "string constant of six white space
"x"string constant having a single character
"Earth is round\n"prints string with a newline

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

C++ Constants

In C++, we can create variables whose value cannot be changed. For that, we use the const keyword.

Let’s how we use the const keyword in code –

const int train_speed = 299792458;
train_speed = 2500 // Error! train_speed is a constant.

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

Defining Constants

There are two simple ways in C++ to define constants −

  • Using #define preprocessor.
  • Using const keyword.

The #define Preprocessor

Following is the form to use #define preprocessor to define a constant −

#define identifier value

The following example explains it in detail −

#include <iostream>
using namespace std;

#define LENGTH 10   
#define WIDTH  5
#define NEWLINE '\n'

int main() {
   int area;  
   
   area = LENGTH * WIDTH;
   cout << area;
   cout << NEWLINE;
   return 0;
}

When the above code is compiled and executed, it produces the following result −

50

The const Keyword

You can use the const prefix to declare constants with a specific type as follows −

const type variable = value;

The following example explains it in detail −

#include <iostream>
using namespace std;

int main() {
   const int  LENGTH = 10;
   const int  WIDTH  = 5;
   const char NEWLINE = '\n';
   int area;  
   
   area = LENGTH * WIDTH;
   cout << area;
   cout << NEWLINE;
   return 0;
}

When the above code is compiled and executed, it produces the following result −

50

C++ Full Course Free enrolling

Introduction to Standard C++ : In a Nutshell – lesson 1


Reference websites :

isocpp.org/

cplusplus.com/

learncpp.com

We will learn about it in detail in the upcoming C++ tutorials.

Share your love
Lamba Pankaj
Lamba Pankaj
Articles: 12

Leave a Reply

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

I'm not a robot *Time limit exceeded. Please complete the captcha once again.

Website Design Trends in 2022 5 Movies that Programmers Should Watch Best Search Engine for Developers – You.com 06 Free Website Hosting Providers for Developers You should Know Learn Programming while playing games