FOCP - FUNCTIONS AND POINTERS (Unit 5)–16Mark Questions


UNIT V – 16 Marks


1. What are functions? Explain the types of functions in detail with an example program for each type.

A function is a self contained block or a sub program of one or more statements

that performs a special task when called.


· Library Functions

· User Defined functions

(a) Function Declaration

returntype function-name(Parameters);


int square(int, int);

(b) Function calling

function-name(actual parameters);


int square(a,b);

(c) Function Definition:

returntype function-name(formal parameters)




local variable declaration;

statement 1; statement 2; return(value);

void square(int a, int b)




Example for functions:

· Addition of two numbers where addition is a separate function

· Program using function for evaluating Fibonacci series.

2. Define arrays. Explain the array types with an example program for each type.

Arrays are data structures which hold multiple variables of the same data type. Consider the case where a programmer needs to keep track of a number of people within an organization. So far, our initial attempt will be to create a specific variable for each user.

This might look like,

int name1 = 101; int name2 = 232; int name3 = 231;

It becomes increasingly more difficult to keep track of this as the number of variables increase. Arrays offer a solution to this problem. An array is a multi-element box, a bit like a filing cabinet, and uses an indexing system to find each variable stored within it. In C, indexing starts at zero. Arrays, like other variables in C, must be declared before they can be used. The replacement of the above example using arrays looks like,

int names[4]; names[0] = 101; names[1] = 232; names[2] = 231; names[3] = 0;

We created an array called names, which has space for four integer variables. You may also see that we stored 0 in the last space of the array. This is a common technique used by C programmers to signify the end of an array. Arrays have the following syntax, using square brackets to access each indexed value (called an element).


so that x[5] refers to the sixth element in an array called x. In C, array elements start with 0. Assigning values to array elements is done by,

x[10] = g; and assigning array elements to a variable is done by, g = x[10];

In the following example, a character based array named word is declared, and each element is assigned a character. The last element is filled with a zero value, to signify the end of the character string (in C, there is no string type, so character based arrays are used to hold strings). A printf statement is then used to print out all elements of the array.

/* Introducing array's, 2 */

#include <stdio.h>



char word[20]; word[0] = 'H'; word[1] = 'e'; word[2] = 'l'; word[3] = 'l'; word[4] = 'o'; word[5] = 0;

printf("The contents of word[] is -->%s\n", word );



Arrays may consist of any of the valid data types. Arrays are declared along with all other variables in the declaration section of the program.

/* Introducing array's */

#include <stdio.h>



int numbers[100]; float averages[20]; numbers[2] = 10;


printf("The 3rd element of array numbers is %d\n", numbers[2]);


The above program declares two arrays, assigns 10 to the value of the 3rd element of array numbers, decrements this value ( --numbers[2] ), and finally prints the value. The number of elements that each array is to have is included inside the square brackets


The declaration is preceded by the word static. The initial values are enclosed in braces,


#include <stdio.h>



int x;

static int values[] = { 1,2,3,4,5,6,7,8,9 };

static char word[] = { 'H','e','l','l','o' };

for( x = 0; x < 9; ++x )

printf("Values [%d] is %d\n", x, values[x]);



Multi-dimensioned arrays have two or more index values which specify the element in the array.


In the above example, the first index value i specifies a row index, whilst j specifies a column index.


int m1[10][10];

static int m2[2][2] = { {0,1}, {2,3} };

sum = m1[i][j] + m2[k][l];

NOTE the strange way that the initial values have been assigned to the two-dimensional array m2. Inside the braces are,

{ 0, 1 },

{ 2, 3 }

Remember that arrays are split up into row and columns. The first is the row, the second is the column. Looking at the initial values assigned to m2, they are,

m2[0][0] = 0 m2[0][1] = 1 m2[1][0] = 2 m2[1][1] = 3


#include <stdio.h>



static int m[][] = { {10,5,-3}, {9, 0, 0}, {32,20,1}, {0,0,8} };

int row, column, sum;

sum = 0;

for( row = 0; row < 4; row++ )

for( column = 0; column < 3; column++ ) sum = sum + m[row][column]; printf("The total is %d\n", sum );



Consider the following program,

#include <stdio.h>



static char name1[] = {'H','e','l','l','o'}; static char name2[] = "Hello"; printf("%s\n", name1);

printf("%s\n", name2);


The difference between the two arrays is that name2 has a null placed at the end of the string, ie, in name2[5], whilst name1 has not. To insert a null at the end of the name1 array, the initialization can be changed to,

static char name1[] = {'H','e','l','l','o','\0'};

Consider the following program, which initialises the contents of the character based array word during the program, using the function strcpy, which necessitates using the include file string.h


#include <stdio.h>

#include <string.h>



char word[20];

strcpy( word, "hi there." );

printf("%s\n", word );


3. Explain the standard string functions with example to support each type.


The group of characters, digits and symbols enclosed within quotes is called as strings or character arrays. Strings are always terminated with ‘\0’ character(NULL).


char name[ ] = {‘H’,’E’,’L’,’L’,’O’};

Standard String Functions:

· strlen( )

· strcpy( )

· strncpy( )

· stricmp( )

· strcmp( )

· strncmp( )

· strcat( )

· strrev( ) etc.,

Example program:

· To read and display a string.

· Program to count the number of lines, words and characters in a text.

4. What are pointers? When and why they are used? Explain in detail with sample programs. (JAN 2009/MAY 2009)

Pointer variable is needed to store the memory address of any variable. Denoted by

(*) asterisk.

Pointer Declaration: Syntax:

datatype *variable-name;


