You are on page 1of 64

ECAP010

PROGRAMMING IN C
Function call C Programming
• Function call is a process to execute function by its
name in main function.

• Function call is divided in following two types: -


• Passing arguments by value (Call by value)

• Passing arguments by reference (Call by reference)


Passing arguments by value
• This method of passing arguments to a function copies
the actual value of an argument into the formal
parameter of the function.

• By default, C programming uses passing arguments by


value.

• This method uses in-mode semantics.


Program
#include<stdio.h>
int func(int x,int y)
{
return x*y;
}
int main(){
int a,b;
printf("Enter two numbers");
scanf("%d %d",&a,&b);
printf("Product of entered number using call by value is = %d ",func(a,b));
return 0;
}
Passing arguments by reference
• When we call a function by passing the addresses of
actual parameters then this way of calling the function is
known as passing arguments by reference.

• This technique uses in/out-mode semantics.


Program
#include<stdio.h>
int func(int *x,int *y)
{
return *x**y;
}
int main(){
int a,b;
printf("Enter two numbers");
scanf("%d %d",&a,&b);
printf("Product of entered number using call by value is = %d ",func(&a,&b));
return 0;
}
Call by Value vs. Call by Reference

Parameters Call by value Call by reference

Arguments Copy of variable is passed. Variable passed itself.

Value
Original value not modified. The original value is modified.
modification
Actual arguments are not Safe.
Actual arguments remain safe They can be accidentally
Safety as they cannot be modified modified, so you need to
accidentally. handle arguments operations
carefully.
Recursion in C Programming

• Recursion is a process, when a function calls itself repeatedly.

• Any function which calls itself is known as recursive function.


Recursion

• During recursion, need to be careful to define an exit condition from


the function, otherwise it will go into an infinite loop.

• Recursive functions are used to solve mathematical problems,


factorial of a number, generating Fibonacci series, etc.
Syntax
int main()
{

abc();

}
void abc()
{

abc();

}
Recursion Process
Recursive Function

Statements

True
Condition

False

Remaining Statements
Program
#include<stdio.h>
Each call gets its own copy of count
fun ( int count )
{
printf( "%d\n", count );
fun ( count + 1 );
}
int main()
{
Calling function in main
fun ( 1 );
return 0;
}
Library Functions in C Programming
Standard library functions are inbuilt functions
in C programming language

Library functions

Predefined

Declaration inside header file

Eg. printf(), scanf()


Library Functions in C Programming

The prototype and data definitions of these


functions are present in their
respective header files.
Library Functions in C Programming

The C library functions are provided by


the system and stored in the library.
Library Functions in C Programming

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.

Example: #include<stdio.h>
List of Most Used Header Files In C language
Header File Description
stdio.h This is standard input/output header file
in which Input/Output functions are
declared
All string related functions are defined
string.h
in this header file
math.h All maths related functions are defined
in this header file
time.h This header file contains time and clock
related functions
ctype.h All character handling functions are
defined in this header file
Header File <stdio.h>

It stands for standard input and standard


output used to perform input-output functions.

print f( ) – Used to display output on the


screen.
Header File <stdio.h>

It stands for standard input and standard


output used to perform input-output functions.

scanf( )– To take input from the user.


Header File <stdio.h>

It stands for standard input and standard


output used to perform input-output functions.

getchar( )– To return characters on the screen.


Header File <stdio.h>

It stands for standard input and standard


output used to perform input-output functions.

fgets( )– To take a line as an input.


.
Header File <stdio.h>

It stands for standard input and standard


output used to perform input-output functions.

fclose( )– To close a file.


Advantages of using C Library Functions

The functions are optimized for performance

It saves considerable development time

The functions are portable

User-friendly syntax
Advantages of using C Library Functions

The functions are optimized for performance

It saves considerable development time

The functions are portable

User-friendly syntax
Advantages of using C Library Functions

The functions are optimized for performance

It saves considerable development time

The functions are portable

User-friendly syntax
Advantages of using C Library Functions

The functions are optimized for performance

It saves considerable development time

The functions are portable

User-friendly syntax
Program : - Using math.h
#include <stdio.h>
#include <math.h>
int main()
{
float num, root;
printf("Enter a number: ");
scanf("%f", &num);
root = sqrt(num);
printf("Square root of %f = %f", num, root);
return 0;
}
Array in C

• An array is variable that can store multiple values.

• Arrays a kind of data structure that can store a fixed-size sequential


collection of elements of the same type.

• Array is non-primitive data type. (int, float, char etc.)

• Arrays consist of contiguous memory location.


Syntax

datatype name_of_array[size];

• Datatype

• Name of array

• Size (integer)
Array Initializing and Memory Representation
of an Array
int arr[5]={ 10,11,12,13,14};
Access Array Elements
• Array elements access by indices.

• Suppose you declare an array arr as above than first


element is arr[0], second element is arr[1] and so on.

arr[0] arr[1] arr[2] arr[3] arr[4]


Advantages of Arrays
• Arrays represent multiple data items of the same type
using a single name.

• Elements of array can be accessed randomly by using the


index number.

• Easy access to all the elements.

• Sorting and searching becomes easy.


