constants, manipulators and operator precedence in c++
#include<iostream>
#include<iomanip>
using namespace std;
int main (){
// int a=5;
// cout<<"the value of a was "<<a<<endl;
// a=7;
// cout<<"the value of a is "<<a<<endl;
//here the value of a changed from 5 to 7
//constants in c++ (to not let change the value of a we use constants)
// const int a=5;
- The const variable cannot be left un-initialized at the time of the declaration.
// cout<<"the value of a was "<<a<<endl;
// a=7;
// cout<<"the value of a is "<<a<<endl;
//here it will show an error because a is a constant
//manipulators in c++
//manipulators vo operators hote h jo data display ki formatting m help krte h for ex:endl
int a=5, b=57, c=5757;
cout<<"the value of a without setw is "<<a<<endl;
cout<<"the value of b without setw is "<<b<<endl;
cout<<"the value of c without setw is "<<c<<endl;
//setw comes from iomanip file
cout<<"the value of a is "<<setw(4)<<a<<endl;
cout<<"the value of b is "<<setw(4)<<b<<endl;
cout<<"the value of c is "<<setw(4)<<c<<endl;
//setw kitna space chodhna usme kaam ata h
//operator precedence in c++
int k=3;
int s=2;
// int ks=k*5+s; //yaha bodmas ka use nhi operator precedence ka use hoga check on en.cppreference sit
int ks=(((k*5+s)-59)+393);//ab + aur - m operator precidency same h to hum associativity dekhenge which is from left to right. mtlb left se right evaluate hoga
cout<<ks<<endl;//perecedence ka mtlb h sbse pehle konsa operator use hoga
//yaha multiply pehle hoga coz opertor precendence m vo pehle ata
return 0;
}
Comments
Post a Comment