Some terms : Variables , Identifier , initialization , assignment, Keyword , Operators ,literals.
Variables, identifier, initialization, assignment, initializer, keyword, operator, operand, associativity, etc are some of the terms use in C++. These terms are commonly used in C++ programming so understanding what they stand for will make the language easier to learn.
i)Variables
ii)Naming convention
iii)Initialization and assignment
iv)*Initializer
iv)Identifier
v)Keywords
vi)Operators
vii)Operand
viii)Left hand operand and right hand operand
ix)Precedence
x)Associativity
xi)literals
variables
Variables are names given to a memory space by the programmer. Using variable we can access the data stored in the memory. Using variable we can also perform numerous tasks like addition, subtraction, etc to the data present in the memory. In short, we require variables if we want to manipulate any data in our program.
** A little in-depth look at a variable:
How does variable help find data? we say a “variable is a name given to the memory by the programmer” so we simply use the name to access the memory, but how exactly? To help understand better let’s look at a chunk of memory(where we store our data) as our Earth and every data as the person on the Earth. Now to find a person what do we require, of course, his/her name and address. Now, treat the ‘name’ of the person as variable, we still require an address to find the person, don’t we? . An interesting thing about the variable is it can also provide us its own address(which is also known as a reference or a pointer it is discussed in another post). So to get an address we, in fact, require only variable. Now if we know the variable we have the ‘name'(the variable itself) and the ‘address'(obtain from the variable) ,so in this way, we can access the data present anywhere in the memory.
There is something known as declaration of variables, it simply means giving a name to the variable. So when we say declaring a variable, we are simply naming a specific region of the memory. You can use any name here but there are some rules which you must follow while giving a name to a variable, look below for more information on ‘variable naming convention’. Note, here you need not worry which part of the memory has been given a name the compiler will take care of that, you only require the variable name to access the data.
While declaring a variable we must follow the format.
data_type variable_name;
the ‘data_type’ if the data type discussed here and the ‘variable_name’ is the name given to the memory or a variable name. An example is given below on how to declare a variable.
int int_name; //'int_name' is the variable
Above we have declared an integer type variable with the name ‘int_name’, a more example is given below.
long int long_name; //a long int type variable declaration float fl; //a float type variable declaration char c; //a char type variable declaration
Variables naming convention
Some people suggest a variable should always begin with a lower case and the name must relay certain information about the purpose of the variable,for instance we must use ‘next_link’ or ‘nextLink’ not ‘nextlink’ ,while programming a linked list program.Following such naming convention will help a great deal in minimizing the confusion that might arise when lots of variable are used in a program,however it may not be wrong to say that whatever method you used to name a variable,it is up to you because you are the programmer.But, make sure that it is recognizable and do not simply use whatever names come to your mind,this may create a lot of trouble later on figuring out which name stands for which value.
Initialization and assignment
The term initialization and assignment has different concept. When a variable is declared and at that moment a value is given to it then it is known as initializing a variable.
But if that same variable value is given another value somewhere else then it is known as assigning a variable. For instance look at the program below.
#include <iostream> using namespace std; int main( ) { int i=0 , i1=12 ; //’i’ and i1 is initialized here int ii=i1 ; // ‘ii’ is initialized here i=ii ; /*'i' is given new value so 'i' is assigned the value 12 (not initialized)*/ cout << i << ii << endl ; cin.get(); return 0; }
Do you see the difference?
Initializer
The term initializer refers to the value which is initialized or assigned to the variable. It can be integer or character or string or real number. Consider the code below.
int i1='23'; //23 is initializer char c='B'; //'B' is initializer double d; //no initializer here d=346.677; // 346.677 is the initializer
Identifier
Identifier are the names given to all the program entities. The name given to a variable or a name given to the function(discuss in chapter 2) or to the constants are all identifiers. So, all variables are identifiers but all identifiers are not variables because identifier is a bigger collection of names given to various program entities.>
Keywords
Keywords are names which have predefined meaning in C++. The compiler knows what each keyword stands for. They cannot be used for naming variables or any other identifier. This is done in order not to create any confusion as which names to be used as variables and which names to be used as keywords in our program. Some of the keywords in C++ are int, float, new, delete, namespace, etc.
Operators
Operators are signs or symbols which can perform certain operations. There are many types of operator in C++ for instance, a mathematical operators which operator can perform mathematical operation, +(addition), –(subtraction), /(division), %(remainder), etc. are some of the mathematical operators. Other types of operator will be discussed in detail later.
Operand
The element on which or between which an operation is perform are known as operand.
Let’s look at the expression, a+b, here ‘a’ and ‘b’ are variables, but ‘a’ and ‘b’ are also known as operands because an addition operation is performed between them.
Let’s look at another example ‘x++‘, here ‘x’ is an operand. The operator ++ (known as increment) increase its value by 1.
Left hand operand and right hand operand
These terms are mainly use when we are assigning a value to a variable or comparing two expressions. Suppose we are assigning the value of one variable to another.
b=a; , b is the left hand operand and a is the right hand operand.
while comparing two expressions the whole left side expression becomes the left hand operand and the whole right side expression becomes the right hand operand. For instance,
x+y=p-q ; , x=y is the left hand operand and p-q becomes the right hand operand.
Precedence
In an expression where many operators are used, the order of performing the operation is called precedence.
Associativity
If the operators have same precedence then the order of performing the operation is known as associativity. We will discuss more about precedence and associativity later in the chapter.
Literals
Literals are values assigned to variables, for instance 90 is a literal, ‘B’ is a literal. Every literal has a type for instance,
‘B’ is a character literal
“The New World” is string literal
12.674 is either float or double literal
In case of 12.674 it can be either a float or double literal(by default it is double).
Unicode characters prefixes and suffixes
For data types handling Unicode characters a value is added in front. This value is known as prefix and it helps identify the characater as a Unicode character.
There are aslo suffixes where tha value is added at the back. Using the prefixes or suffixes we can explicitly tell the compiler to treat the literal as the specified type. The table below shows the data types with their corresponding suffix and prefix value.
Data type | Prefix |
wchar_t | L |
wstring | L |
char16_t | u |
char32_t | U |
Data type | Suffix |
Unsigned | u or U |
long | l or L |
long long | ll or LL |
float | f or F |
long double | l or L |
Note:Small letter ‘l‘ is confusing with 1 so use the upper case ‘L‘ for long type, ‘LL‘ for long long and ‘L‘ for long double.
Using suffix or prefix is pretty simple, for instance,
“The new world” is a string
L“The New World” is a wide string.
12.34f is a float (size allocated is 4 bytes)
12.34L is a long double(size allocated is 12 bytes).