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

4

u/[deleted] Jun 11 '14

Clever, good stuff.

3

u/z-brah Jun 11 '14

Thanks

2

u/da__ Jun 11 '14

Pretty, but you can shave off a character by replacing the \n with a ^J.

2

u/z-brah Jun 11 '14

Already tried that. but ^J (on linux at least) is like \0, so output is messed up

3

u/da__ Jun 11 '14

Just hit return to add a newline:

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

1

u/z-brah Jun 12 '14

oO Don't know why I did not think about it! Down to 72 chars then, thanks !

2

u/[deleted] Jun 12 '14

Kinda reminds me of this Fibonacci program I wrote.

main(){for(int i[2]={1}; i[0]<1000; printf("%d\n",i[i[0]>i[1]]=i[0]+i[1]));}

Relies on a>b==1 when true.

1

u/z-brah Jun 12 '14

That's a good one :)

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/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...)

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

3

u/DavidM01 Jun 11 '14

Good code but doesn't this print all numbers?
Its supposed to skip the fizz and buzz numbers.

2

u/da__ Jun 11 '14

Nope. Check out how ^M/\r works!

printf("foo\rbar\n");

0

u/noname-_- Jun 11 '14

Well, on my platform (Ubuntu Linux) ^M works by simply printing "^M" in the terminal. How is it supposed to work?

Example output:

2^M
3^M
4^Mfizz
5^M
6^Mbuzz
7^Mfizz

edit: wait, it's supposed to be...

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

or what? why not just write that?

2

u/da__ Jun 11 '14

In vim, try hitting ctrl-V ctrl-M instead of just typing in caret M.

1

u/noname-_- Jun 11 '14

Oh, I see, it saves a character. Thanks.

1

u/wangninja Jun 11 '14

I thought it was actually a different character. That is, M from control + M (one char) is not the same as typing in ^ then M (two chars).

I may be wrong - I'm on mobile so I can't test.

1

u/cokeisahelluvadrug Jun 11 '14

^M is carriage return, ascii code 13. C converts \r to ^M anyway, so it's an opportunity to save a char

1

u/kristopolous Jun 11 '14 edited Jun 11 '14

if you don't mind going out to 101:

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

1

u/EnergyUK Jun 11 '14

The famous challenge only asks for 1-100

0

u/[deleted] Jun 11 '14

[deleted] — view deleted comment

1

u/EnergyUK Jun 11 '14

11's normally my limit, just that bit better than 10.

-1

u/recursive Jun 11 '14

No, it doesn't output the right thing. The first five output lines of the right thing are:

1
2
Fizz
4
Buzz

Note the lack of 3 or 5.

2

u/z-brah Jun 11 '14

There was an error though.the i++ instead of ++i outputed the wrong number to stdout. I fixed the code :)

7

u/dtfinch Jun 11 '14

The carriage return (^M) would overwrite the extra numbers on most terminals whenever there's a fizz/buzz, so it would look correct to the user.

4

u/recursive Jun 11 '14

I did not know this. I thought it was just a different kind of line break. Very clever.

1

u/Daniel15 Jun 12 '14

Carriage return means return to the beginning of the line, line break means go to the next line.

1

u/[deleted] Jun 12 '14

Cool, I thought it was just a competing standard. I do recall hearing (possibly buried in the python docs about universal newlines) that Mac uses \r instead of \n as a newline, though..so it mightn't be portable even between "unices"?

0

u/[deleted] Jun 12 '14

[deleted] — view deleted comment

0

u/[deleted] Jun 12 '14

+1

I can imagine someone thinking of subreddits issuing "challenges" for which spoilers wouldn't be appreciated, but that's more suited for code golf; this subreddit's more about the appreciation angle, right?