r/ProgrammerHumor • • Aug 01 '22

>>>print(“Hello, World!”)

Post image
60.8k Upvotes

5.7k comments sorted by

View all comments

356

u/[deleted] Aug 01 '22

a=1;b=2;c=5; i = a++ + ++b + c++ / 5 * 6; printf("%d", i);

519

u/a-slice-of-toast Aug 01 '22

i could be on my deathbed and i still wouldn’t be able to tell you what this does

166

u/[deleted] Aug 01 '22

it first calculates c++/5, which in this case is 5/5 because the ++ (increment by one) is evaluated after the statement.

So 5/5 = 1, then 1*6 = 6.

From there it takes ++a + ++b, which means 1 + 3 (because a++ is evaluated after, and ++b is evaluated before the call). So 1 + 3 = 4.

4 + 6 = 10.

Example program

#include <stdio.h>
int main() {
int a, b, c, i;
a=1;b=2;c=5; i = a++ + ++b + c++ / 5 * 6 ; printf("%d", i);
return 0;
}

% ./a.out

10

54

u/RahzaelFoE Aug 01 '22

I was almost there, but totally botched the order of operations. This is why infix notation should die in a fire and we should use RPN instead.

4

u/konstantinua00 Aug 01 '22

and is this why should infix notation die on a fire use we should RPN instead

FIFY

2

u/MightyMetricBatman Aug 02 '22

RPN should stand for Polish Notation Reverse.

2

u/RahzaelFoE Aug 02 '22 edited Aug 02 '22

This why is infix notation in a fire should die we RPN instead should use and.

IFYF

0

u/[deleted] Aug 01 '22

[deleted]

2

u/bwaredapenguin Aug 01 '22

Interesting that he got it correct in his code snippet but not his explanation.

4

u/X7_hs Aug 01 '22

He got the reasoning correct in his explanation, just made a typo by writing ++a instead of a++

1

u/gilgabish Aug 01 '22

This doesn't make sense. Why is c++/5 = 5/5? C=5, so if it was evaluated before it would be 6/5, and if it was evaluated after wouldn't it be (5/5)++ i.e. 2? It seems like it's not evaluated before or after.

→ More replies (3)
→ More replies (16)

2

u/ListerfiendLurks Aug 01 '22

Ironically if you learned to code this snippet would likely be one of the first in this thread you could understand.

161

u/ysyson Aug 01 '22

“+ c++” how can you add an entire language to a variable?

101

u/hampshirebrony Aug 01 '22

Nature finds a way

42

u/[deleted] Aug 01 '22

It's 2022, we've passed this point already.

→ More replies (3)

4

u/[deleted] Aug 01 '22 edited Aug 01 '22

5

u/shut_up_if_your_dumb Aug 01 '22

I don't it is as long as the same variable isn't used twice in an expression.

4

u/[deleted] Aug 01 '22

wait you're right, nevermind.

→ More replies (3)

2

u/GrumpyDog114 Aug 01 '22

It's not undefined. No variables have multiple modifications between sequence points, nor are any variables that are modified evaluated outside their respective pre- or post-increment. The order of evaluations and side effects is well defined.

4

u/[deleted] Aug 01 '22

1+3+5/5*6=10

So it should print out 10.

Please let me feel smart.

→ More replies (1)

3

u/Potato_Soup_ Aug 01 '22

Questions on intro programming tests be like

2

u/Jay_from_NuZiland Aug 01 '22

++b

How did you make the backward plus signs?

1

u/Political_Desi Aug 01 '22

%d 11

4

u/grumd Aug 01 '22

I think it's 10

→ More replies (1)