CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C DETAILS
Question :
What is the benefit of using const for declaring constants?



Category C Interview Questions
Rating (0.0) By 0 users
Added on 7/19/2006
Views 1316
Rate it!
Answers:

If we use const variable instead of #define, we can specify the scope of the variable.



1)CONST is a keyword in c++.
2)If a variable is declared as const the value of the variable will not change through out the program.
3)example:
void main()
{
clrscr();
const a=40;
for(int i=0;i<=100;i++)
a++;
cout<<a;//a=40;
}
in the above program the output will be 40.



 Posted by: kishore reddy    

Contact kishore reddy  Contact kishore reddy

By declaring a data type as const it can never be modified at any point in the program.

i.e

const int n=10;
cin>>n;

This throw an error.



 Posted by: Ray    

Contact Ray  Contact Ray

Declaring a const variable instead of using #define allows you to specify a data type for the constant. The const modifier itself prevents the variable from being accidentally modified in an expression and gives the compiler the opportunity to substitute the assigned value as an optimization.



 Posted by: Joel Corley    

Contact Joel Corley  Contact Joel Corley


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
What is the difference between declaring a variable and defining a variable?


View Answer
What is a const pointer?
View Answer
What is an lvalue?


View Answer
When is a switch statement better than multiple if statements?


View Answer
Can static variables be declared in a header file?


View Answer
Difference between Funtion to pointer and pointer to function

View Answer
What is #line used for?


View Answer
Can a variable be both const and volatile?


View Answer
All the programs are tested under Turbo C/C++ compilers.
It is assumed that,
?? Programs run under DOS environment,
?? The underlying machine is an x86 system,
?? Program is compiled using Turbo C/C++ compiler.
The program output may depend on the information based on this assumptions (for example sizeof(int) == 2 may be assumed).

Predict the output or error(s) for the following:

i) void main()
{
int const * p=5;
printf("%d",++(*p));
}


View Answer
main()
{
float me = 1.1;
double you = 1.1;
if(me==you)
printf("I love U");
else
printf("I hate U");
}


View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/6059/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.94