Versions Compared

Key

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

...

Code Block
bgColor#FFCCCC

long _Max_Value;
int __length; 

Compliant Solution

The This compliant solution uses identifiers that are not reserved.

Code Block
bgColor#ccccff

long maxValue;
int length;

...

Noncompliant Code Example

In this example, a variable beginning with an underscore is defined with implicit global scope.

Code Block
bgColor#FFCCCC
size_t _limit = 100;

unsigned int getValue(unsigned int count){

  size_t i;
  unsigned int result = 0;

  for(i = 0; i < _limit; i++){    
    result++;        
    if(i == count){ 
      break;
    }
  }

}

Compliant

...

Solution 1

In the this compliant solution, the variable is declared as static and, hence, has file scope.

...

Compliant Code Solution 2

In the this compliant solution, the variable name does not begin with an underscore and, hence, is not reserved.

Code Block
bgColor#ccccff
size_t limit = 100;

unsigned int getValue(unsigned int count){

  size_t i;
  unsigned int result = 0;

  for(i = 0; i < limit; i++){    
    result++;        
    if(i == count){ 
      break;
    }
  }

}

Noncompliant Code Example

Identifiers with external linkage include setjmp, errno, math_errhandling, and va_end.
In the example, errno is defined. The errno value set by the function open() would not be accessible to the program as because its definition is suppressed. For information regarding redefining errno, see ERR31-C. Don't redefine errno.

Code Block
bgColor#FFCCCC

#include <errno.h>

#define errno 200

int validate(unsigned int secretValue){
  
  char fname[] = "non_exist.txt";
  int fd;
  int result = -1;

  fd = open(fname, O_RDONLY); 

  if(fd == -1){
    printf("Error opening file. Error code : %d\n", errno); 
    return result;
  }

  close(fd); 

  if(errno % secretValue == 20){
    result = 0;
  }
  else{
    result = -1;
  } 
  
  return result;
  
}

Compliant Solution

In the this compliant solution, the reserved identifier errno is not used.

...