Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

As Invoking getc() and putc() may be implemented as macros, calling them with parameters with arguments that have side effects may cause unexpected results , since the argument code could be evaluated multiple timesbecause these functions may be implemented as macros and arguments to these macros may be evaluated more than once.

Non-Compliant Code Example: getc()

This code calls the getc() function with an expression as an argument. If getc() is implemented as a macro expansion, the file may be opened several times (see FIO31-C. Do not simultaneously open the same file multiple times).

...

In this non-compliant example, putc() is called with c++ as an argument. If putc() is implemented as a macro, c++ could be evaluated several times " within " the macro expansion of putc(), causing with unintended results.

Code Block
bgColor#ffcccc
char const *filename = "test.txt";
FILE *fptr = fopen(filename, "w");

int c = 97;

while (c < 123) {
  putc(c++, fptr);
}

...

Using an expression that has side effects as the argument to getc() or putc() could can result in unexpected behavior.

...

Wiki Markup
\[[ISO/IEC 9899-1999:TC2|AA. C References#ISO/IEC 9899-1999TC2]\] Section 7.19.7.5, "The {{getc}} function"; Section 7.19.7.8, "The {{putc}} function"