Every program has set of instructions which work on some data.
Variables
Variables are the buckets in the memory that hold some type of data, for example:
- Integer : 4
- Float : 2.5
- Character : a
- String : βHelloβ
Variable name: A label for a memory location. Value: The something that would be stored in a variable. Storage: A place where data can be stored. Declaration: Announcing a variable (usually) at the beginning of a program. Naming Convention: A set of rules about the names of variables. Assignment: Giving (setting) a variable a value.
Naming Convention
- For variable name we can use uppercase and lowercase letters, digits from 1 to 9 and underscore.
- First character must be underscore or letter.
- C++ is strongly typed language. So every variable needs to be declare before using it.
// Valid names
int topics = 3;
int marks = 20;
int student_i = 5;
// Invalid names
1_student = 8;
Variable Initialization
Variables when just declared have garbage value until they are assigned a value for the first time. We can assign a specific value from the moment variable is declared, called as initialization of variable.
// Declare
int marks; // will contain some garbage data
// Declare + Assign (Init)
int x = 10;
Variable assignment
We can assign a specific value to the variable using β=β operator, called as assignment of a value to a variable. Variables by default contain a garbage value.
// Declare
int marks;
// Assignment
marks = 20;
// Assignment
marks = marks + 20;
Basic Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on variables and data. We can perform basic operations on variables using operators:
Operator | Function |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulo |
FAQ
Division Operation (a / b) in our program. The / operator is the division operator. If an integer is by another integer, we will get the quotient. However, if either divisor or dividend is a floating-point number, we will get the result in decimals.
NOTE
The modulo operator % computes the remainder. When a = 9 us divided by b = 4, the remainder is 1.
Datatype
Data comes in different types - such as integer, floats, string, boolean values etc. When you create the variable, you reserve some space in memory. Memory is allocated based on the variableβs datatype. The amount of memory required depends on the datatype.
// Here int, char, bool, are the datatypes
int marks = 80;
char letter = 'A';
bool isRainy = false;
Basic Data Types Variables
Also know as primitives & it types are as follows:
- int : 5, 37, 17834276
- char : βMβ, β@β, βaβ, β5β
- bool : true or false
- float : 16.25, 17.6756
- double : Higher precision; for e.g. value of a pi
- void : nothing
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
// Variable Init
int a = 10;
float b = 20.56; // By default it will print the output upto 4 places of decimal
double e = 20.7876786587;
char c = 'A';
bool d = true;
// Print
cout << a << endl;
cout << b << endl;
cout << fixed << setprecision(6) << e << endl;
cout << c << endl;
cout << d << endl;
return 0;
}
Size of Operator
The sizeof is a keyword, but it is a compile-time operator that determines the size, in bytes, of a variable or data type. The sizeof operator can be used to get the size of variables, classes, structures like 10. Arrays, and any other user defines datatype.
#include<iostream>
using namespace std;
int main(){
int marks = 10;
cout << sizeof(marks) << endl; // Output = 4 bytes & 1 byte = 8 bits
cout << sizeof(float) << endl; // Output = 4
cout << sizeof(double) << endl; // Output = 8
cout << sizeof(char) << endl; // Output = 1
cout << sizeof(boolean) << endl; // Output = 1
return 0;
}
Data Type Modifiers
- Long : 64 bit which is double the normal int type.
- Short : 16 bit which is half the normal int type.
- Signed : if MSB is 1 then it is negative number & if MSB is 0 then it is a positive number. The remaining 31 bits represent the magnitude. By default all integers are signed and MSB is also called the signed bit.
- Unsigned : All 32 bits will be using for storing the data.
#include<iostream>
using namespace std;
int main(){
int marks = 10;
long int number = 12232132132312322321; // more than 10^9
int number_int = number; // memory overflow will happen
cout << number << endl;
cout << number_int << endl;
return 0;
}
Typecasting
Implicit Typecasting
#include<iostream>
using namespace std;
int main(){
cout << (5/3) << endl // output = 1
// Typecasting -> change of the result datatype and the output will be on the basis of the priority list
// Implicit Typecasting (Compiler automatically changes the result)
// Float + Int
cout << (5/3.0) << endl; // output = 1.66667
cout << (5.0/3) << endl; // output = 1.66667
// Char + Int
char letter = 'A';
cout << letter + 1 << endl; // output = 66
letter = letter + 1;
cout << letter << endl; // output = B
// Boolean + Int = Int
cout << (false + 5) << endl; // output = 5
return 0;
}
Priority list:
Floot
ordouble
Int
char
bool
Explicit Typecasting
#include<iostream>
using namespace std;
int main(){
// Int
cout << (float)5/3 << endl; // output = 1.66667
// Char + Int
char letter = 'A';
cout << char(letter + 1) << endl; // output = B
return 0;
}