int *a;

· Pointers and Arrays

· Pointers and Strings

· Pointer as function arguments

· Pointer too pointer

Example program:

· To add two numbers through variables and their pointers.

· To assign a pointer value to another variable.

5. Describe in detail about the Preprocessors in C. (MAY 2009)


The define statement is used to make programs more readable, and allow the

inclusion of macros. Consider the following examples,

#define TRUE 1 /* Do not use a semi-colon , # must be first character on line */

#define FALSE 0

#define NULL 0

#define AND &

#define OR |

#define EQUALS ==

game_over = TRUE;

while( list_pointer != NULL )



Macros are inline code which are substituted at compile time. The definition of a macro, which accepts an argument when referenced,

#define SQUARE(x) (x)*(x)

y = SQUARE(v);

In this case, v is equated with x in the macro definition of square, so the variable y is assigned the square of v. The brackets in the macro definition of square are

necessary for correct evaluation.

The expansion of the macro becomes y = (v) * (v);

Naturally, macro definitions can also contain other macro definitions,

#define IS_LOWERCASE(x) (( (x)>='a') && ( (x) <='z') )

#define TO_UPPERCASE(x) (IS_LOWERCASE (x)?(x)-'a'+'A':(x))

while(*string) {

*string = TO_UPPERCASE (*string);




These are used to direct the compiler to compile/or not compile the lines that


#ifdef NULL

#define NL 10

#define SP 32


In the preceding case, the definition of NL and SP will only occur if NULL has

been defined prior to the compiler encountering the #ifdef NULL statement. The scope of a definition may be limited by

#undef NULL

This renders the identification of NULL invalid from that point onwards in the source file.


This statement is used to classify existing C data types, eg,

typedef int counter; /* redefines counter as an integer */

counter j, n; /* counter now used to define j and n as integers */

typedef struct {

int month, day, year;


DATE todays_date; /* same as struct date todays_date */


Enumerated data type variables can only assume values which have been previously


enum month { jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec };

enum month this_month;

this_month = feb;

In the above declaration, month is declared as an enumerated data type. It consists of a set of values, jan to dec. Numerically, jan is given the value 1, feb the value 2, and so on. The variable this_month is declared to be of the same type as month, then is assigned the value associated with feb. This_month cannot be assigned any values outside those specified in the initialization list for the declaration of month.


#include <stdio.h>



char *pwest = "west",*pnorth = "north", *peast="east", *psouth = "south";

enum location { east=1, west=2, south=3, north=4};

enum location direction;

direction = east;

if( direction == east )

printf("Cannot go %s\n", peast);


The variables defined in the enumerated variable location should be assigned initial



Some routines may be time or space critical. Variables can be defined as being register based by the following declaration,

register int index;


Here variables may exist in separately compiled modules, and to declare that the

variable is external,

extern int move_number;

This means that the data storage for the variable move_number resides in another source

module, which will be linked with this module to form an executable program. In using a variable across a number of independently compiled modules, space should be allocated in only one module, whilst all other modules use the extern directive to access the variable.


These are statements which do not have any body associated with them.

/* sums all integers in array a containing n elements and initializes */

/* two variables at the start of the for loop */

for( sum = 0, i = 0; i < n; sum += a[i++] )


/* Copies characters from standard input to standard output until EOF is reached */

for( ; (c = getchar ()) != EOF; putchar (c));


It is possible to pass arguments to C programs when they are executed. The brackets which follow main are used for this purpose. argc refers to the number of arguments passed, and argv[] is a pointer array which points to each argument which is passed to main. A simple example follows, which checks to see if a single argument is supplied on the command line when the program is invoked.

#include <stdio.h>

main( int argc, char *argv[] )


if( argc == 2 )

printf("The argument supplied is %s\n", argv[1]);

else if( argc > 2 )

printf("Too many arguments supplied.\n");


printf("One argument expected.\n");


Note that *argv[0] is the name of the program invoked, which means that

*argv[1] is

a pointer to the first argument supplied, and *argv[n] is the last argument. If no arguments

are supplied, argc will be one. Thus for n arguments, argc will be equal to n + 1. The program is called by the command line, myprog argument1.

6. Brief call by value and call by reference in detail. (MAY 2009)

Call by value:

In call by value the value of the actual arguments are passed to the formal arguments and the operation is done on formal arguments.

Example program:

· To send two integer values using “call by value”.

Call by reference:

In call by reference the address of actual argument values are passed to formal argument values.

Example program:

· To send a value by reference to user defined function.

7. Discuss about function prototypes in detail. (or)

Explain about the different parameter passing methods with examples (JAN 2009)

· Function with arguments and return type.

· Function without arguments and return type.

· Function with arguments and no return type.

· Function without arguments and return type.

8. Define Structures. Explain structures in detail. (JAN 2009 / MAY 2009)

A structure is a collection of one or more variables of different data types grouped together under a single name. It contains different data types.


struct struct-name


type variable 1; type variable 2; type variable n;

} structure_variables;


struct student


char name[25];

int rollno;

int m1,m2,m3,total;

float avg;


· Structure within structure

· Array of structures

· Pointers to structures

· Structures and functions

Example program:

· To define a structure and read the member variable values from user.

· To copy structure elements from one object to another object.

9. Define Union. Explain Union in detail. (JAN 2009)

Union is a collection of variables similar to structure. The union requires bytes that are equal to number of bytes required for the largest number.


union student


char name[20];

int rollno,m1,m2,m3,tot;

float avg;


Union of structure

Union can be nested with another union.

Example program:

· Program to use structure within union. Display the contents of structure elements.