Introduction:
C++ Variables,Literals
A memory location is generally given a variable name . Generally data is stored in them.
The value that is usually stored in any variable can be modified or reused during the execution of the program.
Below we can find the syntax to declare a variable
// Declaration of a Single variable
// Declaration of Multiple variables
For the purpose of indicating the storage area, each variable is supposed to be given a unique name (identifier).
For example,
Here, rishu is our variable of the int data type, and here we have given an integer value 21 to it.
We are allowed to change the value of a variable, hence the name variable.
For example:-
Below we can find the rules for naming a variable
1) A variable name is supposed to be consisting of alphabets (both uppercase and lowercase), numbers and the underscore _.
2) A variable name is not supposed to begin with a number.
3) In naming a variable no spaces or special characters are allowed.
4) Variable names are generally not supposed to begin with an uppercase letter.
5) Variable names are generally case-sensitive and need special attention while using them.
6) A variable name cannot be a keyword. For example, int is the keyword that generally comes in use to denote integers.
7) A variable name is allowed to start with an underscore. However, it’s not generally considered to be a good practice.
C++ Literals
Fixed values are represented with an object called literals. They can be used directly in the code. For the sake of example: 1, 2.1, ‘r’ etc.
Here, 1, 2.1 and ‘r’ are our literals.
Below we can find a list of different literals that are present in C++ programming.
1. Integers literal
An integer can be called as a numeric literal(that are associated with numbers) not consisting of any fractional or exponential part. Generally there are three different types of integer literals in C++ programming language:
1) decimal (base 10)
2) octal (base 8)
3) hexadecimal (base 16)
2. Floating-point Literals
A floating-point literal can be called as a numeric literal that either can be in a fractional form or an exponent form. For example:
3. Characters literal
A character inside single quotes are generally represented and stored using a character literals. A character literal is generally created by surrounding a single character with single quotation marks. For example: ‘r’, ‘i’, ‘S’, ‘2’, ‘)’ etc.
4. Escape Sequences
It sometimes gets necessary to use characters that cannot be typed or own a special meaning in C++ programming. For e.g., newline (enter), tab, question mark, etc.
5. String Literals
A sequence of characters enclosed in double-quote marks is called as a string literal.
6. Boolean Literals
Boolean constant values are represented using Boolean literal.
This literal type generally can take only two below mentioned Boolean values.
- True
2. False
C++ Variables and Literals