// this section is to explain data types in cpp
/* int x=5; here x is variable and it is of data type integer.
we have following data types:
1. intger (1,2,3..-4,-556, ., int)
2. char (characters, strings?)
3. flaoting point (double,float)
4. bool
5. arrays
6. classes
7. structures
5/2=2
5.0/2=2.5
5/2.0=2.5
#include<iostream>
int main()
{
std::cout<<5.0/2<<"\t"<<5/2<<"\t"<<5/2.0<<std::endl;
}
follwing code will generate the following output:
2.5 2 2.5
int data types stores less memory.
int x=9999999999999999; will generate following error.
p5_data_types.cpp:47:8: warning: overflow in conversion from ‘long int’ to ‘int’ changes value from ‘99999999999999’ to ‘276447231’ [-Woverflow]
47 | int x=99999999999999;
*/
#include<iostream>
#include <climits>
#include<float.h> //will help us for FLT_DIG use which tells us the number of significant digits float,double, long double are trustworthy.
int main()
{
short a;
int b; //at least 16 bits, usually 32 bits
long c; //32 bits
long long d; //64 bits
//short<<int<<long<<long long
//each of these have unsigned counterparts, unsigned one's will only allow positive values
unsigned short aa;
unsigned int bb;
unsigned long cc;
unsigned long long dd;
using std::cout;
using std::endl;
char x=50;
cout<<"the value of x won't be what has assigned :"<< x<<endl;
cout<<"sizeof char in bytes "<<sizeof(char)<<endl;
cout<<"sizeof(short) in bytes"<<sizeof(short)<<endl;
cout<<"sizeof(short int) in bytes"<<sizeof(short int)<<endl;
cout<<"sizeof(int) in bytes"<<sizeof(int)<<endl;
cout<<"sizeof(long) in bytes"<<sizeof(long)<<endl;
cout<<"sizeof(double) in bytes"<<sizeof(double)<<endl;
cout<<"sizeof(long int ) in bytes"<<sizeof(long int)<<endl;
cout<<"sizeof(long long) in bytes"<<sizeof(long long)<<endl;
cout<<"sizeof(long double) in bytes"<<sizeof(long double)<<endl;
cout<<"sizeof(long long int) in bytes"<<sizeof(long long int)<<endl;
cout<<"sizeof(unsigned short) in bytes"<<sizeof(unsigned short)<<endl;
cout<<"sizeof(unsigned int) in bytes"<<sizeof(unsigned int)<<endl;
cout<<"sizeof(unsigned long) in bytes"<<sizeof(unsigned long)<<endl;
cout<<"sizeof(unsigned long long) in bytes"<<sizeof(unsigned long long)<<endl;
cout<<"sizeof(short) "<<SHRT_MIN<<" TO "<<SHRT_MAX<<endl;
cout<<"sizeof(int) "<<INT_MIN<<" TO "<<INT_MAX<<endl;
cout<<"sizeof(long) "<<LONG_MIN<<" TO "<<LONG_MAX<<endl;
cout<<"sizeof(long long) "<<LLONG_MIN<<" TO "<<LLONG_MAX<<endl;
cout<<"sizeof(unsigned short) "<<USHRT_MAX<<endl;
cout<<"sizeof(unsigned int) "<<UINT_MAX<<endl;
cout<<"sizeof(unsigned long) "<<ULONG_MAX<<endl;
cout<<"sizeof(unsigned long long) "<<ULLONG_MAX<<endl;
bool am_I_ok=true;
cout<<bool(am_I_ok)<<endl;
cout<<std::boolalpha<<am_I_ok<<endl;
float aaa=10.0/3;
double bbb=10.0/3;
long double ccc=10.0/3;
cout<<std::fixed<<aaa*1000000000000000000<<" \twhich is true upto "<<FLT_DIG<<" digits"<<endl;
cout<<std::fixed<<bbb*1000000000000000000<<" \twhich is true upto "<<DBL_DIG<<" digits"<<endl;
cout<<std::fixed<<ccc*1000000000000000000<<" \twhich is true upto "<<LDBL_DIG<<" digits"<<endl;
/*
333333326835482624.000000 which is true upto 6 digits
3333333333333333504.000000 which is true upto 15 digits
3333333333333333481.250000 which is true upto 18 digits
*/
}
/* Purpose of char data types is to store small numbers or characters
It is characterised in integer data types which we will see.
sizeof(char) is just 8 bits or 1 byte.
char x=''; single quotes for individual characters, x='a', double quotes are
reserved for strings.
signed char stores value from -128 to 127 and unsigned char stores values from 0 to 255.
signed char x=128;
cout<<(int) x<<std::endl; //will not provide us 128 but -127
unsigned char x=128;
cout<<(int) x <<std::endl; //will print 128.
char x='A';
cout<<(int) x ; will print 65 which is ASCII value of char A.
char x=55;
cout<<(int) x; will print 55 ;
char x=55;
cout<<x; // will print 2
char16_t and char32_t stores characters from different language(hindi,....)
these char data types gives us enough memory to store different characters.
ESCAPE SEQUENCE:
\t,\n,
\b delete a charater.
\n for next line.
\t for tab.
\v for verticl tab.
\a for ?
\0 for null terminating character.
\" "\ is used to put quotes around in cout<<"I am \"vishal"\ "; will print I am "vishal".
\\ is used to print a single backslash.
look on internet for full list of escape sequences.
Bool Data types:
bool pizza_is_bad=true; or bool pizza_is_bad=1; we can use both.
cout<<pizza_is_bad ; and cout<<bool(pizza_is_bad); will print 1.
purpose of bool variable : it is used as flag.
cout<<std::boolalpha<<pizza_is_bad<<endl; will print true or false rather than 1 or 0.
or we can use if statement:
if(pizza_is_bad) cout<<"true";
Floating point data types(float and double and long double):
int main()
{
float a; //only trustworthy upto certain significant digits.
double b;
long double c;
}
Constants:
1.const int x=5; //now x is a constant, it is no longer a variable.
x=10; will throw some error. This is called symbolic constant.
const int x;
x=5; will also throw some error, as x is not a variable.
2.Another way to declare constant is outside main()
#include<iostream>
#define x 10 //here x is constant (this is called macro constant)
int main()
{
}
3.Another way is to via enum: {enum y=100}; is used inside main().
*/