:confused:#include<stdio.h>
#include<conio.h>
void main(){
int a;
clr scr();
a=10;
a=a++ + --a;
print f("%d",a);
get ch();
}
the sol must be 21 but its showing 20 http://forum.codecal...1/confused1.gif
9 replies to this topic
#1
Posted 27 September 2010 - 09:59 AM
|
|
|
#2
Posted 27 September 2010 - 11:08 AM
This is not a tutorial: moved to the correct forum.
Do you understand the difference between a++, ++a, --a, and a--? What exactly are you trying to accomplish?
Do you understand the difference between a++, ++a, --a, and a--? What exactly are you trying to accomplish?
#3
Posted 27 September 2010 - 12:16 PM
WingedPanther said:
What exactly are you trying to accomplish?
Who said it must be 21?
What'll happen is
a++ + --a
(10 + (10-1) ) + 1... which is 20.
Quote
Do you understand the difference between a++, ++a, --a, and a--?
#4
Posted 27 September 2010 - 07:27 PM
thank you Mr.Panther. if we give the same at print f its giving sol as 21 if u don't mind can u please explain the difference of a++,++a,--a, and . a-- please
#5
Posted 27 September 2010 - 07:48 PM
It's a very basic operator in a language, you should know it:
Pre and Post Increment and Decrement Operators
Pre and Post Increment and Decrement Operators
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#6
Posted 27 September 2010 - 07:57 PM
thanx mr oxano but im still in confusion about this solution y bcz when u give in print f statement its giving as 21 can plz explain the difference
#7
Posted 27 September 2010 - 08:04 PM
thank you sir i am a bit clarified but i try to understand the logic with your example its really good sol. thanku once again
#8
Posted 28 September 2010 - 04:47 PM
Can you post the exact code that produces 21?
#9
Posted 30 September 2010 - 02:30 AM
code to produce 21
#include<stdio.h>
int main()
{
int a=10;
a=++a + a--; //++a is pre-increment that means it increments the value by 1 before any further computation is done ans a-- is post increment i.e first //current value of a is assigned and then decrements by 1
printf("%d",a);
return 0;
}
#include<stdio.h>
int main()
{
int a=10;
a=++a + a--; //++a is pre-increment that means it increments the value by 1 before any further computation is done ans a-- is post increment i.e first //current value of a is assigned and then decrements by 1
printf("%d",a);
return 0;
}
#10
Posted 30 September 2010 - 04:32 AM
So really, you're asking what's the difference between:
Correct?
I would expect one to return 22, and the other 20.
int a=10;
a=++a + a--;
anda=10; a=a++ + --a;
Correct?
I would expect one to return 22, and the other 20.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









