Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: changed filenames from strings to some variable called file_name

...

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

Code Block
bgColor#ffcccc
const char *filename = "test.txt";
FILE *fptr;

int c = getc(fptr = fopen(filenamefile_name, "r"));

This non-compliant code example also violaties FIO33-C. Detect and handle input output errors resulting in undefined behavior.

...

In this compliant solution, getc() is no longer called with an expression as its argument.

Code Block
bgColor#ccccff

const char *filename = "test.txt";
FILE *fptr = fopen(filenamefile_name, "r");
if (fptr == NULL) {
  /* Handle Error */
}

int c = getc(fptr);

...

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() with unintended results.

Code Block
bgColor#ffcccc

const char *filename = "test.txt";
FILE *fptr = fopen(filenamefile_name, "w");
if (fptr == NULL) {
  /* Handle Error */
}

int c = 'a';

while (c <= 'z') {
  putc(c++, fptr);
}

...

In the compliant solution, c++ is no longer an argument to putc().

Code Block
bgColor#ccccff

const char *filename = "test.txt";
FILE *fptr = fopen(filenamefile_name, "w");
if (fptr == NULL) {
  /* Handle Error */
}

int c = 'a';

while (c <= 'z') {
  putc(c, fptr);
  c++;
}

...