C++ variables , identifiers and Data types
- Variables in C++:The variables in C++ are containers in which we some data or values when our code is executing. These are like bucket to store any of different types such as it may be integer , character etc.
The variables may be declared as follows:
Type variable_name[=value];
The example will give any clear understanding to variables:
int apples=4;//Here int is an data type of variable name as apple which store 4 as value into it.
There are some naming conventions of c++ to be known before knowing some rules :
1)lowercase convention is like all the letters in a variable must be in small letters. Ex: teacontainer
2)uppercase convention is like all the letters in a varible must be written in capital letters. Ex: TEACONTAINER
3)camelcase convention is like first word to be in small letters and if it is combination of two words the second word must first letter must be written in capital letter. Ex:teaContainer. This case is always used in c++ while declaring variables of two words.
Rules for naming variables in c++:
Rule 1:Variable name should not begin with a number. Ex: 2xyz is not allowed but xyz2 is allowed.
Rule 2: Whitespace is not permitted in variable names. Ex: tea container is not allowed nut tea_container is allowed.
Rule 3:A C++ keyword(reserved word) cannot be used as a variable name. Ex: int cout;//This is not allowed as cout is an reserved keyword of c++,but int apple is allowed.
Rule 4: It is preferred to use variable names with more than one word with all the lowercase letters for the first word and capitalization of the first letter while defining and declaring an variable in c++.Ex: bubbleSort().
C++ Datatypes while declaring any variable to restrict the type of data to be stored. In simple words like take an example of no. of fruits which are in numbers that are be to be stored in 'INT' datatype which is an integer datatype. In c++ there are many types of datatypes with different memory allocations according to there size.
There are 3 types of datatypes in c++:
1)Pimitive Data Types:These type of data types are directly used by user which are not needed to be defined and are already defined. The most commonly used primitive datatypes are: INT,FLOAT,CHAR etc with different memory allocations like int datatype has 4 bytes of memory to store the data.
2)Derived Data Types:These type of data types are dervied from primitive data types.Ex:Arrays,pointers,functions etc.
3)User-defined datatypes:These types of datatypes are derived by itself.Ex: Class etc.
examples of primitive data type with code:
#include<iostream>
using namespace std;
int main(){
int apple=4;
float area=87.88;
cout<<"I'M apple with integer datatype storing value:"<<apple<<endl;
cout<<"I'm area with float datatype storing value:"<<area<<endl;
return 0;
}
output: I'M apple with integer datatype storing value:4
I.m area with float datatype storing value:87.88
In upcoming blogs i will deeply mention about the other dataypes with detail explaination about it.