473,290 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,290 software developers and data experts.

prefix , postfix operator associativity

hey all ! i am pragensh and joined today for follwoing reason
can you help me how this output came ?
x=3;
x-=--x-x--;
printf("x=%d",x);
here answer is coming as 1 how come ?
can you please help me to find the reason ? or
step wise explaination
Sep 14 '06 #1
3 3646
D_C
293 100+
Putting two unary operations like that on the same line has undefined behavior, it's not ever a good idea. However, in your case, it looks like your compiler evaluates left to right. However, parenthesis come first. I substitute for x when I put it's value in parenthesis, i.e. --(3), (2)--, or (1).

x = 3;
x = x - (--x - x--)
x = x - (--(3) - x--)
x = x - (2 - x--)
x = x - (2 - (2)--)
x = x - (2 - 2)
x = (1) - 0
x = 1 - 0
x = 1
Sep 14 '06 #2
Putting two unary operations like that on the same line has undefined behavior, it's not ever a good idea. However, in your case, it looks like your compiler evaluates left to right. However, parenthesis come first. I substitute for x when I put it's value in parenthesis, i.e. --(3), (2)--, or (1).

x = 3;
x = x - (--x - x--)
x = x - (--(3) - x--)
x = x - (2 - x--)
x = x - (2 - (2)--)
x = x - (2 - 2)
x = (1) - 0
x = 1 - 0
x = 1
thank you sir but i am get back to you with some doubt regarding the same topic
Sep 15 '06 #3
sir i think this is how the steps will go,but can u explain me your logic in steps (1) & (2)


x = 3;
x = x - (--x - x--)
x = x - (--(3) - x--)
x = x - (2 - x--)
x = x - (2 - (2)--)
x = x - (2 - 1) /* (sir according to postfix operation) */ .. (1)
x = (2) - 1 (2)
x = 2-1
x = 1
Jan 12 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Sergey | last post by:
Hello! Could anybody be kind enough to explain this concept? Why C++ make two ops for prefix and postfix ++ operator? As I guess, it is possible to implement both cases via sole prefix...
8
by: lovecreatesbeauty | last post by:
Hello experts, Why can this difference between prefix increment/decrement and postfix increment/decrement reside in built-in operators for built-in data types? Thanks. // test.cpp // //...
98
by: jrefactors | last post by:
I heard people saying prefix increment is faster than postfix incerement, but I don't know what's the difference. They both are i = i+1. i++ ++i Please advise. thanks!!
17
by: Anoob | last post by:
Can we consider () unary operator when calling a function, in exps eq. f(), ( 1 + 2). But when we call function f( 10 ) it is a binary operator. Even if we pass f( 10, 20) as we are using ,...
2
by: shan | last post by:
Hi to everybody, I am begginer in C programming language. My simple doubt is the difference between postfix & prefix unary operator plus. (i.e) i++ and ++i . plz give me an example program...
30
by: Xah Lee | last post by:
The Concepts and Confusions of Prefix, Infix, Postfix and Fully Functional Notations Xah Lee, 2006-03-15 In LISP languages, they use a notation like “(+ 1 2)†to mean “1+2â€....
8
by: subramanian100in | last post by:
Consider int i = 10; Why do we say that ++i yields an Lvalue and i++ yields an Rvalue ? I thought both these expressions yield only values. I am unable to understand the difference
2
by: swapnaoe | last post by:
Hi, In http://elearning.embnet.org/file.php/43/ctutorial/Postfix-and-prefix----and---.html I read about postfix and prefix unary operators. It said " If the increment or decrement operator is...
3
by: news.aioe.org | last post by:
Is it possible to overload increment(++) and decrement(--) postfix and prefix operators for primitive datatypes such as int, char, short, etc. in global scope (vs as a class member function where...
2
by: puzzlecracker | last post by:
How can we implement operator ++ as postfix in prefix? thanks
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.