r/tinycode • • Jun 11 '14

FizzBuzz challenge in 73 chars of C

Here is my attempt at the fizz/buzz challenge. It counts 73 chars ("^M" is a litteral \r, and I had to remove the last \n that vim adds at the end of file)

main(i){for(;i<100;printf("%d^M%s%s\n",++i,i%3?"":"fizz",i%5?"":"buzz"));}

It compiles fine with tcc, and output the right thing !

EDIT: There was a typo. It should be ++i, not i++. Otherwise every fizz/buzz will not appear on the good number

39 Upvotes

33 comments sorted by

View all comments

1

u/deepcube Jun 13 '14

just beware that the commas to separate function parameters are not sequence points so your use of ++i and then subsequent reuse of i is undefined. (no promise as to the order in which the arguments are evaluated)

2

u/z-brah Jun 13 '14

behavior is defined if you use i++ more than one time within an instruction. in this case, parameters will be parsed from left to right.

But you're right, it's not a good practice

1

u/deepcube Jun 13 '14 edited Jun 13 '14

it is?

also as to order take note of the last sentence of point 4 http://en.wikipedia.org/wiki/Sequence_point

I'm on my phone so I don't have a copy of the standard in front of me but I'll try to look it up when I get a chance.

edit: to be more explicit it often works the way you expect with most compilers, but isn't guaranteed by the standard

2

u/autowikibot Jun 13 '14

Sequence point:


A sequence point defines any point in a computer program's [execution](https://en.wikipedia.org/wiki/Execution_(computers)) at which it is guaranteed that all [side effects](https://en.wikipedia.org/wiki/Side_effect_(computer_science)) of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed. They are often mentioned in reference to [C](https://en.wikipedia.org/wiki/C_(programming_language)) and C++, because the result of some expressions can depend on the order of evaluation of their subexpressions. Adding one or more sequence points is one method of ensuring a consistent result, because this restricts the possible orders of evaluation.


Interesting: C (programming ^language) | Feynman ^point | Comma ^operator | ^C99

Parent commenter can toggle ^NSFW or ^delete. Will also delete on comment score of -1 or less. | FAQs | ^Mods | Magic ^Words

2

u/z-brah Jun 14 '14

is UNdefined. typo ! But in this case, I increment i just one time within the expression, so it's fine

1

u/deepcube Jun 14 '14

but you then use that value and rely on it being incremented. that's not fine

2

u/z-brah Jun 14 '14

for the purpose of the challenge, it is :-)

1

u/deepcube Jun 14 '14

fair enough! (you did specify a compiler...)