4

Standard says that:

There is a sequence point immediately before a library function returns. C17dr § 7.1.4 3.

I know that there is a sequence point before actual call and after return statement (due to semicolon if there is another reason please let me know) but i couldn't understand the sentence above. Could anyone please explain this ?

Ulaş Sezgin
  • 308
  • 2
  • 10
  • Does this answer your question? [What's the consequence of a sequence-point "immediately before a library function returns"?](https://stackoverflow.com/questions/45814572/whats-the-consequence-of-a-sequence-point-immediately-before-a-library-functio) – Nate Eldredge Jun 02 '20 at 19:34
  • Yes, but I didn't understand this "Library functions don't have the code that implements them covered by the standard (they might not even be implemented in C)" part of the answer. – Ulaş Sezgin Jun 02 '20 at 19:40
  • @UlaşSezgin what part of that quote didn't you understand? It answers your question – M.M Jun 02 '20 at 20:51
  • 1
    I think the point is that we know there is a sequence point after a `return` statement, but we don't have any guarantee that a library function actually contains a `return` statement, or any statements at all, since it may not be written in C. – Nate Eldredge Jun 02 '20 at 22:03

1 Answers1

4

It means you can write code like t = sqrt(t). It would be really annoying if you couldn't.

And the behaviour of the above would be undefined if the C standard didn't guarantee that the functions had sequencing points in prior to their returning.

Note that C standard library functions might be hardcoded by the compiler - so this is an important consideration. It also adds extra protection for the user of a standard library implementation that might implement some functions as macros (which is permitted subject to a plethora of rules).

(Note that the rule has been carried over to C++).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 1
    Now I'm curious whether there are other ways in which a library function might not act exactly "as if" implemented by a C language function definition. – aschepler Jun 02 '20 at 18:59
  • 1
    Okay, but I think, we can write this code thanks to other sequence point rules. Can't we ? – Ulaş Sezgin Jun 02 '20 at 19:03
  • @aschepler I imagine things like setjmp and thread creation functions definitely do not operate as something that can be implemented in pure C. – Max Jun 02 '20 at 20:00
  • @aschepler: It also guards against functions implemented as macros (although it could be a tautology). See https://stackoverflow.com/questions/47029568/defining-c-functions-in-the-c-standard-library-as-macros#:~:text=The%20rules%20of%20the%20standard,also%20implemented%20as%20a%20macro. – Bathsheba Jun 02 '20 at 20:36
  • I am at my happiest when the compiler does hardcode standard lib stuff. Please give me my inline intrinsic functions per platform without needing LTO to make the function calls go away... – Michael Dorgan Jun 02 '20 at 20:57