Forums.Sureshkumar.net : A Perfect Place to Share Knowledge         Blogs     Games    Magazines    

"Sharing knowledge does not lessen your store, often it gets you more. Sharing plays a key role in relationships and bonding, happens in small steps and is assisted through community membership."

Go Back   SURESHKUMAR.NET FORUMS > TECHNICAL DISCUSSIONS > DATA STRUCTURES, C, C++, VC ++ > ds. c , c++ Interview / Technical Questions
Register FAQ Members List Calendar Games Blogs Search Today's Posts Mark Forums Read

ds. c , c++ Interview / Technical Questions Kindly solve an many as questions you can. It will sharpen your skills and those solutions will help others too.

   

Reply
 
LinkBack Thread Tools Rate Thread Display Modes
Old 02-05-08, 12:04 PM   #1 (permalink)
Junior Member
 
Join Date: Sep 2007
Age: 25
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 2 gouriproutray is on a distinguished road
Smile WHY THIS CPP PROGRAM SHOWS AN ERROR

#include<iostream.h>
int main()
{
if(1)
int x;
x++;
cout<<x;
}
/*WHY THE DECLARATION CANNOT BE APPLIED HERE. IN C++ U CAN DECLAR ANY VARIABLE ANY WHERE BEFORE U USE THIS,THEN WHY THIS SHOWS AN ERROR*/
gouriproutray is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 02-05-08, 01:50 PM   #2 (permalink)
Super Moderator
 
sk_kireeti's Avatar
 
Join Date: Feb 2006
Location: Hyderabad
Posts: 2,355
Thanks: 117
Thanked 241 Times in 198 Posts
Thanks: 117
Thanked 241 Times in 198 Posts
Blog Entries: 4
Rep Power: 51 sk_kireeti has a reputation beyond repute sk_kireeti has a reputation beyond repute sk_kireeti has a reputation beyond repute sk_kireeti has a reputation beyond repute sk_kireeti has a reputation beyond repute sk_kireeti has a reputation beyond repute sk_kireeti has a reputation beyond repute sk_kireeti has a reputation beyond repute sk_kireeti has a reputation beyond repute sk_kireeti has a reputation beyond repute sk_kireeti has a reputation beyond repute
Re: WHY THIS SHOWS AN ERROR

gouriproutray,

What you said is true. In C++ we can declare a variable at any of time within program, before using it. But the limiting factor is, scope of the variable.

In you case, "x" has block scope i.e., X is accessible with in the "if" block. In your case, x++ is out of "if" block. So it is not accessible.

Thanks,
Krishna
__________________
Give 1 cup rice to a hungry Indian just by a mouse click. No money, no form filling, no hassle... Click here for the donation.
sk_kireeti is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 05-05-08, 12:31 PM   #3 (permalink)
Junior Member
 
Join Date: Sep 2007
Age: 25
Posts: 16
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 2 gouriproutray is on a distinguished road
Smile Re: WHY THIS CPP PROGRAM SHOWS AN ERROR

...
if(1)
int x;
x++;
...
in this the error occured in 2nd line as the declaration cannot be done here.. why???
gouriproutray is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 14-06-08, 02:19 PM   #4 (permalink)
Junior Member
 
Join Date: Jun 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1 tarzan7373 is on a distinguished road
Re: WHY THIS CPP PROGRAM SHOWS AN ERROR

ur code is

if(1)
int x; // this is end of ur if block, and all declarations in if block are not alive beyond if block
x++; // dat is why u will get an err at this step cos 'x' is not declared
tarzan7373 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
ABB - Placement Paper sridhar ABB Placement Papers 3 07-05-08 10:40 AM
HACK WITH MS DOS __wicked__ SYSTEM SECURITY & ETHICAL HACKING 0 20-10-07 10:56 PM
TCS Pacement paper 13 sridhar TCS Placement Papers 0 12-02-07 05:34 PM
HCL paper - 28 sridhar HCL Placement Papers 0 06-02-07 09:20 PM
Student Project Program - Microsoft Academic Project Program 2006-2007 jasmine84 Other Requirements 0 26-01-07 07:47 PM


All times are GMT +6.5. The time now is 01:22 PM.





Search Engine Optimization by vBSEO 3.1.0