Sunteți pe pagina 1din 8

Rajalakshmi Engineering College, Chennai 602 105

Department of Computer Science and Engineering

II-Semester CSE

CS6202 Programming and Data Structures I

What is the need for user defined functions?

The functions can be developed by different people and can be combined together as one
Easy to code and debug.
Functions support reusability. That is, once a function is written, it can be called from any other
module without having to rewrite the same. This saves time in rewriting the same code.

Distinguish between library function and user defined function.

Library Function User Defined Function

The library functions are predefined sets of
The user-defined functions defined by the
functions. user according to his/her requirement.
A user cannot understand the internal The user certainly understands the internal
working of these functions. working of the function and has full scope to
implement his/her ideas in the function.
The user can only use the functions but The user can modify the function according
cannot change or modify them. to the requirement.

Define function prototyping. Why it is necessary? (or) What is function declaration?

A function prototype is the information to the compiler regarding the user-defined function
name, the data type and the number of values to be passed to the function and the return data type
from the function. This is required because the user-defined function is written towards the end of
the program and the main does not have any information regarding these functions.

The function prototypes are generally written before main. A function prototype should
end with a semicolon

Function Prototypes declare only the signature of the function before actually defining the
function. The signature includes function name, return type, list of parameter data types and
optional names of formal parameters.

General Form

return_data_type function_name(data_type arg1,

data_type arg2, . . . data_type argn);

Distinguish between call by value and call by reference.

Call by value Call by reference

A copy of value is passed to the function. An address of value is passed to the function.
Consumes more memory space, because Consumes less memory space, because
formal parameter also occupies memory irrespective of the actual arguments data type,
space. each pointer occupies only 4 bytes.
Takes more time for execution, because the Takes less time because no values are copied.
values are copied.
Changes made inside the function is not Changes made inside the function is reflected
reflected on other functions. outside the function also.

Define array. What are the different types of arrays?

An array is a collection of similar data types in which each element is located in separate
memory locations. It is a linear and homogeneous data structure. It means similar data types are
stored contiguously in memory and that too under one variable name.

Arrays can be classified into three types. They are:

1. One-dimensional arrays
2. Two-dimensional arrays
3. Multi-dimensional arrays

List some examples where the concept of an array can be used.

1. CGPA of a class of students.

2. List of employees and their email ids.
3. Table of daily rainfall data.

What are the drawbacks of liner array?

The following are limitations of linear array:

The number of elements, which are to be stored in an array, is to be known first.
When an array is declared, memory is allocated to it. If array is not filled completely, the
vacant place occupies memory.
Once the array is declared, its dimension can be changed.

What is compile time initialization of an array?

We can initialize the elements of array in the same way as the ordinary variables when they
are declared. The general form of initialization array is:

General Form

data_type array_name[size] = {list of values};

The value in the list are separated by commas.


The statement:

int number[3] = {0, 0, 0};

will declare the variable number as an array of size 3 and will assign zero to each element.


How will you declare a two-dimensional array? Give example.

Like any other variable, array must be declared before they are used so that the compiler can
allocate space for them in memory.

General Form

data_type array_name[row_size][column_size];

Where data_type specifies the type of element that will be contained in the array, such as int, float
or char. The row_size indicates the maximum number of rows and the column_size indicates the
maximum number of columns, and the product of row_size x column_size represents the maximum
number of elements that can be stored inside the array.


int value[4][3];

declares the value to be an array containing 12 (4 x 3) integer elements. Any subscript [0][0]
to [3][2] are valid. As with the single-dimensional arrays, each dimension of the array is indexed
from zero to its maximum minus one; the first index selects the row and the second index selects the
column within that row.

How will you declare a multi-dimensional array? Give example.

General Form

data_type array_name[s1][s2][s3]...[sn];

where si is the size of the ith dimension.


int sales[4][5][12];
float table[5][4][5][3];

Here, sales is a three-dimensional array declared to contain 240 integer type elements.
Similarly table is a four-dimensional array containing 300 elements of floating-point type.

Define string.

In C language, the group of characters, digits, and symbols enclosed within double quotation
marks is called string. The string is always declared as character arrays. In other words, character
arrays are called strings.

How to declare and initialize string variables?

C does not support strings as a data type. However, it allows us to represent strings as
character arrays. In C, therefore, a string variable is any valid C variable name and is always
declared as an array of characters.


General Form

The general form of declaration of a string variable is:

char varname[size];

The size determines the number of characters in the varname.


char city[10];
char name[30];

When the compiler assigns a character string to a character array, it automatically supplies a
null character (\0 ) at the end of the string. Therefore, the size should be equal to the maximum
number of characters in the string plus one.

Like numeric arrays, character arrays may be initialized when they are declared. C permits a
character array to be initialized in either of the following two forms:

