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 ...
|
|||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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. |
|
|||
|
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.
|
|
|||
|
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 |
![]() |
| Thread Tools | |
|
|
|
||||
| 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 |