Live data from Hacker News

There is no 'printf'

netmeister.org

11–20 of 158 posts

Re: There is no 'printf'

#12

This is a bit like saying there is no '+'; Because if you put in return 1+2+3; And look at the assembly code, you will see that the compiler generated something like return 6; The compiler is allowed to take advantage of the standard to substitute in more efficient code that does the same thing. IIRC, for C++, it would actually be ok if std::vector was implemented completely as a compiler intrinsic with no actual hea…

> more efficient code that does the same thing In this case, it produces a different result.

It produces a different ub, which is ub.

Furthermore observability would be defined in terms of the C abstract machine, “observing” by decompiling the program is out of scope.

Re: There is no 'printf'

#13

So, why does puts do "return r ? EOF : '\n';"? Some backwards compatibility? Or is there a logical reason for that?

Per the man:

> puts() and fputs() return a nonnegative number on success, or EOF on error.

r is the result of the write, if it’s nonzero the write failed and thus so did puts.

Re: There is no 'printf'

#14

This is a bit like saying there is no '+'; Because if you put in return 1+2+3; And look at the assembly code, you will see that the compiler generated something like return 6; The compiler is allowed to take advantage of the standard to substitute in more efficient code that does the same thing. IIRC, for C++, it would actually be ok if std::vector was implemented completely as a compiler intrinsic with no actual hea…

Code that does

  #include 
must compile, so that header must exist (whether it is stored in a file is the implementer’s choice. AFAIK, the standard carefully avoids the use of the term ‘header file’)

Also, I think code that doesn’t do that include must fail to compile when it tries to use std::vector. So, logically, that header must exist.

Re: There is no 'printf'

#15
Quick Summary:

The C compiler optimizer replaces printf("Hello World!\n") with puts("Hello World!\n") and the implicit return from main() changes from 13 (the return value of printf) to 10 (the return value of puts)

Re: There is no 'printf'

#16

Moar please. I'm loving these counterintuitive C optimization gotchas lately[1]. They are like little brain teasers. 1. https://news.ycombinator.com/item?id=28930271

About a year ago there was something of a "joke isEven() implementation discourse" on Twitter, which eventually evolved a sort of informal optimizer abuse contest. For example:

https://twitter.com/zeuxcg/status/1291872698453258241

https://twitter.com/jckarter/status/1428071485827022849

Re: There is no 'printf'

#17

So, why does puts do "return r ? EOF : '\n';"? Some backwards compatibility? Or is there a logical reason for that?

That particular implementation probably returns the result of the last fputc() or equivalent that it called.

puts() returns EOF (typically -1) on error, or some unspecified non-negative value on success.

fputc() returns EOF on error or the written character, treated as an unsigned char and converted to int, on success.

Don't expect all puts() implementations to do the same thing. For example, the glibc implementation appears to return the number of characters written on success. Implementations are free to rely on implementation-defined behavior. User code that's intended to be portable cannot.

Re: There is no 'printf'

#18

So, why does puts do "return r ? EOF : '\n';"? Some backwards compatibility? Or is there a logical reason for that?

Per the man: > puts() and fputs() return a nonnegative number on success, or EOF on error. r is the result of the write, if it’s nonzero the write failed and thus so did puts.

Yeah, but I think the question was why EOF and "\n". It could as easily just return 1 or -1 for example, and it would make more sense I think.

Re: There is no 'printf'

#19

Moar please. I'm loving these counterintuitive C optimization gotchas lately[1]. They are like little brain teasers. 1. https://news.ycombinator.com/item?id=28930271

It's all fun and games until you write (or review) C/C++ test cases for a compiler or disassembler ;-) It never stopped to amaze me how good the compiler was to figure out that I actually wrote very complicated "return 0".
Post reply on HN