char city [8] = CHENNAI;

char city [8]={C,H,E,N,N,A,I,\0};

List the string handling functions available in C?

Function Action
strcat() Appends one string to another.
strchr() Scans a string for the first occurrence of a given character.
strcmp() Compare two strings.
stricmp() Compare two strings without case sensitivity.
strcpy() Copies string src to dest.
strlen() Calculates length of a string.
strlwr() Converts s to all lowercase.
strupr() Converts s to all uppercase.
strncat() Appends a portion of one string to another.
strncmp() Compare portions of two strings.
strnicmp() Compare portions of two strings, without case sensitivity.
strncpy() Copies at most maxlen characters of src to dest.
strrev() Reverses all characters in s (except for the terminating null).


List the preprocessor directives and their functions.

Directive Function
#define Defines a macro substitution
#undef Undefines a macro
#include Specifies the files to be included
#ifdef Test for a macro definition
#endif Specifies the end of #if
#ifndef Test whether a macro is not defined
#if Test a compile-time condition
#else Specifies alternatives when #if test fails

What is a macro substitutions directives?

Macro substitution is a process where an identifier in a program is replaced by a predefined

string composed of one or more tokens. The preprocessor accomplishes this task under the direction
of #define statement. This statement, usually known as a macro definition (or simply a macro).

General Form

#define identifier string

What is a argumented macro substitution?

The preprocessor permits us to define more complex and more useful form of replacements.

General Form

#define identifier(f1, f2, . . . fn) string

Notice that there is no space between the macro identifier and the left parentheses. The
identifiers f1, f2,.......,fn are the formal macro arguments that are analogous to the formal arguments
in a function definition.

What is a nested macro substitution? Give Example.

We can also use one macro in the definition of another macro. That is, macro definitions
may be nested.


For instance, consider the following macro definitions.

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

#define CUBE(x) (SQUARE(x) * (x))
#define SIXTH(x) (CUBE(x) * CUBE(x))

The preprocessor expands each #define macro, until no more macros appear in the text. For
example, the last definition is first expanded into:

((SQUARE(x) * (x)) * (SQUARE(x) * (x)))


Since SQUARE (x) is still a macro, it is further expanded into


which is finally evaluated as X6.

How to undefined a macro?

A defined macro can be undefined, using the statement #undef.

General Form

#undef identifier


#undef TEST

This is useful when we want to restrict the definition only to a particular part of the program.

Define pointer and explain how to declare and assign pointer to an integer.

A pointer is a data type which stores the address of a variable. A variable is a name given to
a set of memory locations allocated to it. For every variable there is an address, the starting address
of its set of memory locations. If a variable called p holds the address of another variable i then p is
called as a pointer variable and p is said to point to i.


int qty;
int *ptr; /* declaration */
ptr = &qty; /* initialization */

What are the operators exclusively used with pointers?

The operators exclusively used with pointers are & and *.

In C, it is possible to access and display the address of the memory location of a variable
using the & operator with variable name.

The pointer variable is needed to store the memory address of any variable and is denoted by
an (*) asterisk symbol.

What is a null pointer?

Using a pointer without initializing it to any data may result in data corruption or even
program crash. To differentiate between initialized and uninitialized pointers, we usually set an
uninitialized pointer to NULL.



/* Initializing pointer to NULL */

int *ptr = NULL;

A pointer can be checked whether it is pointing to NULL using the == (Equal to) operator.

Write a C program to multiply two numbers without using * operator.


#include <stdio.h>

int main()
int i, fno, sno, res = 0;
printf("Enter the first number : ");
scanf("%d", &fno);
printf("Enter the second number : ");
scanf("%d", &sno);
for(i = 1; i <= fno; i++)
res = res + sno;
printf("Result = %d", res);
return 0;


Enter the first number : 2

Enter the second number : 3
Result = 6

List any four advantages of pointers.

Pointers save memory space.

Execution time with the pointer is faster because data is manipulated with the address,
that is, direct access to memory location.
The memory is accessed efficiently with the pointers. The pointer assigns the memory
space and it also releases the memory. Dynamically memory is allocated.
Pointer are used with data structures and in file handling.

What is pointer of pointer? Give Example.

It is possible to make a pointer to point to another pointer, thus creating a chain of pointers
as shown.

Here, the pointer variable p2 contains the address of the pointer variable p1, which points to
the location that contains the desired value. This is known as multiple indirections.


A variable that is a pointer to a pointer must be declared using additional indirection
operator symbols in front of the name. Example:

int **p2;

This declaration tells the compiler that p2 is a pointer to a pointer of int type. Remember, the
pointer p2 is not a pointer to an integer, but rather a pointer to an integer pointer.

We can access the target value indirectly pointed to by pointer to a pointer by applying the
indirection operator twice.