Applications of Array
• Arrays can be used for CPU scheduling.

• Arrays can be used for reverse data elements, sort data


elements etc.

• Arrays are used to Perform Matrix Operations

• Arrays are used to Store List of values

• Arrays are also used to implement stack and queues.


Type of Array

Arrays

One Multi
dimensional dimensional
array array
Type of Array
• One dimensional Array

int a[5];

• Multi-dimensional Array

int m[3][3];
Program
#include <stdio.h>

int main()

int arr[5]={10,11,12,13,14};

for(int i=0;i<=4;i++)

printf("%d\n",arr[i]);

return 0;

}
Two Dimensional Array in C

• An array of arrays is known as two dimensional array.

• Two dimensional array in C language organized as matrices which is


used in C programming to represent rows and columns.
Declaration of two dimensional array in C
data_type name_of_array[rows][columns];

• Datatype

• Name of array

• rows (integer)

• columns (integer)

• Number of elements in 2D arrays is multiply of rows and columns for eg. Int
x[3][3] has 3*3 i.e. 9 elements.
Array Initializing and Memory Representation
of an Array
int x[3][4] = {{1,2,3}, {4,5,6}, {7,8,9}};

Index Value Index Value Index Value


Advantages of 2D Arrays

• 2D Arrays represent multiple data items of the same type in the form
of matrix.

• We can perform all matrices operation, sum of matrix, transpose of


matrix using 2D Array.
Program
#include <stdio.h> }
int main() printf("\n");
{ }
int x[3][4] = {{1,2,3}, {4,5,6}, return 0;
{7,8,9}}; }
int i,j;
for( i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d\t",x[i][j]);
Program
#include <stdio.h> }
int main() printf("\n");
{ }
int x[3][4] = {{1,2,3}, {4,5,6}, return 0;
{7,8,9}}; }
int i,j;
for( i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d\t",x[i][j]);
Sorting
A Sorting process is used to rearrange a given array or
elements based upon selected algorithm/ sort function.

25 30 15 12 16

12 15 16 25 30
Types of sorting

• Selection sort
Types of sorting

• Selection sort

• Insertion sort
Types of sorting

• Selection sort

• Insertion sort

• Bubble sort
Types of sorting

• Selection sort

• Insertion sort

• Bubble sort

• Merge Sort
Types of sorting

• Selection sort

• Insertion sort

• Bubble sort

• Merge Sort

• Quick sort
Types of sorting

• Selection sort

• Insertion sort

• Bubble sort

• Merge Sort

• Quick sort

• Heap sort
Selection sort

• Selection Sort finds the smallest element in the array and exchanges it
with the element in the first position, it then finds the second
smallest element and exchanges it with the element in the second
position and continues this process until the entire list is sorted.
Selection sort

• 22 10 15 18 >> 10 22 15 18

• 10 22 15 18 >> 10 15 22 18

• 10 15 22 18 >> 10 15 18 22

10 15 18 22
Insertion sort

• Insertion sort, start with one item, take a new item and sort the two
items relative to each other, then take a new item and sort the three
items relative to each other (swapping the new item with consecutive
values until it is no longer lower, and thus inserting it in that position),
and so on
Bubble sort

• Bubble sort, swap neighbours the larger items drop down while the
smaller ones bubble up, in n-1 passes through the array.

• 6 4 2 >> 462

• 4 6 2 >> 426

• 4 2 6 >> 246
Merge sort

• Merge Sort follows the Divide and Conquer rule where in it divides
the input array into two halves, sorts the two halves and merges the
two sorted halves.
Searching

• It is a process to find or search for the specified element in the given


list.

• Two commonly used searching algorithms

• Binary Search

• Linear Search
linear search

• In a linear search, array is traversed sequentially and each element is


checked until a match is found.

search element: 10

15 18 22 35 10 45 53
Binary search

• Binary Search Algorithm follows the Divide and Conquer strategy


where it finds the item from the sorted list of items.

• In a binary search, the data in the array is ordered and then the
middle of the contracted array is tested until the required match is
found
Character Array
• Character array is collection of characters, stored under a
single name.
Character Array
• Character array is collection of characters, stored under a
single name.

• Character arrays can be initialized using string literals


(Double quotes).
Character Array
• Character array is collection of characters, stored under a
single name.

• Character arrays can be initialized using string literals


(Double quotes).

• One dimensional array of characters also known as a


string.
Initializing Character Array
char abc[]=“Program”;

Data type

name of character
array

String
Initializing Character Array

char a[]=“Program”;

• “Program” is a string.

• String terminates with null character i.e. ‘\0’.

• String “Program” has 8 characters.


P r o g r a m \0
Initializing Character Array

• char abc[ ] =“hello”;

• char abc[10]= {‘h’,’e’,’l’,’l’,’o’,’\0’};

• Char abc[ ] = {‘h’,’e’,’l’,’l’,’o’,’\0’};

• Char abc[10]= “hello”;


Program
#include<stdio.h>
int main(){
char a[]={'h','e','l','l','o','\0'};
for(int i=0;i<=6;i++){
printf("%c",a[i]);
}
return 0;
}

output

You might also like