Professional Documents
Culture Documents
Modifiers - Literals - Referencevariable - Typecasting - Constants and Manipulators in C++
Modifiers - Literals - Referencevariable - Typecasting - Constants and Manipulators in C++
Ans – Modifiers are the keywords which is used to modify the datatype and their meaning
such as signed, unsigned, long, short
Datatype Variable
name
Question – Can we print a global variable inside a function when there is a variable
having same name?
ANS – Yes! Using Scope Resolution operator (::) we can call the global variable inside the
function when there is already exiting variable having same name.
#include <iostream> Output
using namespace std; The value of local variable is: 50
The value of local variable is: 25
int a = 25;
int main()
{
int a = 50;
cout << "The value of local variable is: " << a << endl;
cout << "The value of global variable is: " << ::a;
return 0; Scope Resolution
Operator
}
The scope Resolution operator (::) is used to access global variable inside a function.
Program 2 –
#include <iostream> Output
using namespace std; the sum of float a and int b is: 80
the sum of int a and int b is: 80
int main() the sum of float a and float b is: 80.54
{ the sum of double a and float b is: 80.54
int a = 45;
float b = 35.54;
cout<<"the sum of float a and int b is: "<<float(a)+int(b)<<endl;
cout<<"the sum of int a and int b is: "<<int(a) + int(b)<<endl;
cout<<"the sum of float a and float b is: "<<float(a)+float(b) <<endl;
cout<<"the sum of double a and float b is: "<<double(a)+float(b)<<endl;
return 0;
}
Ques. What is constants in C++?
Ans – Constants are the non-variable values ones assigned; cannot be manipulate (change)
by a function or method. The keyword ‘const’ is used to create a constant.
#include<iostream> Output:
using namespace std;
mian.cpp: In function 'int main()':
int main() mian.cpp: 7:6: error: assignment of read-only variable 'x'
{ x+=50;
const int x = 10; //declaration a constant int ^
x+=50; //changing the value of constant integer
cout<<x; // pass an error
return 0;
}
return 0;
}