Versions Compared

Key

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

...

In this example, the programmer sets the value of the msg variable, expecting to reuse it outside the block. Due to the reuse of the variable name, however, the outside msg variable value is not changed.

Code Block
bgColor#FFCCCC
char msg[100];
{
  char msg[80] = "Hello";
  strcpy(msg, "Error");
}

printf("%s\n", msg);

...

This compliant solution uses different, more descriptive variable names.

Code Block
bgColor#ccccff
char error_msg[100];
{
  char hello_msg[80] = "Hello";
  strcpy(error_msg, "Error");
}

printf("%s\n", error_msg);

...