In this blog post, we will learn about variables, constants, and literals in C++ with the help of examples.
C++ Variables:
A variable is a storage location that contains some value known or unknown. A symbolic name (identifier) is associated with a variable and usually, we used this name to refer to the stored value.
Let’s see an example of how we can create a variable in programming.
//A general syntax for variable declaration data_type var_name;
//integer variable int weight = 27;
The above statement tells the program that it is storing an integer and that the name weight represents the integer’s value. Here weight is representing 27 (integer).
When the above statement will execute a chunk of memory will allocate to store the integer value. Using the ‘weight’ we can access the memory to read the store value (27). If you want you can also change the store value by using the below statement.
//changing weight value weight = 6;
The variable name does not give you information that where in-memory value is stored, but using the & operator we can retrieve the address of the variable. In our next post, we will see it in detail.
Naming Rule of the variable in C++:
A variable name can be composed of letters, digits, or underscore. C++ is a case-sensitive language so upper and lower case are totally different from each other. for example, data and DATA both are two different variables.
1. A variable name should not contain space.
int data Len; (wrong) int dataLen; (correct)
2. A variable name can only have alphabets, numbers, and the underscore ( _ ).
3. A variable name cannot start with a digit but you can use it between or end of the identifier.
int 2length; (wrong) int le2ngth; (correct)
4. You cannot use a special character in identifiers except the underscore.
int len@gth; (wrong) int le#ngth ; (wrong) int len_gth; (correct) int _length; (correct)
5. A variable name cannot be a keyword.
int if; (wrong) int while; (wrong)
Note: We always try to give meaningful names to variables and should follow single notation in projects, like camel case or Hungarian notation both are very popular.
C++ Constants:
The const keyword is a qualifier, it changes the behavior of the variable and makes it a read-only type. When we want to make an object read-only type, then we have to declare it as const.
const int data = 0; data = 10 // Compiler Error because data is a constant.
In another separate article, we will read the const qualifier in detail.
If you want to learn C++11 from scratch then you can follow this course trial is free.
C++ Literals:
A literal is a notation for representing a fixed value in source code. You can not assign value to literals, for example, 10, 2.5, ‘a’,.etc. Here, 10, 2.5, and ‘a’ are literals because you cannot assign different values to these terms.
There are several kinds of literals in C++.
integer-literal :
An integer literal is a sequence of digits that has no period or exponent part. An integer literal has the following form,
- binary-literal integer-(suffix optional): Ex. 0, 1
- octal-literal integer-(suffix optional): Ex. 0, 1, 2, 3, 4, 5, 6, 7
- decimal-literal integer-(suffix optional) Ex. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
- hexadecimal-literal integer-(suffix opt)Ex. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, A, b, B, c, C, d, D, e, E, f, F
Note: Here I am not going into detail, I will cover literals in a separate article.
character-literal
A character literal is one or more characters enclosed in single quotes, as in ’x’. Also, character literals optionally preceded by u8, u, U, or L, as in u8’w’, u’x’, U’y’, or L’z’, respectively.
A character literal that does not begin with u8, u, U, or L is an ordinary character literal. For example: ‘a’, ‘b’, ‘A’, ‘2’, ‘)’ etc.
floating-literal:
floating-point literals are real numbers that have a decimal point or an exponential part. They can be represented as:
1. digit-sequence exponent-part floating-suffixopt:
Representing a whole number without a decimal separator, in this case, the exponent is not optional. Example, 1e5, 1e-3L
2. digit-sequence. exponent-partopt floating-suffixopt:
Representing a whole number with a decimal separator, in this case, the exponent is optional. Example, 1. , 1.e-3
3. digit-sequenceopt . digit-sequence exponent-part(optional) floating-suffixopt
Representing a fractional number. The exponent part is optional. Example, 3.14, .1f, 0.1e-1L
4. hexadecimal-prefix(0x) hexadecimal-digit-sequence binary-exponent-part floating-suffixopt
Representing a whole number without a radix separator. Example, 0x1ffp10, 0X0p-1
5. hexadecimal-prefix(0x) hexadecimal-digit-sequence . binary-exponent-part floating-suffixopt
Representing a whole number with a radix separator. Example, 0x1.p0, 0xf.p-1
6. hexadecimal-prefix(0x) hexadecimal-digit-sequenceopt. hexadecimal-digit-sequence binary-exponent-part floating-suffixopt
Representing a fractional number with a radix separator. Example, 0x0.123p-1, 0xa.bp10l
An abbreviation that has been used in the above syntax.
string-literal
A string literal is a sequence of characters surrounded by double-quotes. A string literals, optionally prefixed by R, u8, u8R, u, uR, U, UR, L, or LR, for example, “…”, R”(…)”, u8″…”, u8R”**(…)**”, u”…”,
uR”*~(…)*~”, U”…”, UR”zzz(…)zzz”, L”…”, or LR”(…)”, respectively.
boolean-literal
The Boolean literals are the keywords false and true. Such literals are prvalues and have type bool. for example, false, true.
pointer-literal
The pointer literal is the keyword nullptr. It is a prvalue of type std::nullptr_t.
Note: std::nullptr_t is a distinct type that is neither a pointer type nor a pointer-to-member type; rather, a prvalue of this type is a null pointer constant and can be converted to a null pointer value or null member pointer value.
user-defined-literal
You can create your own literals using the integer, floating-point, character, and string literals. User-defined-literal introduces in C++11.