What is const Qualifier - C Function

C examples for Function:Function Parameter

Introduction

Arguments can be passed to functions in one of two ways: pass by value and pass by reference.

When passing arguments by value, C makes a copy of the value for the receiving function to use.

Passing arguments by reference can modify argument contents via pointers.

To have the power of passing arguments by reference without changing a variable's contents, use the const qualifier.

The following program passes a read-only integer type argument to a function.

Demo Code

#include <stdio.h> 

void printArgument(const int *); 

int main(){ /*  w  ww  . j ava  2s  . co m*/

   int iNumber = 5; 
   printArgument(&iNumber); //pass read-only argument 
}

void printArgument(const int *num) //pass by reference, but read-only 
{ 
   printf("\nRead Only Argument is: %d ", *num); 
}

Related Tutorials