Functions in C

Function is a block of statements which perform some specific task and always return single value to the calling function. Functions in c are used to minimize the repetition of code.

Some languages distinguish between functions which return variables and those which don’t. C assumes that every function will return a value. If the programmer wants a return value, this is achieved using the return statement. If no return value is required, none should be used when calling the function.

There are two types of functions in c language.

Library Functions

A function which is predefined in c language is called library function. printf(), scanf(), getch() etc are examples of built-in or library functions.

User Defined Functions

A function written by a programmer is called user defined function.

User Defined Function Example

int add (int x, int y) {

int z;
z = x + y;
return (z);


main ()

int i, j, k;
i = 15;
j = 5;

k = add(i, j);
printf (“The value of k is %d\n”, k);



The value of k is 30

Scope of Function

Only a limited amount of information is available within the body of each function. Variables declared within the calling function can’t be accessed from the outside functions unless they are passed to the called function as arguments.

Global Variables

A variable that is declared out side all functions is called Global variable. Global variables don’t die on return from a function. Their value is retained, and is available to any other function in whole program.

Local Variables

A variable that is declared within a function is called Local variable. They are created each time the function is called, and destroyed on return from the function. The values passed to the functions (arguments) are also treated like local variables.

Static Variables

Static variables are like local variables but they don’t die on return from the function. Instead their last value is retained, and it becomes available when the function is called again.

Pointers in C

A pointer is a variable suitable for keeping memory addresses of other variables. The values you assign to a pointer are memory addresses of other variables or other pointers.

Pointers in C are characterized by their value and data-type. The value is the address of the memory location the pointer points to, the type determines how the pointer will be incremented/decremented in pointer or subscript arithmetic.

Pointers are used to manipulate arrays and they can be used to return more than one value from a function.

Pointers are declared by using the asterisk(*).
int *p;

Each variable has two attributes: address and value. The address is the location in memory. In that location, the value is stored. During the lifetime of the variable, the address is not changed but the value may change.

void main (void)

int i;
int * a;
i = 10;
a = &i;
printf (” The address of i is %8u \n”, a);
printf (” The value of i is %d\n”, i);
printf (” The value at that location is %d\n”, *a);



The address of i is 631672
The value of i is 10
The value at that location is 10

Arrays and Pointers

An array is actually a pointer to the 0th element of the array. Dereferencing the array name will give the 0th element. This gives us a range of equivalent notations for array access. In the following examples, arr is an array.

arr[0] *arr
arr[1] *(arr+1)
arr[n] *(arr+n)

Arrays in C

An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.
Arrays in C is a data structure of multiple elements with the same data type. Array elements are accessed using subscript. The valid range of subscript is 0 to size -1.

Declaration of Array

int arr[10];


void main(void)

int a[5];
int i;
for(i = 0;i<5;i++)



for(i = 0;i<5;i++)

printf(“%d value of I is = %d\n”,i,a[i]);




1 value of I is = 0
2 value of I is = 1
3 value of I is = 2
4 value of I is = 3
5 value of I is = 4

Multidimensional Arrays

A multi-dimensional array of dimension n (i.e., an n-dimensional array or simply n-D array) is a collection of items which is accessed via n subscript expressions. Multidimensional arrays can be described as “arrays of arrays”.

Multidimensional Arrays Example

void main(void){

int a[3][2];
int i,j;

for(i = 0;i<3;i++){

for(j=0; j<2 ;j++) {




for(i = 0;i<3;i++){

for(j=0;j<2;j++) {

printf(“value in array %d\n”,a[i][j]);




Strings in C

Strings in c is character type array and ends with the NULL character. %s placeholder is used in the printf() function to display string values.

String Declaration

char name[50];


void main (void )

char *st1 = “abcd”;
char st2[] = “efgh”;
printf( “%s\n”, s1);
printf( “%s\n”, s2);



void main(void){

char myname[] = {‘J’,’h’,’o’,’n’};

printf(“%s \n”,myname);


String input and output

The gets function relieves the string from standard input device while put S outputs the string to the standard output device.

The function gets accepts the name of the string as a parameter, and fills the string with characters that are input from the keyboard till newline character is encountered.

The puts function displays the contents stored in its parameter on the standard screen.

Syntax of the gets() function

gets (str_var);

Syntax of the puts() function

puts (str_var);

String input and output Example

# include < stdio.h >
void main ()

char myname [40];
printf (“Type your Name :”);
gets (myname);
printf (“Your name is :”);


C String Functions

Function Description
strcpy(string1, string2) Copy string2 into string1
strcat(string1, string2) Concatenate string2 onto the end of string1
length = strlen(string) Get the length of a string
strcmp(string1, string2) Return 0 if string1 equals string2, otherwise nonzero
strchr(string1, chr); will find the first matching character in a string.

Structures and Unions

A structure is a collection of variables under a single name. These variables can be of different types, and each has a name which is used to select it from the structure. A structure is a convenient way of grouping several pieces of related information together.

Declaring Structures

struct mystruct

int numb;
char ch;


Structure has name mystruct and it contains two variables: an integer named numb and a character named ch.

Declaring structure variable

struct mystruct s1;

Accessing Member Variables




typedef can also be used with structures. The following creates a new type sb which is of type struct chk and can be initialised as usual:

typedef struct chk

char name[50];
int magazinesize;
float calibre;

} sb;

ab arnies={“adam”,30,7};

Unions in C

A union is an object that can hold any one of a set of named members. The members of the named set can be of any data type. Members are overlaid in storage. The storage allocated for a union is the storage required for the largest member of the union, plus any padding required for the union to end at a natural boundary of its strictest member.

union {

char n;
int age;
float weight;

} people;