GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 184 of 453    Print  
What would be the output of the following program?
main()
{
const int x=5;
int *ptrx;
ptrx=&x;
*ptrx=10;
printf("%d",x);
}
A) 5
B) 10
C) Error
D) Garbage Value



  
Total Answers and Comments: 11 Last Update: March 19, 2008   
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: louisyang
 

It depends on compiler.

In C++, it is not allowed to convert const int* to int*. So error

In C, it is ok, and i = 10; So dont use C compiler, it is time to use C++ compiler, just my one cent



Above answer was rated as good by the following members:
sittoop
  Sorting Options  
  Page 1 of 2   « First    1    2    >     Last »  
January 23, 2006 23:44:25   #1  
javedamaan21 Member Since: January 2006   Contribution: 2    

RE: What would be the output of the following program?...

answer will be option (c).that is error.

Here we are trying to modify the constant value of x to 10 which should not be done.. so it will pop an error saying that cant modify a constant value


 
Is this answer useful? Yes | No
January 24, 2006 08:56:55   #2  
kiranck007 Member Since: January 2006   Contribution: 11    

RE: What would be the output of the following program?...

B

actually there will be comiler warning....but the value will be changed when u run the program


 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 2Overall Rating: -2    
March 17, 2006 05:21:12   #3  
Bhanu        

RE: What would be the output of the following program?...

Ans: a) 5

Hello

Here we are assiging address of x to ptrx so we are not changing

the value of X So value of x is 5


 
Is this answer useful? Yes | No
March 29, 2006 14:26:11   #4  
louisyang Member Since: December 2005   Contribution: 4    

RE: What would be the output of the following program?...

It depends on compiler.

In C++ it is not allowed to convert const int* to int*. So error

In C it is ok and i 10; So dont use C compiler it is time to use C++ compiler just my one cent


 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
April 11, 2006 04:11:38   #5  
satyanarayana.p        

RE: What would be the output of the following program?...

hai all

Ans is 10;

Because we r using pointer to chane the value of the variable.So it should be ans is 10.

if u give ptr 10 in above question then ans is 5 only


 
Is this answer useful? Yes | No
February 29, 2008 12:49:44   #6  
Rahul_chandra Member Since: February 2008   Contribution: 3    

RE: What would be the output of the following program? main() { const int x=5; int *ptrx; ptrx=&x; *ptrx=10; printf("%d",x); }
here an error because we can not convert const int * to int *
 
Is this answer useful? Yes | No
February 29, 2008 12:53:10   #7  
Rahul_chandra Member Since: February 2008   Contribution: 3    

RE: What would be the output of the following program? main() { const int x=5; int *ptrx; ptrx=&x; *ptrx=10; printf("%d",x); }
it is not a matter of compiler it is matter of our fundamental
so fundamental is that we can not change constant .
so there is error. if talk to turbo compiler then run this program without error.
and i m telling that turbo compiler has sevrak mistakes that confuse a begginer.

 
Is this answer useful? Yes | No
February 29, 2008 12:54:43   #8  
Rahul_chandra Member Since: February 2008   Contribution: 3    

RE: What would be the output of the following program? main() { const int x=5; int *ptrx; ptrx=&x; *ptrx=10; printf("%d",x); }
it is not a matter of compiler it is matter of our fundamental
so fundamental is that we can not change constant .
so there is error. if talk to turbo compiler then run this program without error.
and i m telling that turbo compiler has sevral mistakes that confuse a begginers.

 
Is this answer useful? Yes | No
March 04, 2008 14:27:10   #9  
bismitapadhy Member Since: July 2007   Contribution: 5    

RE: What would be the output of the following program? main() { const int x=5; int *ptrx; ptrx=&x; *ptrx=10; printf("%d",x); }
Ans is 10

const int x; means x is constant.We can't change value of x but we can chage *ptrx.

But still we will get a warning suspecious pointer conversion.

 
Is this answer useful? Yes | No
March 05, 2008 06:39:54   #10  
ravidhingra89 Member Since: March 2008   Contribution: 1    

RE: What would be the output of the following program? main() { const int x=5; int *ptrx; ptrx=&x; *ptrx=10; printf("%d",x); }
c
 
Is this answer useful? Yes | No
  Page 1 of 2   « First    1    2    >     Last »  


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape