Variables , identifier,initialization , assignment , 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.
Variables are name given to a memory space by the programmer. There are different data types in C++ allowing the programmer to utilized all the known letters , symbols , numbers,etc. but how do we utilized them in our program.For instance you want to make a calculator program ,then this program must perform operation such as :addition,subtraction,division,etc. between two or more numbers.This in turn means the integers should be made available to the program and the program should deploy some means to hold the integers value which it will utilized to perform the mathematical operation .Here,variables comes into play.The program will use some space in the memory to store the value using a variable.These variables in a program usually have some names(it can be any names) through which the value store in the memory is accessed.To implement a variable in a program a method known as declaring a variable is used.
Note::Another way of interpreting a variable is,say our Earth is a memory and every person is a data or a value or a piece of memory with the data.To find a specific person which is the first information that is required? of course a person’s name .If we know the person’s name finding that person becomes easy(of course we also need his address/nationality but let’s keep that aside).In Computer programming variable has the same valuable necessity as the person name on Earth.So a variable is defined as a name given to a memory space(some specific space) by the programmer.
How to declare a variable is shown below.
int i ; ///i is the name of the variable
Declaring a variable “i” for int data type is same as telling the compiler “Allocate 4 bytes(since int size is 4 bytes) size of memory that can hold only integer value and name the memory space as ‘i’“.This name will be used later to access whatever value is present in that memory storage.Before we can access the value we have to initialized a value to a variable.To initialize a variable we use the sign ‘=’ and write the value next to it,for instance.
int ii=i ; ///The value of i is initialized to ii
Now ‘i’ memory store the value 90 and also ii has the value 90.
Earn money($$$) from your website-Join Now!
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 replaced somewhere else with another value then it is known as assigning a variable.For instance look at the program below.
using namespace std;
int main( )
int ii=i1 ; /// ‘ii’ is initialized
i=ii ; /// i is assigned(not initialized) a value 12
cout << i << ii << endl ;
Variables naming convention
Some people suggest a variable should always begin with a lower case and the name must have certain significance related to what it is named for and must be understandable for instance, you can use ‘next_link’ or ‘nextLink’ not ‘nextlink’ ,while using linked list.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 is up to you because you are the programmer,but make sure that it is recognizable and not simply use whatever names come to your mind and creating a lot of trouble later figuring out which name stands for which value.
Identifier are the names given to all the program entities.The names given to a variable or a name given to the functions(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 are names which have a 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 keyword in our program.Some of the keywords in C++ are int , float , new,delete , namespace, etc.
Operators are signs or symbols that can perform certain operations.There are many types of operator in C++ for instance,a mathematical operator.Such 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.
The element on which or between which an operation is perform are known as operand.Let’s look at the expression,a+b, 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 it’s 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.
In an expression where many operators are used,the order for performing the operation is called precedence.Visit this link for more information Precedence and Associativity in C and C++.
If the operators have same precedence then the order for performing operation is known as associativity.We will discuss more about precedence and associativity later in the chapter.Visit this link for more informationPrecedence and Associativity in C and C++.
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) but we can make 12.674 as float by adding a suffix.For data types handling Unicode characters the value is added in front so it is prefix.Using this prefix or suffix value we can explicitly tell the compiler to treat the literal as the given specified type.The table below shows the data types with their corresponding suffix and prefix value.
|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 upper case L for long , ‘LL‘ for long long and ‘L‘ for long double.
Using either 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).