Hacker Newsnew | past | comments | ask | show | jobs | submit | accatyyc's commentslogin

How about just

    puts(“Hello, World!”)
which has been in C since the dawn of time?


GCC at least will compile a printf without special formatting characters as a pit, so it ends up being six of one in the end.


Looks even better!


I think this is part of the problem with the language- a fragmented set of keywords, all with slightly different behavior.


puts and printf aren't keywords, they're regular functions.


Well, sort of.

Specifically, printf is an oddball function because it uses the varargs mechanism, and the whole format strings mechanism is inherently risky because it effectively bypasses the type system and says "trust me." Back when I was learning C, on a Mac with THINK C, misusing printf was a sure-fire way to crash the computer very quickly, especially since misaligned accesses of 16-bit or 32-bit words caused crashes. Compilers now go to a great deal of trouble to try to do additional safety and consistency checks.

Don't get my wrong, I grew up using printf, and it is massively useful. But it was designed when computers were much smaller and simpler, and design tradeoffs were made back then that probably wouldn't be chosen today. So printf, along with a whole family of related functions, has been a seething mess of a security and safety hole longer than most programmers have been alive.


No.

The popular C compilers have a feature where they will do some additional type checking on the arguments passed to "format" functions. You can mark your own functions with this attribute.

See the format attribute https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attribute....

printf is not an oddball function. Also, typechecking format strings in general does not have to be that complicated. They are still used in golang.

Of all the security pitfalls of C, the format string design of printf is way down the list. As others have noted, printf is not what makes the C type system weak.


Nope, printf is a regular function - regular functions can use varargs just fine.

And it's C; everything bypasses the type system and says "trust me". Memory allocation bypasses the type system and says "trust me".

  struct foo* foo = malloc(sizeof foo);
  // yep, this is definitely the right number of bytes
If you want strong typing (!= static typing), C is not the language you should be using, printf or no printf.


Format strings (... at least, static format strings) don't have to bypass the type system.


Sure, but that's varargs being the special cased but, not printf (I've written printf implementations for some ebedded systems, it's always just regular C code).


How do you start the radio? If you start it from a song of the genre you want to listen to now, or a playlist containing mostly that genre, it won't mix in random stuff you've liked before. I get very nice mood/genre radios just by starting them from some song I liked currently.


Usually from a song or playlist, and no matter where I've started it it will start mixing stuff in that isn't remotely similar. Unless it's trance or house.


In Objective-C (and some other languages with ternary operators) the same can be written like this:

    int a = b ?: c ?: 0


That one's straight up called the Elvis Operator.

I hate it when things like this get names that everyone's supposed to know and it's considered a mark against you if you've never heard it before, but I do think the name Elvis Operator is hilarious. :D


I was wondering why the funny name, and

> The name "Elvis operator" refers to the fact that when its common notation, ?:, is viewed sideways, it resembles an emoticon of Elvis Presley.

https://en.wikipedia.org/wiki/Elvis_operator


Also, a lot of other languages (Python, Ruby, JS, etc.) can do the same thing with their OR operator:

    b = list.next.to_s || "Reached end of list."
If list.next.to_s is non-nil, then the OR statement is short-circuited and list.next.to_s will be assigned to b. If b is nil and "Reached end of list." is non-nil, then OR statement will return the string literal to be assigned to b instead. Thus,

    a = b || c || 0
would be the equivalent for those languages.

edit: One BIG downside to this I forgot to mention at first: If nil and a false value are both possible for a variable, then this construction can betray you and break your heart.


Yup.

Personally, I like the ternary operator, and I also like the nil-coalescing operator, but I'm quite aware that they can produce difficult-to-maintain code, so I'm careful with them. I have gone and done some silly stuff with both, but then, I realized that I was leaving unmaintainable code.

If you use godbolt.org, you can actually see what code is produced by the source.


The second conditional is unneeded as the 0 will only be returned if c evaluates to 0, so it can equally well be written

    int a = b ?: c;


That's not specific to Objective-C, it's a GCC extension to the C language.


(Which Clang happens to support, hence it working here.)


Same here, probably something added by oh-my-zsh


For me, using pure git is fine when working on solo projects. The benefits of such services is when there are many developers in the same project. Gives a good overview of issue reports, what commits are linked to what issues, easy "pull request" overviews/quick reviews without checking out branches etc. It's more about the social and less about the source control for me


Personally I think you should skip the /r/ or /i/ part of the paths. Always bothered me that reddit has it - communities are central to these sites so IMO they should be the first part of the path


Then email servers would have to download billions of images of which 99% the corresponding email won't even be opened


Turns out he was...


Indeed, and I don't know why I'm surprised.


It feels like a programmer thing, since unsaved files won't be compiled when you run the program. At least that's where I got the habit from


Programmers have an additional issue with incremental build systems that do not always rebuild everything they should.


Why would informing people about the importance of testing make you look like an asshole? You could do it in a polite and helpful way, instead of being an asshole about it.


Well, the article in question called it an asshole thing to do.

>Publicly calling out and blaming others


    > the article in question called it an asshole thing to do [publicly calling out and blaming others]
It's all about how it's done. In the case described above, an explanation of what happened, why it happened, how to avoid it in the future, and perhaps a little humor would be positively received by everyone involved.

On the other hand, someone could very well have the exact same situation and make it into a-hole incident by shaming the interns and making them reconsider even being there.


Automation takes this and a whole class of problems out of the interpersonal domain. Failed tests make the build fail. Who ever breaks the build buy donuts for the team. Easy, solved and no personal issues. Include a linter in the build, incorrect formatting fails the build. The team decided on the standard format (tabs vs spaces, 4/2 spaces, brace same line or next line etc). More donuts. No arguments because linting rules were decided by the team. New members adapt.


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

Search: