C Notes Unit 4 25 Reg
C Notes Unit 4 25 Reg
Regulation
PROGRAMMING 2025
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.
Example: sin, cos, power and Example: fibo, addition, and etc.,
5
etc.,
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.
Output
I/II Page 3
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025
Output
I/II Page 4
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025
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.
function definition:
1. with arguments and with return values int function( int a )
{
statements;
return a;
}
function declaration:
void function ( int );
I/II Page 6
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025
function declaration:
void function();
function declaration:
int 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
Output
I/II Page 8
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025
Output
Output
I/II Page 9
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025
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
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
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.
Output
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
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
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
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.
Like variables, pointers have to be declared before they can be used in your program.
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.
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 = &variable;
Output
I/II Page 22
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025
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
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.
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
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.
Output
I/II Page 24
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025
Dangling pointer
Complex pointer
Near pointer
Far pointer
Huge pointer
Output
I/II Page 25
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025
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).
Suppose the address of i, d and ch are 1000, 2000, 4000 respectively, therefore ip, dp and cp are
at 1000, 2000, 4000 initially.
I/II Page 26
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025
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
Output
I/II Page 28
CS25C01- PROBLEM SOLVING USING C VCET
Regulation
PROGRAMMING 2025
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).
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
Output
I/II Page 31