Versions Compared

Key

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

...

The following non-compliant code example simply illustrates calling the standard string handling function strlen() with a plain character string, a signed character string, and an unsigned character string:

Code Block
bgColor#FFCCCC
#include <string.h>

int main(void) {
  size_t len;
  char cstr[] = "char string";
  signed char scstr[] = "signed char string";
  unsigned char ucstr[] = "unsigned char string";

  len = strlen(cstr);
  len = strlen(scstr);  /* warns when char is unsigned */
  len = strlen(ucstr);  /* warns when char is signed */
  return 0;
}

Wiki Markup
Compiling at high warning levels in compliance with [MSC00-A. Compile cleanly at high warning levels] causes warnings to be issued when converting from {{unsigned char\[\]}} to {{const char *}} when {{char}} is signed and from {{signed char\[\]}} to {{const char *}} when char is defined to be unsigned.  Casts are required to eliminate these warnings, but excessive casts can make code difficult to read and hide legitimate warning messages.

...

The compliant solution uses plain char for character data.

Code Block
bgColor#ccccff

#include <string.h>

int main(void) {
  size_t len;
  char cstr[] = "char string";

  len = strlen(cstr);
  return 0;
}

Conversions are not required and the code compiles cleanly at high warning levels without casts.

...