Versions Compared

Key

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

...

The following compliant solution includes the header file containing the appropriate library function declarationprototype.

Code Block
bgColor#ccccff
#include <stdio.h>
#include <string.h>

int main(void) {
  char *c = strchr("world", 'w');
  printf("Hello %c!\n", *c);
}

Non-Compliant Code Example

The non-compliant code example uses the identifier-list form for the parameter declarations.

Code Block
bgColor#FFCCCC

extern int max(a, b)
int a, b;
{
  return a > b ? a : b;
}

Section 6.11 of the C99 standards, "Future language directions", states that "6.11.7 Function definitions
1 The use of function definitions with separate parameter identifier and declaration lists
(not prototype-format parameter type and identifier declarators) is an obsolescent feature."

Compliant Solution

In this compliant solution, extern is the storage-class specifier and int is the type specifier;{{ max(int a, int b)}} is the function declarator; and {{{ return a > b ? a : b; }}}
is the function body.

Code Block
bgColor#ccccff

extern int max(int a, int b)
{
  return a > b ? a : b;
}

Non-Compliant Code Example

...

In this example, the definition of func() expects three parameters but is supplied only two. However, because there is no prototype for func(), the compiler assumes that the correct number of arguments has been supplied, and uses the next value on the program stack as the missing third argument.

Code Block
bgColor#FFCCCC
func(1, 2);
...
int func(int one, int two, int three){
  printf("%d %d %d", one, two, three);
  return 1;
}

Compliant Solution

...

To correct this example, the appropriate function prototype for func() should be specified.

Code Block
bgColor#ccccff
int func(int, int, int);
...

func(1,2);
...
int func(int one, int two, int three){
  printf("%d %d %d", one, two, three);
  return 1;
}

Non-Compliant Code Example

...

Wiki Markup
The following example is based on rule \[[MEM02-A|MEM02-A. Do not cast the return value from malloc()]\]. The header file {{stdlib.h}} contains the function prototype for {{malloc()}}. Failing to include {{stdlib.h}} causes {{malloc()}} to be implicitly defined.

Code Block
bgColor#FFCCCC
char *p = malloc(10);

Compliant Solution

...

Including stdlib.h ensures the function prototype for malloc() is declared.

...