GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 80 of 208    Print  
What is difference between followin intialization.

int iVar1;
int iVar2 = int();

and which one of two should we prefer always and why?

  
Total Answers and Comments: 7 Last Update: November 17, 2008     Asked by: mohit12379 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
April 26, 2006 06:40:47   
Preeti Yadav Member Since: April 2006   Contribution: 30    

RE: What is difference between followin intialization....
in first case a variable will be create in memeory with the default base type value (depending upon compiler 2 compiler) bcoz it is not initialized. in second case the variable will be created in the memory with the value retuned by the function int() (if int is a user define function) the second statement should have been int *i new int();
 
Is this answer useful? Yes | No
July 28, 2006 16:30:44   
mohit12379 Member Since: March 2006   Contribution: 17    

RE: What is difference between followin intialization....

@PRITI YADAV HAV U TRIED THE FOLOWING STATEMENT EVER

int iVar2 int();

PLS TRY IT....

FOR UR KIND INFO :- int * ivar2 new int(); and int iVar2 int(); are two different statement... thas why i have asked this que.... DNT MIX IT.



 
Is this answer useful? Yes | No
September 13, 2006 10:13:46   
ramachandra        

RE: What is difference between followin intialization....
Preeti Yadav is right Ivar1 is not initialized but in the second case compiler initilizes the Ivar2 to sero. create a console program and use COUT to print Ivar1 and Ivar2 and observe that Ivar1 prints junk value and Ivar2 value will be zero
 
Is this answer useful? Yes | No
October 15, 2006 07:27:39   
Shiva Shankar Anumula        

RE: What is difference between followin intialization....


int iVar1;
int iVar2 int();

In first case memory fpr iVar1 is allocated and it is not intialized.

second case is used to intialize the '0' to ivar2


 
Is this answer useful? Yes | No
November 07, 2006 05:20:31   
Ashish        

RE: What is difference between followin intialization....

int iVar1 does not initalize the iVar1. Its C style of declation of integer.

whereas in C++ everything is implemented using classes even basic built-in data types.

int iVar2 int();

so int() is nothing but constructor that creates one object of int type and assigned to iVar2. (means assignment operator is overloaded). try int(5)... i.e.

int iVar2 int(5);

in this case iVar2 is initalized to 5.


 
Is this answer useful? Yes | No
May 21, 2007 15:27:31   
Anil        

RE: What is difference between followin intialization....
depends

if use in main both will get initialize to '0'.
ie: global

if use inside a function (local) iVar1 will be garbage iVar2 will be initialised to '0';

 
Is this answer useful? Yes | No
November 17, 2008 02:28:58   
mucdull Member Since: November 2008   Contribution: 6    

RE: What is difference between followin intialization.int iVar1;int iVar2 = int();and which one of two should we prefer always and why?
int iVar1; // iVar1 is never initialized
int iVar2 int(); // iVar2 will always be initialized with some initial value

The latter should be used because use of uninitialized variable is dangerous and can cause runtime check failure for some C++ compiler.



 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

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

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

Page copy protected against web site content infringement by Copyscape