I am needing one statement in ISO-compliant C code. Say there are integers x
, y
, z
. I need to store x + y
in z
and increment x
by one. How do I do this in one statement?
Asked
Active
Viewed 31 times
-2

Jonathan Leffler
- 730,956
- 141
- 904
- 1,278
-
1Welcome to Stack Overflow. Please read the [About] and [Ask] pages soon. You're expected to show what you've tried — if there are problems, we can help you fix them, but we expect you to show an honest attempt at solving the problem. How do you write the addition? What have you learned about special operators in C? This is quite basic; your C text book should have the information you need. Also, you say 'ISO C', but do you mean C90, C99 or C11 (or all three, since the expression required is the same in all three — and in pre-standard C too). Hmmm; on second thoughts, scratch the ISO issue. – Jonathan Leffler Oct 25 '16 at 03:40
-
Search the net for post-increment. – Support Ukraine Oct 25 '16 at 03:52
-
Why does it need to be one statement? – user253751 Oct 25 '16 at 04:07
-
Possible duplicate of [What is the difference between ++i and i++](http://stackoverflow.com/questions/24853/what-is-the-difference-between-i-and-i) – Pieter van den Ham Oct 25 '16 at 07:11
1 Answers
-2
You could use the comma operator to combine the two operations into one statement:
(z = x + y), x++;

user253751
- 57,427
- 7
- 48
- 90
-
That's a tad devious, isn't it? It works, but it isn't the obvious answer. – Jonathan Leffler Oct 25 '16 at 04:36
-
@JonathanLeffler It's a general way to combine two assignments (or similar) into one statement, rather than one that only works in this specific case. – user253751 Oct 25 '16 at 06:18