Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
1K views31 pages

C Notes Unit 4 25 Reg

Uploaded by

nkp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views31 pages

C Notes Unit 4 25 Reg

Uploaded by

nkp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

CS25C01- PROBLEM SOLVING USING C VCET

Regulation
PROGRAMMING 2025

Velammal College of Engineering and Technology, Madurai – 625 009


(Autonomous)
DEPARTMENT OF INFORMATION TECHNOLOGY
CS25C01- PROBLEM SOLVING USING C PROGRAMMING

UNIT IV- FUNCTIONS AND POINTERS


Modular Programming: Function Prototype, Function Definition, Function Call, Built-in
Functions: String Functions and Math Functions - User Defined Functions – Recursion -
Pointers: Pointer Operators, Pointer Arithmetic, Arrays and Pointers, Array of Pointers -
Parameter Passing: Pass by Value and Pass by Reference.

CO4: Develop applications using Functions and Pointers. (K3)

4.1 Modular Programming - Function prototype, function definition, function


call
Modular Programming
If the program is divided into number of functional parts, then we use to call it as
modular programming.
If the main program is divided into sub programs, then we can independently code each
sub module later combine into single unit. This type of individual modules is termed as
functions.
Advantages
The advantages of modular programming include −

 It is easy to understand the program.


 Debugging and maintenance becomes easy.
 Saves programmers or user’s time.
 Reuse the code where ever necessary.
Example of modular programming
An example of modular programming is given below −

I/II Page 1
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

The splitting of a problem into its related sub problems is analogous to the process of refining an
algorithm.

4.1.1 Functions
C functions are basic building blocks in a program. All C programs are written using
functions to improve re-usability, understandability and to keep track on them.
A large C program is divided into basic building blocks called C function. C function
contains set of instructions enclosed by “{ }” which performs specific operation in a C program.
Actually, Collection of these functions creates a C program.

Library Function Vs User Defined Function


S.No Library Function (LF) User Defined Function (UDF)
LFs are predefined function. UDFs are the function which is created by
1
user as per his / her own requirements.
LFs are part of header file UDFs are part of the program which
2 (Such as math.h) which is compile runtime.
called runtime.
In LF, the name of function is In UDF, the name of function is decided
4
given by developers. by user.
In LF, name of function cannot In UDF, name of function can be changed
4 be changed. any time.

Example: sin, cos, power and Example: fibo, addition, and etc.,
5
etc.,

4.1.2 Uses of Functions


 C functions are used to avoid rewriting same logic/code again and again in a program.
 We can call functions any number of times in a program and from any place in a
program.
 A large C program can easily be tracked when it is divided into functions.

I/II Page 2
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

 The core concept of C functions are, re-usability, dividing a big task into small pieces to
achieve the functionality and to improve understandability of very large C programs.

4.2 Function Declaration, Function Call and Function Definition


There are 4 aspects in each C function. They are,
 Function declaration or prototype – This informs compiler about the function name,
function parameters and return value’s data type.
 Function call – This calls the actual function
 Function definition – This contains all the statements to be executed.

Example Program: Addition of two numbers

Output

I/II Page 3
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

4.2.1 How to call functions in a program? (Parameter Passing)


There are two ways that a C function can be called from a program. They are,
1. Call by value
2. Call by reference
1. Call by Value (Pass by Value)
 In call by value method, the value of the variable is passed to the function as parameter.
 The value of the actual parameter cannot be modified by formal parameter.
 Different Memory is allocated for both actual and formal parameters. Because, value of
actual parameter is copied to formal parameter.
Note:
 Actual parameter – This is the argument which is used in function call.
 Formal parameter – This is the argument which is used in function definition
Example Program: Swapping of two numbers

Output

I/II Page 4
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

4.2.2. Call by Reference (Pass by Reference)


 In call by reference method, the address of the variable is passed to the function as
parameter.
 The value of the actual parameter can be modified by formal parameter.
 Same memory is used for both actual and formal parameters since only address is used by
both parameters.
Example Program: Swapping of two numbers

Output

I/II Page 5
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

4.4 Function Prototype and its types (arguments and return value)

Function can be called either with arguments or without arguments in a C program. These
functions may or may not return values to the calling function. Following are various types of
function prototype.
1. C function with arguments (parameters) and with return value.
2. C function with arguments (parameters) and without return value.
3. C function without arguments (parameters) and without return value.
4. C function without arguments (parameters) and with return value.

C Function Aspects Syntax


function declaration:
int function ( int );

function call: function ( a );

function definition:
1. with arguments and with return values int function( int a )
{
statements;
return a;
}

function declaration:
void function ( int );

function call: function( a );


2. with arguments and without return values function definition:
void function( int a )
{
statements;
}

I/II Page 6
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

function declaration:
void function();

function call: function();

4. without arguments and without return values function definition:


void function()
{
statements;
}

function declaration:
int function ( );

