LEARNING C++ DAY-2
Variables
A variable is a container (storage area) used to hold data.
Each variable should be given a unique name (identifier).
int a=2;
Here a is the variable name that holds the integer value 2.
The value of a can be changed, hence the name variable.
There are certain rules for naming a variable in C++
- Can only have alphabets, numbers and underscore.
- Cannot begin with a number.
- Cannot begin with an uppercase character.
- Cannot be a keyword defined in C++ language (like int is a keyword).
Fundamental Data Types in C++
Data types are declarations for variables. This determines the type and size of data associated with variables which is essential to know since different data types occupy different size of memory.
1 int
- This data type is used to store integers.
- It occupies 4 bytes in memory.
- It can store values from -2147483648 to 2147483647. Eg. int age = 18
2 float and double
- Used to store floating-point numbers (decimals and exponentials)
- Size of float is 4 bytes and size of double is 8 bytes.
- Float is used to store upto 7 decimal digits whereas double is used to store upto 15 decimal digits. Eg. float pi = 3.14 double distance = 24E8 // 24 x 108
3 char
- This data type is used to store characters.
- It occupies 1 byte in memory.
- Characters in C++ are enclosed inside single quotes ͚ ͚.
- ASCII code is used to store characters in memory. Eg͘ char ch= 'a';
4 bool
- This data type has only 2 values ʹ true and false.
- It occupies 1 byte in memory.
- True is represented as 1 and false as 0. Eg. bool flag = false
#include <iostream>
using namespace std;
int main(){
int a; //declaration
a=12;
cout<<"size of int "<<sizeof(a)<<endl;
float b;
cout<<"size of float "<<sizeof(b)<<endl;
char c;
cout<<"size of char "<<sizeof(c)<<endl;
bool d;
cout<<"size of bool "<<sizeof(d)<<endl;
return 0;
}
OUTPUT
size of int 4
size of float 4
size of char 1
size of bool 1
C++ Type Modifiers
Type modifiers are used to modify the fundamental data types.
*************** THANK YOU ALL FOR READING **********************
@pranvmadhavan1
INSTAGRAM : @_pra.n.v
Top comments (0)