Skip to main content

C++ Variables, Literals and Constants

In this tutorial, we will learn about variables, literals, and constants in C ++ with the help of examples.

C++ Variables

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

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

int age = 16 ; 

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

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

We will learn about all the data types in detail in the next tutorial.
The value of a variable can be changed, hence the name variable

int age = 14 ;
age = 16;

Rules for naming a variable : 

  • A variable name can only have alphabets, numbers and the underscore _.
  • A variable name cannot begin with a number.
  • Variable names cannot begin with an uppercase character.
  • A variable name cannot be a keyword. For example, int is a keyword that is used to denote integers.
  • 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.

C++ Literals 


Literals are data used for representing fixed values. They can be used directly in the code.
 For example: 1, 2.5, 'c' etc. Here, 1, 2.5 and 'c' are literals. Why? You cannot assign different values ​​to these terms. Here's a list of different literals in C ++ programming.

1. 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)

For example :-
Decimal: 0, -9, 22 etc
Octal: 021, 077, 033 etc
Hexadecimal: 0x7f, 0x2a, 0x521 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.

3. Character

A character literal is created by enclosing a single character inside single quotation marks. For example: 'a', 'm', 'F', '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.




Comments

  1. The article is clear as it is. It clearly communicated the idea of why functions are beneficial in programming. If anyone want to learn C++ you can visit our website. This make a mastered in C++ programing. We have complete C++ tutorials teaching you to program in C++. Whether you’ve had any prior experience programming or not, the tutorials on this site will walk you through all the steps you’ll need to know in order to create and compile your programs..

    Thank you very much!

    ReplyDelete

Post a Comment