C++ Variables, Constants and Literals

Through the use of examples, we will learn about variables, literals, and constants in C++ in this tutorial.

C++ Variables

C++ Variables, Constants - banner

A variable is a storage area (container) for data in programming.

int geektocode = 2022;

Here, geektocode is a variable of the data type int, and we have given it the integer value 2022 in this instance.

Creating and Declaring Variables in C++

In order to create a variable, choose the type and give it a value:

type variableName = value;

For instance, there are various types of variables in C++ that are defined with various keywords.

  • int: stores only whole numbers (integers), such as 145 or -144, without any decimal points.
  • double: carries decimal floating point numbers, such as 20.59 or -39.89.
  • char: single characters, like “a” or “B,” are stored. Single quotes are used to enclose character values.
  • string: such as “Hello GeektoCode,” text is stored. Double quotes are used to enclose string values.
  • bool: stores values in the true or false states.

Guidelines for variable naming.

  • In a variable name, only letters, numbers, and the underscore (_) are permitted.
  • A variable name cannot begin with a number.
  • It is recommended to use a lowercase character to start variable names. For instance, the name is preferred over the Name.
  • A keyword cannot be a variable name. For instance, the keyword int is used to indicate integers.
  • An underscore may be used to begin a variable name. It is not regarded as a good practice, though.

Note: Try to give your variables names that mean something. For example, school_id is a better variable name than si.

Multiple variables to a single value:

Additionally, you can set the same value for several variables in a single line:

int x, y, z;
x = y = z = 88;
cout << x + y + z;

Escape Sequences

In C++ programming, it may occasionally be necessary to use characters that cannot be typed or have unique meanings. the question mark, tab, newline (enter), and so forth.

Escape sequences are used to access these characters:

Escape SequencesMeaning
\fForm feed
\tHorizontal tab
\vVertical tab
\’Single quotation mark
\”Double quotation mark
\?Question mark
\0Null Character

Literals in Strings

A series of characters enclosed in double-quote marks is referred to as a string literal. For instance:

“GeektoCode”string constant
“”null string constant
” “string constant of six white space
“G”string constant having a single character
“Hello GeektoCode\n”prints string with a newline

C++ Constants

Use the const keyword (which will declare the variable as “constant,” which means unchangeable and read-only) when you do not want others (or yourself) to override existing variable values.

const int studentId = 2022;  // studentId will always be 2022
studentId = 2022;  // error: assignment of read-only variable studentId '

When you have values that are unlikely to change, you should always declare the variable as constant:

Leave a Reply

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