1. C++ variables
<data-type> <variable-name>;
int var = 0; // declaration and initialization
2. C++ Data types
unsigned char a = 'a';
unsigned short int b = 10;
unsigned long int c = 3456;
The difference between signed and unsigned are, unsigned variables will not store the sign of a number. To store the sign, it will take 1 byte of data.
Below example we use “sizeof” operator to know how much each variable type is taking the memory.
#include<iostream>
using namespace std;
int main()
{
cout<<"The size of int is: "<<sizeof(int) <<endl;
cout<<"The size of char is: "<<sizeof(char) <<endl;
cout<<"The size of short int is: "<<sizeof(short int) <<endl;
cout<<"The size of long int is: "<<sizeof(long int) <<endl;
cout<<"The size of float is: "<<sizeof(float) <<endl;
cout<<"The size of double is: "<<sizeof(double) <<endl;
return 0;
}
The size of int is: 4
The size of char is: 1
The size of short int is: 2
The size of long int is: 8
The size of float is: 4
The size of double is: 8
3. C++ Enums:
enum <enum-variable-name> { list of names} variable-list;
enum week {sunday, monday, tuesday, wednesday, thursday, friday, saturday} weekOfDay;
week myWeek;
Method 1:
Declare a enum variable during enum declaration.
#include<iostream>
using namespace std;
enum week {sunday, monday, tuesday, wednesday, thursday, friday, saturday} weekOfDay;
int main()
{
weekOfDay = sunday;
cout<<weekOfDay<<endl;
weekOfDay = monday;
cout<<weekOfDay<<endl;
weekOfDay = tuesday;
cout<<weekOfDay<<endl;
return 0;
}
Output:
0
1
2
Method 2:
Declare a enum variable as a normal variable.
#include<iostream>
using namespace std;
enum week {sunday, monday, tuesday, wednesday, thursday, friday, saturday};
int main()
{
week weekOfDay = wednesday;
cout<<weekOfDay<<endl;
weekOfDay = thursday;
cout<<weekOfDay<<endl;
weekOfDay = friday;
cout<<weekOfDay<<endl;
return 0;
}
Output:
3
4
5
All enum variables will start with the value of 0.
You can change the default value, by assigning a value during enum declaration as shown below.
The subsequent options will take the next number.
#include<iostream>
using namespace std;
enum week {sunday =10 , monday, tuesday, wednesday, thursday, friday, saturday};
int main()
{
week weekOfDay = sunday;
cout<<weekOfDay<<endl;
weekOfDay = monday;
cout<<weekOfDay<<endl;
weekOfDay = friday;
cout<<weekOfDay<<endl;
return 0;
}
Output:
10
11
15
4. C++ scope of variable:
1. Local Variables.
2. Global Variables.
1. Local Variables:
1. They are defined inside a function.
2. There lifetime is till the execution of that function.
3. The variables that are in the function arguments are called as formal parameters.
Example:
#include<iostream>
using namespace std;
int main()
{
int i; //Declaration of local variables
float f;
i = 10; //initialization of local variables
f = 34.56;
return 0;
}
2. Global variables:
1. They are defined outside of a function.
2. Their life time is till the execution of the program.
3. They are available for all the functions in that file or project.
4. If there is a local variable of the same name, then local variable to that function is given priority.
Example:
#include<iostream>
using namespace std;
int global = 30;
int i = 50;
int main()
{
int i; //Declaration of local variables
float f;
i = 10; //initialization of local variables
f = 34.56;
cout<<"The value of global is : "<<global<<endl;
cout<<"The value of i is : "<<i<<endl;
return 0;
}
Output:
The value of global is : 30
The value of i is : 10
5. C++ Storage Classes:
There are 5 storage classes in CPP.
auto
register
static
extern
mutable
Storage classes helps in the visibility and scope of the variables.
auto
1. When you declare a local variable, it will be automatically taken as auto.
2. The scope is till the execution of the function.
3. The lifetime of the variable is till the execution of a function.
4. initial value will be garbage value.
5. We use the keyword “auto” to define a automatic variable.
Example:
#include<iostream>
using namespace std;
int main()
{
int i = 10;
cout<<"The value of i is : "<<i<<endl;
return 0;
}
Output:
The value of i is : 10
register:
1. We can only declare local variables as register.
2. The scope is till the execution of the function.
3. The lifetime of the variable is till the execution of a function.
4. initial value will be garbage value.
5. It is a request to the compiler to store the variable in the computer register. It is not guaranteed that it will be able to do it.
6. We use the keyword “register” to define a register variable.
Example:
register int i = 10;
static
1. When you declare a local variable as static new instance of that variable is not created for each function call.
2. The scope is till the execution of the function.
3. The lifetime of the variable is till the execution of a function.
4. initial value will be zero.
5. If a global variable is declared as static, then that variable is visible to that file only.
6. We use the keyword “static” to define a static variable.
Example:
#include<iostream>
using namespace std;
int func()
{
static int i = 1;
int j = 1;
i++;
j++;
cout<<"The value of i = "<<i<<endl;
cout<<"The value of j = "<<j<<endl;
return 0;
}
int main()
{
func();
func();
func();
func();
func();
return 0;
}
Output:
The value of i = 2
The value of j = 2
The value of i = 3
The value of j = 2
The value of i = 4
The value of j = 2
The value of i = 5
The value of j = 2
The value of i = 6
The value of j = 2
As you can see, value of i is stored and incremented for each function call, but value of j is reset for each function call.
extern
1. We use extern variable to access a variable from another file.
2. That another file should belong to the same project.
3. We use the keyword “extern” to define an extern variable.
1.c
#include<iostream>
using namespace std;
extern int i;
int main()
{
cout<<"The i value is :" <<i <<endl;
return 0;
}
2.c
#include<iostream>
using namespace std;
int i = 10;
Output:
g++ 2.cpp 1.cpp
The i value is :10
Mutable
Mutable storage class are applicable to objects only. We shall discuss in further chapters.
6. C++ Escape Sequences
\' single quote
\0 Null
\\ backslash
\a audible bell
\" double quote
\b backspace
\? question mark
\f form feed - new page
\n new line
\r carriage return
\t horizontal tab
\ooo octal number