C Function prototype

Description

C Function prototype is for the compiler to recognize the function.

Example - Function prototype

We usually should provide the prototype of our function and let compiler to recognize the function.


#include <stdio.h>
//from   ww  w .  j a  v  a  2  s . c  o  m
int change(int number);                /*Function prototype */

int main(void)
{
  int number = 10;                    
  int result = 0;                     

  result = change(number);
  printf("\nIn main, result = %d\tnumber = %d", result, number);
  return 0;
}

int change(int number)
{
  number = 2 * number;
  printf("\nIn function change, number = %d\n", number);
  return number;
}

The code above generates the following result.

When to prototype

Function is defined after main: prototype the function.


#include <stdio.h>
/*from   ww w  .ja  v a 2 s  . c om*/
f1(int *k);

main ( ){
    int i;

    i = 0;
    printf (" The value of i before call %d \n", i);
    f1 (&i);
    printf (" The value of i after call %d \n", i);
}

f1(int *k)
{
    *k = *k + 10;
}

The code above generates the following result.

Function is defined above the function main: it is not necessary to prototype the function.


//from w w w. jav  a2  s  .  co  m
#include <stdio.h>

void f1(int *k)
{
    *k = *k + 10;
}

main ( ){
    int i;
    i = 0;
    printf (" The value of i before call %d \n", i);
    f1 (&i);
    printf (" The value of i after call %d \n", i);
}

The code above generates the following result.





















Home »
  C Language »
    Language Advanced »




File
Function definition
String
Pointer
Structure
Preprocessing