function call: function ( );

function definition:
4. without arguments and with return values int function( )
{
statements;
return a;
}

NOTE:
 If the return data type of a function is “void”, then, it can’t return any values to the calling
function.
 If the return data type of the function is other than void such as “int, float, double etc”,
then, it can return values to the calling function.
Example Program: 1. with arguments and with return values

Output

I/II Page 7
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Example Program: 2. with arguments and without return values

Output

Example Program: 4. without arguments and without return values

I/II Page 8
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Output

Example Program: 4. without arguments and with return values

Output

I/II Page 9
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

4.4 Built in Functions (Library Functions)


 Library functions in C language are inbuilt functions which are grouped together and
placed in a common place called library.
 Each library function in C performs specific operation.
 We can make use of these library functions to get the pre-defined output instead of
writing our own code to get those outputs.
 These library functions are created by the persons who designed and created C compilers.
 All C standard library functions are declared in many header files which are saved as
file_name.h.
 Actually, function declaration, definition for macros are given in all header files.
 We are including these header files in our C program using “#include<file_name.h>”
command to make use of the functions those are declared in the header files.
 When we include header files in our C program using “#include<filename.h>” command,
all C code of the header files are included in C program. Then, this C program is
compiled by compiler and executed.

Library function string.h


All C inbuilt functions which are declared in string.h header file are given below:

String
Description
Functions
strcat( ) Concatenates str2 at the end of str1
strncat( ) Appends a portion of string to another
strcpy( ) Copies str2 into str1
Copies given number of characters of one string to
strncpy( )
another
strlen( ) Gives the length of str1
Returns 0 if str1 is same as str2. Returns <0 if strl <
strcmp( )
str2. Returns >0 if str1 > str2
Same as strcmp() function. But, this function negotiates
strcmpi( )
case. “A” and “a” are treated as same.

I/II Page 10
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

strchr( ) Returns pointer to first occurrence of char in str1


strrchr( ) last occurrence of given character in a string is found
strstr( ) Returns pointer to first occurrence of str2 in str1
strrstr( ) Returns pointer to last occurrence of str2 in str1
strdup( ) Duplicates the string
strlwr( ) Converts string to lowercase
strupr( ) Converts string to uppercase
strrev( ) Reverses the given string
strset( ) Sets all character in a string to given character
It sets the portion of characters in a string to given
strnset( )
character
strtok( ) Tokenizing given string using delimiter

Library function math.h


All C inbuilt functions which are declared in math.h header file are given below:

Functions Description
This function returns the nearest integer which is less than
floor( )
or equal to the argument passed to this function.
This function returns the nearest integer value of the
float/double/long double argument passed to this
function. If decimal value is from “.1 to .5”, it returns
round( )
integer value less than the argument. If decimal value is
from “.6 to .9”, it returns the integer value greater than the
argument.
This function returns nearest integer value which is
ceil( ) greater than or equal to the argument passed to this
function.
sin( ) This function is used to calculate sine value.
cos( ) This function is used to calculate cosine.
cosh( ) This function is used to calculate hyperbolic cosine.
This function is used to calculate the exponential “e” to
exp( )
the xth power.
tan( ) This function is used to calculate tangent.
tanh( ) This function is used to calculate hyperbolic tangent.
sinh( ) This function is used to calculate hyperbolic sine.
log( ) This function is used to calculate natural logarithm.
log10( ) This function is used to calculate base 10 logarithms.
This function is used to find square root of the argument
sqrt( )
passed to this function.
pow( ) This is used to find the power of the given number.
trunc( ) This function truncates the decimal value from floating
I/II Page 11
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

point value and returns integer value.

4.4.1 Recursion
When a function in C program calls by itself, then that function is called recursive
function. This process is called recursion.

Factorial and Fibonacci series will be a good example for recursive function as it calls by
itself.

Example Program: Factorial of a number using Recursion

Output

Example Program: Fibonacci Series using Recursion

I/II Page 12
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Output

Example Program:
 Computation of Sine series
Sin x is a series of sin function of trigonometry; it can expand up to infinite number of
term. Through this series, we can find out value of sin x at any radian value of sin x
graph.

I/II Page 13
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Logic:
1. First the computer reads the value of x and limit from the user.
2. Now convert x to radian value x=x*(4.1415\180)
3. Then using for loop the value of sin(x) is calculated.
4. Finally the value of sin(x) is printed.

Program

Output

I/II Page 14
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

 Scientific calculator using built-in functions,

I/II Page 15
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

I/II Page 16
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

I/II Page 17
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

I/II Page 18
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Output

Binary Search using recursive functions.


Program

I/II Page 19
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

I/II Page 20
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Output

4.5 Pointers – Pointer operators – Pointer arithmetic – Arrays and pointers –


Array of pointers – Example Program: Sorting of names
Pointer

