Exforsys

Online Training

Is there any Difference

This is a discussion on Is there any Difference within the C and C++ forums, part of the Programming Talk category; Consider declaring variables and using then for computation in C++ program as below in Method 1 and Method 2 Method ...


Go Back   Exforsys > Programming Talk > C and C++

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-18-2007, 04:07 PM
Senior Member
 
Join Date: Apr 2006
Posts: 144
caradoc is on a distinguished road
Exclamation Is there any Difference

Consider declaring variables and using then for computation in C++ program as below in Method 1 and Method 2

Method 1:
int a, b, c;
a=b+c;

Method 2:
int b, c;
int a=b+c;

Though on the user interface side both gives the same result I have a doubt in this. Is there any difference in the working of Method 1 and Method 2 internally or will it get assigned and evaluated internally also in the same way. Kindly explain me.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-19-2007, 04:31 PM
Senior Member
 
Join Date: Apr 2006
Posts: 125
ashlee is on a distinguished road
Yes there is clearly a distinction between the internal working of Method 1 and Method2.Methdo2 is always preferred than Method1 when you understand the internal working of both methods. In method1 the expression b+c is evaluated and is stored in a temporary object of type int and then only it is assigned to integer variable named as a. After assigning the value of b+c integer variable a, the temporary object gets destroyed. In contrast in method 2 the value of b+c is calculated and directly assigned to integer variable a without the use of temporary variable. So performance wise method2 is always better than method1.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-22-2007, 06:56 AM
Junior Member
 
Join Date: May 2007
Posts: 9
hvg_99 is on a distinguished road
Is there any difference - Yes there is...

Method 1:
int a, b, c;
a=b+c;

Method 2:
int b, c;
int a=b+c;

In the first case we are declaring 3 variables and next step we are adding two values and assigning it to 'a';

Whereas in the second case we are declaring two variables by name 'b' and 'c' and in the next step we are declaring a variable by name 'a' and immediately assigning sum of b and c. so we are minimizing the declaration line.

In the first case 3 variables are getting declared and then the expression.

I hope i have cleared your doubt.

Regards,
Gayatri
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

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
difference between winrunner 7./7.6 zecar Software Testing 1 11-13-2006 03:32 AM
Linux vs. Windows web Hosting, does it make a difference? sanereddy Unix Articles and Tutorials 7 04-23-2006 02:39 PM
what is difference between terminal service and remote desktop techguru Interview Questions 1 04-17-2006 09:14 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit Tech FAQ 0 06-01-2004 07:00 AM
Smalltalk FAQ (v.1.0) Vikas Malik Tech FAQ 0 04-17-2004 08:27 AM


All times are GMT -4. The time now is 08:43 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.