There is no 'printf'
11–20 of 158 posts
Re: There is no 'printf'
#12This 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.
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'
#13So, why does puts do "return r ? EOF : '\n';"? Some backwards compatibility? Or is there a logical reason for that?
> 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'
#14This 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…
#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'
#15The 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'
#16Moar 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
Re: There is no 'printf'
#17So, why does puts do "return r ? EOF : '\n';"? Some backwards compatibility? Or is there a logical reason for that?
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'
#18So, 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'
#19Moar 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
Re: There is no 'printf'
#20How does it decide which nonnegative integer to return?