A pointer is a variable that stores the address of another variable. Unlike other variables
that hold values of a certain type, pointer holds the address of a variable. For example, an integer
variable holds (or you can say stores) an integer value, however an integer pointer holds the
address of an integer variable.

4.5.1 Declaring a pointer

Like variables, pointers have to be declared before they can be used in your program.

A pointer declaration has the following form.

date_type *pointer_variable_name;

where,

 data_type indicates the type of the variable that the pointer points to.

I/II Page 21
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

 The asterisk (*: the same asterisk used for multiplication) which is indirection operator,
declares a pointer.

4.5.2 Initialize a pointer

After declaring a pointer, we initialize it like standard variables with a variable address. If
pointers are not uninitialized and used in the program, the results are unpredictable and
potentially disastrous.

Pointer initialization is done with the following syntax.

pointer = &variable;

Example: Program to illustrate the concepts of Pointers

Output

I/II Page 22
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

4.5.3 Pointers Operators

Operator Meaning

It serves 2 purpose
* 1. Declaration of a pointer
2. Returns the value of the referenced variable
& It serves only one purpose.
 Returns the address of a variable

4.5.4 Types of a pointer

1. NULL Pointer

We can create a null pointer by assigning null value during the pointer declaration. This
method is useful when you do not have any address assigned to the pointer. A null pointer
always contains value 0.

Example: Program about NULL Pointer

Output

2. void Pointer

A void pointer is also called as a generic pointer. It does not have any standard data type.
A void pointer is created by using the keyword void. It can be used to store an address of any
variable.
I/II Page 23
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Example: Program about void Pointer

Output

4. wild Pointer

A pointer is said to be a wild pointer if it is not being initialized to anything. These types
of pointers are not efficient because they may point to some unknown memory location which
may cause problems in our program and it may lead to crashing of the program. One should
always be careful while working with wild pointers.

Example: Program about wild Pointer

Output

I/II Page 24
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Other types of pointers in 'C' are as follows:

 Dangling pointer
 Complex pointer
 Near pointer
 Far pointer
 Huge pointer

4.5.5 Direct and Indirect Access Pointers


There are two equivalent ways to access and manipulate a variable content.
 Direct access: we use directly the variable name
 Indirect access: we use a pointer to the variable

Example: Program about Access Pointers

Output

4.6 Pointers Arithmetic


We can’t perform every type of arithmetic operations with pointers. Pointer arithmetic is
slightly different from arithmetic we normally use in our daily life. The only valid arithmetic
operations applicable on pointers are:

I/II Page 25
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

1. Addition of integer to a pointer


2. Subtraction of integer to a pointer
3. Subtracting two pointers of the same type

The pointer arithmetic is performed relative to the base type of the pointer. For example,
if we have an integer pointer ip which contains address 1000, then on incrementing it by 1, we
will get 1004 (i.e 1000 + 1 * 4) instead of 1001 because the size of the int data type is 4 bytes. If
we had been using a system where the size of int is 2 bytes then we would get 1002 ( i.e 1000 + 1
* 2 ).
Similarly, on decrementing it we will get 996 (i.e 1000 - 1 * 4) instead of 999.
So, the expression ip + 4 will point to the address 1016 (i.e 1000 + 4 * 4).

Consider the following:

Suppose the address of i, d and ch are 1000, 2000, 4000 respectively, therefore ip, dp and cp are
at 1000, 2000, 4000 initially.

Pointer Arithmetic on Integers

Pointer Arithmetic on double

I/II Page 26
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Pointer Arithmetic on Characters

Note: When we increment or decrement pointer variables using pointer arithmetic then, the
address of variables i , d , ch are not affected in any way.

Example
Program

Output

I/II Page 27
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

4.7 Arrays and pointers


An array is a block of sequential data.
Example Program: Displaying the addresses of elements in an array

Output

I/II Page 28
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

There is a difference of 4 bytes between two consecutive elements of array x. It is


because the size of int is 4 bytes (on our compiler).

Notice that, the address of &x[0] and x is the same. It's because the variable name x points to the
first element of the array.

From the above example, it is clear that &x[0] is equivalent to x. And, x[0] is equivalent to *x.
Similarly,
 &x[1] is equivalent to x+1 and x[1] is equivalent to *(x+1).
 &x[2] is equivalent to x+2 and x[2] is equivalent to *(x+2).
 ...
 Basically, &x[i] is equivalent to x+i and x[i] is equivalent to *(x+i).

Example Program 1: Pointers and Arrays

I/II Page 29
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Output

Example Program 2

Output 4

In this example, &x[2], the address of the third element, is assigned to the ptr pointer.
Hence, 4 was displayed when we printed *ptr. And, printing *(ptr+1) gives us the fourth
element. Similarly, printing *(ptr-1) gives us the second element.

I/II Page 30
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025

Array of pointers – Example Program: Sorting of names


Example Program

Output

I/II Page 31

You might also like