Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Nitpick: Almost all Hello World C examples are wrong. printf is for when you need to use a format string. Hello World doesn't. Besides:

> puts() writes the string s and a trailing newline to stdout.

int main() { puts("Hello World!"); }



But "Hello world\n" is a format string. The format strings with no % chars in them are the best type of format string! They're nearly impossible to get wrong!


I agree, but I have to point out that if you're gonna be like that, then you should be explicit about your final

    return 0;


The C standard (since C99) says that `main()` has an implicit `return 0`, you don't need to write it explicitly.


Sure but are we teaching good habits to students, or are we golfing?


Given how many tutorials leave best practices out on how to do proper error handling, strings and arrays in C, doing analysis as part of the build, I would say golfing most of the time.


Aww, of course, you're right.


That’s not the point of hello world. It’s not to be as small a valid program as possible. It’s to be a small program that also exercises the needed functionality for using the tool usefully. All of the exercises following that hello world need formatted text, so introducing puts would just add confusion and wouldn’t verify that you have a working printf.


Eh, it compiles down to the same thing with optimizations enabled:

https://godbolt.org/z/zcqa4Txen

But I agree, using printf for constant strings is one step away from doing printf(x) which is a big no-no.


Useless bit of compiler optimizations trivia: the "this printf() is equivalent to puts()" optimization seems to work by looking for the '%' in the format string, not by counting whether there is only one argument to printf(), e.g. if you add 42 as a second argument to the printf() — which is absolutely legal and required by the standard to Work as Intended™ — the resulting binary still uses puts().


The example is kind of pedantic, but I think a linter might be able to catch it.


Eh, not a fan of puts. It doesn't add any value over write or printf and it should be named "printLine".

But if you're still using raw libc in 2025 that's a problem you willingly opted into. I have zero sympathy.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: