Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Updated for ctype.h, clarified some wording

...

  • memset()
  • memset_s()
  • fprintf() and related functions (For the length modifier c, if no l length modifier is present, the int argument is converted to an unsigned char, and the resulting character is written.)
  • fputc()
  • ungetc()
  • memchr()

And and to arguments to the following library functions that are converted to char:

  • strchr()
  • strrchr()
  • All of the functions listed in <ctype.h>

The only integer type conversions that are guaranteed to be safe for all data values and all possible conforming implementations are conversions of an integral value to a wider type of the same signedness. The C Standard, subclause 6.3.1.3 [ISO/IEC 9899:2011], says,

...

Code Block
bgColor#FFcccc
langc
#include <string.h>
#include <stddef.h>
#include <limits.h>
 
int *init_memory(int *array, size_t n) {
  return memset(array, 4096, n); 
} 

...

In general, the memset() function should not be used to initialize an integer array unless it is to set or clear all the bits., as in this compliant solution:

Code Block
bgColor#ccccff
langc
#include <string.h>
#include <stddef.h>
#include <limits.h>
int *init_memory(int *array, size_t n) {
  return memset(array, 0, n); 
} 

...

...