c programming all chapters notes
c programming all chapters notes
Module 1
Introduction to C Programming
What is Computer ?
Characteristics of computer
Benefits of computer
Computer Components
Input Devices
Output Devices
Memory Unit
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
WHAT IS COMPUTER ?
A computer is a programmable
electronic device that accepts raw
data as input and processes it with a
set of instructions (a program) to
produce the result as output
The father of
the computer:
Charles
Babbage engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
WHAT IS COMPUTER ?
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
CHARACTERISTICS OF COMPUTER
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
BENEFITS OF COMPUTER
Increases
Productivi Storage
ty
Connects
Improves
to the
ability
Internet
Organizes Provides
data and an
informatio entertain
n ment
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
COMPUTER COMPONENTS
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
COMPUTER COMPONENTS
1. Input Devices
2. Output Devices
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
OUTPUT DEVICES
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
OUTPUT DEVICES
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
COMPUTER COMPONENTS
3. Central Processing
Unit
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
CENTRAL PROCESSING UNIT (CPU)
4. Memory Unit
Memory unit is used to retain the program instruction and data for
processing
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
MEMORY UNIT
Primary Memory
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
MEMORY UNIT
Primary Memory
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
MEMORY UNIT
Secondary Memory
External Memory
High storage capacity
Permanent storage
Non volatile
Slower access
Magnetic or optical memory - Less Expensive
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
MEMORY UNIT
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
Introduction to
Algorithm and Flowchart
What is Algorithm ?
Characteristics of algorithm
Flowchart
Flowchart Symbols
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
WHAT IS ALGORITHM
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
CHARACTERISTICS OF ALGORITHM
Step 1: Start
Step 2: Read values num1 and num2.
Step 3: Add num1 and num2 and assign the result to
sum.
sum←num1+num2
Step4: Display sum
engineering _munotes
Step 5: Stop
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
HOW TO WRITE AN ALGORITHM
Example: Find the largest number among three
numbers
Step 1: Start
Step 2: Read variables a, b and c.
Step 3: If a > b
If a > c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b > c
Display b is the largest number.
Else
Display c is the greatest number.
Step 4: Stop engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
FLOWCHART
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
FLOWCHART SYMBOLS
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
HOW TO DRAW A FLOWCHART
Example: Add two numbers
Step 1: Start
Step 2: Read values num1 and num2.
Step 3: Add num1 and num2 and assign the result to
sum.
sum←num1+num2
Step 5: Display sum
Step 5: Stop
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
HOW TO DRAW A FLOWCHART
Example: Find the largest number among three
numbers
Start
Input a, b,
c
No Is Yes
a>b ?
Yes Is No No Is Yes
b>c ? a>c ? Print a is
Print b is Print c is
the largest
the largest the largest
number
number number
Endengineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
Introduction to C
Programming
What is Programming Language ?
Types of Programming Languages
What is Compiler ?
What is Interpreter ?
Compiler Vs Interpreter
C Programming Language
Features of C
Applications of C
C Program Structure
Compilation Process
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
WHAT IS PROGRAMMING LANGUAGE
A programming language is a computer language that is
used by programmers (developers) to communicate with
computers
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
TYPES OF PROGRAMMING LANGUAGES
User friendly
High Level Programming Language
Used to develop applications, websites
Requires compiler and interpreter
Example: Python, C, C++, Java
Middle Level Programming Related to both high level and Machine level
Language languages
Requires Assembler
Example: Assembly Language
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
TYPES OF PROGRAMMING LANGUAGES
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
WHAT IS COMPILER ?
A compiler is a computer program that transforms code
written in a high-level programming language into the
machine code
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
WHAT IS INTERPRETER ?
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
COMPILER VS INTERPRETER
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
COMPILER VS INTERPRETER
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
C PROGRAMMING LANGUAGE
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
APPLICATIONS OF C
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
C PROGRAM STRUCTURE
/* C program to Print Hello World */
#include<stdio.h>
#define MAX 10
int a = 20;
int main()
{
printf(“Hello World”);
printf(“a = %d”, a);
return 0;
engineering _munotes
}
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
COMPILATION PROCESS
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
Basics of C
Constants
Keywords
Identifiers
Variables
Types of Variables
Data Types
Operators
Unary Operators
Binary Operators
Ternary Operators
Operator Precedence
Operator Associativity
Format Specifiers
engineering
Formatted I/O _munotes
Functions
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
CONSTANTS IN C
1. Numeric
Constants
String Constants:
Rules for Constructing String Constants:
Sequence of characters enclosed within double
String constants should be enclosed
quotes
within double quotes
Example:
String constants can be of any length
“college” “m” “a8bn”
String constant ends with a null
character assigned to it by the compiler
Note:
1. ‘a’ ≠ “a”
2. Compiler will add ‘\0’ (null character at the engineering _munotes
end of the string PRPEARED BY: PROF. SAYLEE NARKHEDE ©
CONSTANTS IN C
2. Character
Constants
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
CONSTANTS IN C
Defining Constant
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
KEYWORDS IN C
32 Keywords in C
Keywords cannot be used as a variable names
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
KEYWORDS IN C
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
IDENTIFIERS IN C
Identifiers represent the name in the C program; Like
names of variables, functions, arrays, structures, unions,
labels
Note:
C is a case
sensitive
language
Sum ≠ sum
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
IDENTIFIERS IN C
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
VARIABLES IN C
A variable is a name given to the memory location
where data is stored depending upon the type of
the variable
Variable Declaration:
100 104
Syntax:
10 20
data_type variable_name;
n1 n2
Example:
140 200
int choice;
5 ‘a’
char ans;
n3 ch
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
VARIABLES IN C
Variable Initialization:
Variable initialization means assigning some value to that variable
Example:
int ch;
ch = 10;
OR
Variable Declaration and initialization on the same line
int ch = 10;
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
TYPES OF VARIABLES
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
TYPES OF VARIABLES
1. Local Variables
A variable that is declared inside the function or block is called a local variable
Scope: Lies within the function or block of code
Life: Stay in the memory till the end of the program
Example:
void fun1()
{
int lv = 20;
printf(“value = %d”, lv);
} engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
TYPES OF VARIABLES
2. Global
Variables
A variable declared with static keyword which retains its value within the
function calls is called as static variable
Cannot be initialized again
Scope: Local to the function or block
Life: Value retains within the function calls
Example:
static int a = 11;
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
TYPES OF VARIABLES
4. Automatic
Variables
All variables in C that are declared inside the block, are automatic variables by
default
Explicit declaration using auto keyword
Scope: Local to the function or block
Life: Till the end of the block
Example:
void fun1()
{
int a = 10; // Automatic
auto int b = 20; engineering _munotes
} PRPEARED BY: PROF. SAYLEE NARKHEDE ©
TYPES OF VARIABLES
5. External Variables
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
DATATYPES IN C
Data types refer to the characteristics of data stored into
a variable
Data type helps you find Size, range and type of 100 104
value 10 20
C Data Types are used to: n1 n2
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
DATATYPES IN C
Primitive Data Types
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
DATATYPES IN C
Primitive Data Types
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
DATATYPES IN C
Primitive Data Types
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
DATATYPES IN C
Primitive Data Types
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
OPERATORS IN C
C operators are symbols that are used to perform mathematical or logical
manipulation
The C programming language is rich with built-in operators
Operators take part in a program for manipulating data and variables and form
a part of the mathematical or logical expressions
Mathematical Expression:
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
OPERATORS IN C
Operators are symbols or a set of symbols that change or assign values,
combine values, or check or verify values in your code
Types of Operators:
1. Unary Operator
2. Binary Operator
3. Ternary Operator
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
UNARY OPERATORS
Unary Operators are the operators which require single operand to perform any action
1. Sizeof
2. Unary minus
3. Logical NOT
4. Increment
5. Decrement
6. & (Addrss of)
7. * (Value of)
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
UNARY OPERATORS
Unary Operators are the operators which require single operand to perform any action
Example: sizeof(operand)
2. unary minus: it negates a value (Prefix Operator)
Example: a = -10;
b = -a;
3. Logical NOT: The logical NOT operator inverts the value of a Boolean variable (Prefix
Operator)
engineering _munotes
Example: c = True
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
UNARY OPERATORS
Unary Operators are the operators which require single operand to perform any action
a) Post Increment:
Example: a++
b) Pre Increment:
Example: ++a
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
UNARY OPERATORS
Unary Operators are the operators which require single operand to perform any action
a) Post Decrement:
Example: a--
a) Pre Decrement:
Example: --a
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
UNARY OPERATORS
Unary Operators are the operators which require single operand to perform any action
7. * (Value of): Returns value stored at the address (Works with pointers)
Example: *a
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
BINARY OPERATORS
Binary Operators are the operators which require two operands to perform any action
1. Arithmetic
2. Relational
3. Logical
4. Bitwise
5. Assignment
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
BINARY OPERATORS
1. Arithmetic Operators: used to perform arithmetic/mathematical operations
Consider a = 20 and b = 10
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
BINARY OPERATORS
2. Relational Operators: used to compare two values
Consider
a=5
b = 10
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
BINARY OPERATORS
3. Logical Operators: used to test more than one condition
Consider x = 10 and y = 20
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
BINARY OPERATORS
4. Bitwise Operators: used to perform bit level operations
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
BINARY OPERATORS
5. Assignment Operators: used for assigning a value to a variable
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
TERNARY OPERATOR
The ternary operator is also called as “Conditional Operator”
Syntax:
Condition ? expression1 : expression2
Example:
x = a > b ? a+10 : b;
A ternary operator is a short form of if-else
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
OPERATOR PRECEDENCE
The precedence of operators determines which operator is
executed first if there is more than one operator in an
expression
=900
=10+(20*30)
=610
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
OPERATOR PRECEDENCE
Problem:
To generate machine level instructions that could properly evaluate arithmetic
expression
X = (a / b)* (c ‒ d)
X=a/b*c-d
X = (a / b * c) - d
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
OPERATOR PRECEDENCE
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
OPERATOR ASSOCIATIVITY
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
PRACTICE QUESTION
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
INPUT OUTPUT FUNCTIONS
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
FORMAT SPECIFIERS
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
FORMATED I/O FUNCTIONS
printf()
Syntax:
printf(“Format string”, List of variables);
Example:
printf(“\n Value of a = %d”, a);
Syntax:
scanf(“Format string”, List of addresses of variables);
Example:
scanf(“%d %f %c”, &a, &b, &c);
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
UNFORMATED I/O FUNCTIONS
Unformatted Input
functions
getch(): getche():
getch() is used to get a character getche() is used to get a character
from the console but does not echo to from the console and echo to the
the screen screen
Example: Example:
char ch; char_munotes
engineering ch;
ch = getch(); ch = getche();
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
UNFORMATED I/O FUNCTIONS
Unformatted Input
functions
getchar(): gets():
getchar() is used to get or read the gets() accepts single or multiple
input (i.e a single character) at characters of string including spaces
runtime
Library: stdio.h
Library: stdio.h
Example :
Example: char ch[10];
char ch; gets(ch);
engineering _munotes
putch(): putchar():
putch displays any alphanumeric putchar() displays any alphanumeric
characters to the standard output device characters to the standard output device
Syntax: Syntax:
putch(variable_name); putchar(variable_name);
Example: Example:
putch(ch); putchar(ch);
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
UNFORMATED I/O FUNCTIONS
Unformatted Output
functions
puts():
puts() displays single or multiple characters of string including
spaces to the standard output device
Syntax:
puts(variable_name);
Example:
char ch[10]="Example";
puts(ch); engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
C PROGRAMMING
Module 2
Control Structures
In Sequence structure instructions are executed in the order they are written following the
sequential flow
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
SELECTION STRCTURE
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
LOOP STRUCTURE
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
Decision Control
Structures
If statement
If else statement
Nested if else
Else if ladder
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
IF STATEMENT
Syntax:
if(expression)
{
engineering _munotes
//code to be executed PRPEARED BY: PROF. SAYLEE NARKHEDE ©
IF STATEMENT
#include<stdio.h>
int main()
{
int n;
Example: Find whether the
printf("Enter a number:");
given number is even number scanf("%d", &n);
if(n%2 == 0)
{
printf("%d is even number“ ,n);
}
return 0;
}
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
IF ELSE STATEMENT
The if-else statement is used to perform two
operations for a single condition
i.e., one is for the correctness of the condition,
and the
other is for the incorrectness of the condition
if and else block cannot be executed
simultaneously
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
IF ELSE STATEMENT
int main()
Example: Find whether the given {
number is even number or odd number int n;
printf("Enter a number:");
scanf("%d", &n);
Syntax: if(n%2 == 0)
if(expression) {
printf("%d is even
{
number“ ,n);
//code to be executed if condition is }
true else
} {
else printf("%d is odd number“ ,
n);
{
}
//code to be executed if condition is return 0;
false engineering _munotes
}
} PRPEARED BY: PROF. SAYLEE NARKHEDE ©
NESTED IF ELSE STATEMENT
When an if else statement is
present inside the body of another
“if” or “else” then this is called
nested if else
It is used when there are more
than one condition to check at a
time
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
NESTED IF ELSE STATEMENT
Syntax:
if(condition)
{
if(condition2)
{
//Statements inside the body of nested "if"
}
else
{
//Statements inside the body of nested "else"
}
}
else
{
//Statements inside the body of "else"
} engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
NESTED IF ELSE STATEMENT
Example: Find whether the person is eligible for marriage or not based on the age
and gender
#include<stdio.h> else
int main() {
{ if(age>= 18)
int age; printf("\n Eligible for marriage");
char gender; else
printf("\n Your gender ? (M/F)"); printf("\n Not eligible for Marriage");
scanf("%c", &gender); }
printf("\n Enter your age:"); return 0;
scanf("%d", &age); }
if(gender == 'M')
{
if(age >= 21)
printf("\n Eligible for marriage");
else
printf("\n Not eligible for Marriage");
}
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
ELSE IF LADDER
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
Loop Control Structures
Looping structure
for loop
do while loop
while loop
break statement
continue statement
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
LOOPING STRUCTURE
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
FOR LOOP
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
FOR LOOP
#include<stdio.h>
int main()
{
int i;
for(i=1; i<=10; i++)
printf(“%d”, i);
return 0;
}
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
WHILE LOOP
update_expression;
} engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
WHILE LOOP
Flowchart of while
loop
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
WHILE LOOP
#include<stdio.h>
int main()
{
int i = 1;
while(i <= 10)
{
printf(“%d”, i);
i++;
}
return 0;
}
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
DO WHILE LOOP
A do...while loop is similar to the while loop except that the condition is always executed
after the body of a loop so it is an exit-controlled loop
The do-while loop is mainly used in the case where we need to execute the loop at least
once
The do-while loop is mostly used in menu-driven programs where the termination
condition depends upon the end user
Syntax:
initialization expression;
do
{
// statements
update_expression;
} while (test_expression);
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
DO WHILE LOOP
Flowchart of do-while
loop
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
WHILE LOOP
#include<stdio.h>
int main()
{
int i = 1;
do
{
printf(“%d”, i);
i++;
} while(i <= 10);
return 0;
}
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
BREAK STATEMENT
The break is a keyword in C which is used to bring the program control out of
the loop
The break statement is used inside loops or switch statement and it is
associated with if
Syntax:
//loop or switch case
break;
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
BREAK STATEMENT
Example: Find sum of any 5 numbers entered only between 1
to 10
#include<stdio.h>
int main()
{
int n, count = 0, sum=0;
printf(“\n Get sum of any 5 numbers between 1 to 10”);
do
{
printf(“\n Enter a number only between 1 to 10”);
scanf(“%d”, &n);
if(n < 1 && n > 10)
break;
sum = sum + n;
count++;
}while(count <= 5);
return 0;
engineering _munotes
}
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
CONTINUE STATEMENT
The continue statement skips the current iteration of the loop and continues with the
next iteration
When the continue statement is executed in the loop, the code inside the loop following
the continue statement will be skipped and next iteration of the loop will begin.
Syntax:
//loop or switch case
continue;
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
CONTINUE STATEMENT
#include<stdio.h>
int main()
{
int i;
for(i=1; i<=10; i++)
{
if(i == 4)
continue;
printf(“%d”, i);
}
return 0;
} engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
C PROGRAMMING
Module 3
Functions
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
ADVANTAGES OF FUNCTIONS
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
TYPES OF FUNCTIONS
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
TYPES OF FUNCTIONS
Library functions
❖ Library functions are those functions which are already defined in C library
❖ Example: printf(), scanf(), strcat() etc.
❖ You just need to include appropriate header files to use these functions
User-defined functions:
❖ User Defined Functions are those functions which are defined by the user at the time of writing
program
❖ These functions are made for code reusability and for saving time and space
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
HOW FUNCTION WORKS
return_type function_name (argument list)
{
function body;
return_type function_name (argument list); }
Function Function
Declaration Definition
Function Call
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
HOW FUNCTION WORKS
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
HOW FUNCTION WORKS
Actual arguments:
The values that are passed to the called function from the
main function are known as Actual arguments
Formal arguments:
The variables declared in the function prototype or
definition are known as Formal arguments
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
PARAMETER (ARGUMENT) PASSING
void add(int a, int b); // Function Declaration
int main()
Formal
{ Arguments
int n1,n2;
Example: Write a C Program printf("\n Enter two numbers:");
to perform addition of two scanf("%d %d", &n1, &n2);
numbers using function.
add(n1, n2); // Function Call
return 0; Actual
} Arguments
void add(int n1, int n2) // Function Definition
{
Formal
printf("\n Addition of %d and %d = %d", a, b, a+b);
Arguments
}
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
RETURN STATEMENT
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
PARAMETER (ARGUMENT) PASSING
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
CALL BY VALUE
❖ The value of the actual parameters is copied into the formal parameters
❖ The arguments in the function call (Actual arguments) are not modified by the change
in parameters of the called function (Formal arguments)
❖ Different memory is allocated for actual and formal parameters
a b a b
Actual Argument 10 20 10 20 Formal Argument
❖ The address of the variable is passed into the function call as the actual parameter
❖ The value of the actual parameters can be modified by changing the formal parameters since
the address of the actual parameters is passed
❖ Same memory is allocated for both formal parameters and actual parameters
a a
Actual Argument 10 100 Formal Argument
100 200
Normal Variable Pointer that stores the address
of actual argument
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
CALL BY REFERENCE
❖ Recursion is the process of defining a problem or solution of the problem in terms of itself
❖ Recursion is the process in which function calls itself directly or indirectly
❖ Recursive Function:
A function which calls itself is called as a recursive function
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
HOW RECURSION WORKS
int sum(int n)
{
if(n = = 1)
return 1; n=1 sum(1) return 1
else
return n + sum(n-1);
n=2 sum(2) return 3
}
n=3 sum(3) return 6
int main()
{ n=4 sum(4) return 10
int n = 4, result;
result = sum(n); n=4 main() result = 10
printf(“%d”, result);
return 0; Stack
}
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
HOW TO WRITE RECURSIVE FUNCTION
Fact(n) = n * Fact(n-1)
Base Case is the case that solves the problem and helps to stop recursion
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
RECURSIVE FUNCTION CALL
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
MODULE 4
engineering _munotes
WHAT IS AN ARRAY ?
values
char
engineering _munotes
Data is stored in a contiguous memory
ARRAY BASICS
Need of an array
What is an array ?
Array Declaration
Array elements in memory
Accessing Array elements
Array Initialization
Entering Data into array
Accessing data from array engineering _munotes
ARRAY DECLARATION
Arrays are declared like a variable.
Array declaration tells compiler which type of array it is and what is the
size of an array.
Syntax:
data_type array_name[array_size];
Example:
engineering _munotes
ACCESSING ARRAY ELEMENTS
engineering _munotes
ARRAY INITIALIZATION
Initialization of an array at the time of declaration:
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
11 21 33 40 05
engineering _munotes
ACCESSING DATA FROM ARRAY
engineering _munotes
ACCESSING DATA FROM ARRAY
11 21 33 40 5
Iteration Iteration
2 4
i=1 i=3
engineering _munotes
EXAMPLE
int main(){
int a[20];
printf("\n Printing values in array");
int i, n; for(i=0; i<n; i++)
printf("\n How many elements you want {
to enter? ");
printf("\n a[%d] = %d ", i, a[i]);
scanf("%d", &n);
printf("\address of a[%d] = %u", i, &a[i]);
printf("\n Enter values in an array"); }
for(i=0; i<n; i++) return 0;
{ printf("\n Enter value at a[%d] = " }
,i); scanf("%d", &a[i]);
engineering _munotes
}
PASSING ARRAY ELEMENTS TO A FUNCTION
engineering _munotes
PASSING ARRAY ELEMENTS TO A FUNCTION Call by value
#include<stdio.h>
int main() }
{ \\ Function Definition
int i; {
2-dimensional array
engineering _munotes
2- DIMENSIONAL ARRAY
2-dimensional array is called as a matrix with rows and columns
Each element in 2-d array is represented with row and column number
engineering _munotes
2- DIMENSIONAL ARRAY
0 1 2 3
0 11 33 45 23
1 70 15 55 66
2 9 63 12 50
engineering _munotes
2 – D ARRAY DECLARATION
Syntax:
data_type array_name[rows][columns];
0 1 2 3
Example: a[0][0] a[0][1] a[0][2] a[0][3]
0
int a[3][4]; a[1][0] a[1][1] a[1][2] a[1][3]
1
// a is a 2-dimensional array with 3 rows and
a[2][0] a[2][1] a[2][2] a[2][3]
4 columns 2
engineering _munotes
2 – D ARRAY INITIALIZATION
{11,12,13,14},
{21,22,23,24},
{31,32,33,34}
};
engineering _munotes
2 – D ARRAY INITIALIZATION
engineering _munotes
2 – D ARRAY DECLARATION
Valid Declarations:
int a[3][4];
int a[][4];
Invalid Declarations:
int a[][];
int a[3][];
engineering _munotes
ENTERING DATA INTO 2-D ARRAY
engineering _munotes
READING DATA FROM 2-D ARRAY
engineering _munotes
MATRIX OPERATIONS
Matrix Addition
Matrix Subtraction
Matrix Multiplication
Matrix Transpose
engineering _munotes
MATRIX ADDITION
A matrix can only be added to (or subtracted from) another matrix if the two matrices have the
same dimensions
To add two matrices, just add the corresponding entries, and place this sum in the corresponding
position in the matrix which results
engineering _munotes
MATRIX MULTIPLICATION
You can only multiply two matrices if their dimensions are compatible , which means the
number of columns in the first matrix is the same as the number of rows in the second matrix
In matrix multiplication first matrix one row element is multiplied by second matrix all column
elements
engineering _munotes
MATRIX MULTIPLICATION
0 1 2 0 1 2
0 1 2 3 0 24 33 42
1 4 5 6 1 9 12 15
1*3
1*2 +++2*4
4*3
1*1
4*1
4*2 2*6
2*5
5*4
5*5 15
5*6 ==== 9
12
42
24
33
engineering _munotes
MATRIX TRANSPOSE
Transpose of a matrix is obtained by changing rows to columns and columns
to rows
0 1
0 1 2
0 1 4
0 1 2 3
1 2 5
1 4 5 6 engineering _munotes
2 3 6
ARRAY OF POINTERS
engineering _munotes
EXAMPLE
# include<stdio.h>
int main( ) {
int *arr[ 4 ] ; /* array of integer
for ( m = 0 ; m <= 3 ; m++ )
pointers */
printf ( "%d\n", * ( arr[ m ] ) ) ;
int i = 31, j = 5, k = 19, l = 71, m ;
return 0 ;
arr[ 0 ] = &i ;
}
arr[ 1 ] = &j ;
arr[ 2 ] = &k ;
arr[ 3 ] = &l ;
engineering _munotes
STRINGS
What is string ?
Sting Declaration
String Initialization
Accepting String from user
String functions
engineering _munotes
WHAT IS STRING ?
engineering _munotes
STRING DECLARATION
Syntax:
char string_name[size];
Example:
char s[5];
engineering _munotes
, b = 20
STRING INITIALIZATION
Two ways:
1. By Character array
2. By String literal
1. By character array:
2. By string literal:
engineering _munotes
ACCEPTING STRING FROM USER
int main(){
#include <stdio.h>
char name[20];
int main(){
printf("Enter name: ");
char name[20];
scanf("%[^\n]s", name);
printf("Enter name: ");
printf("Your name is %s", name);
scanf("%s", name);
return 0; }
printf("Your name is %s", name);
Here, [^\n] indicates that scanf( ) will keep
return 0; } receiving characters into name[ ] until \n is
engineering _munotes
encountered.
ACCEPTING STRING FROM USER
# include<stdio.h>
scanf( ) is not capable of receiving
int main( ) {
multi-word strings. Therefore,
names such as ‘Debashish Roy’ char name[ 25 ] ;
would be unacceptable. The way to printf ( "Enter your full name: " ) ;
get around this limitation is by
using the function gets( ). The gets ( name ) ;
usage of functions gets( ) and its puts ( "Hello!" ) ;
counterpart puts( ) is shown below.
puts ( name ) ;
return 0 ;
engineering _munotes
}
PRINTING A STRING
# include<stdio.h>
int main( ) {
char name[ ] = “engineering" ;
int i = 0 ;
while ( i <= 10)
{
printf ( "%c", name[ i ] ) ;
i++ ;
}
printf ( "\n" ) ;
return 0 ; engineering _munotes
}
PRINTING A STRING
# include <stdio.h>
int main( )
{
printf ( "\n" ) ;
char name[ ] = "Klinsman" ;
return 0 ;
int i = 0 ;
}
while ( name[ i ] != '\0' )
{
printf ( "%c", name[ i ] ) ;
i++ ;
}
engineering _munotes
PRINTING A STRING
# include <stdio.h>
int main( )
{
char name[ ] = "Klinsman" ; printf ( "\n" ) ;
char *ptr ; return 0 ;
ptr = name ; /* store base address of string }
*/
while ( *ptr != '\0' )
{
printf ( "%c", *ptr ) ;
ptr++ ; engineering _munotes
}
STRING FUNCTIONS
String library functions:
o/p:
String Library functions are
Before swapping the values in main a=10,b=20
available in string.h header
After swapping values in function a=20,b=10 file
After swapping values in main a =20,b=10
To use string library
functions in a program you
need to include string.h
header file
#include <stdio.h>
len = strlen(s);
#include <stdio.h>
int main(){
int i = 0;
while(s[i] != '\0')
i++;
char d[20];
Syntax:
strcpy(d, s);
strcpy(destination, source);
printf("Source string s = %s", s);
#include <stdio.h>
int main(){
char s[20] = “college”;
printf(“Source string s = %s”, s);
char d[20];
printf(“Destination string d = %s”, d);
int i = 0;
return 0;
while(s[i] != ‘\0’)
}
{
d[i] = s[i];
i++;
} engineering _munotes
strcmp()
Compares two strings to check #include<string.h>
whether they are same or different int main(){
If the two strings are equal it char s[20] = “college”;
returns 0
if(strcmp(s, “college”) == 0)
Else it returns ASCII value
printf(“Strings are equal);
difference
else
Syntax:
printf(“Strings are different”);
strcmp(string1, string2);
return
engineering 0;}
_munotes
COMPARE TWO STRINGS WITHOUT USING strcmp() FUNCTION
else{
int main(){ cmp = 1;
char s[20] = “college”; break; }
char d[20] = “terna”; }
int i = 0; if(cmp == 0)
while(s[i] != ‘\0’ || d[i] != ‘\0’)
printf(“Strings are equal);
{
if(d[i] == s[i] { else
cmp = 0; printf(“Strings are different”);
i++;}
return 0;
engineering _munotes
}
strcat()
#include<string.h>
Concatenates two strings
int main(){
i.e. concatenate source string at the end
char s[20] = "college";
of destination string
char d[20] = "terna ";
Syntax:
strcat(d, s);
strcat(destination, source);
printf("Source string s = %s", s);
return 0;
engineering _munotes
}
CONCATENATE TWO STRINGS WITHOUT USING strcat() FUNCTION
int main(){
char s[20] = “college”;
char d[20] = “terna”;
int i = 0, j = 0; printf(“Source string s = %s”, s);
i++; return 0;
while(s[j] != ‘\0’) { }
d[i] = s[j];
i++;
j++; engineering _munotes
}
strlwr and strupr
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string.h>
int main()
int main()
{
{
char string[] = "strupr in c";
char string[] = "Strlwr in C";
printf("%s\n",strupr(string));
printf("%s\n",strlwr(string));
return 0;
return 0;
}
engineering _munotes
}
C PROGRAMMING
Module 5
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
Structure
Introduction of structure
Structure Declaration
Structure Variable
Structure Example
Typedef
Array of structures
Nested Structure
Problem Statement:
Store students information like roll no., name, department, college, marks
Roll No. – int
Name – char[10]
Department – char[5]
Marks – float
Defining a structure:
Syntax Example
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
STRUCTURE VARIABLE
Structure is user defined data type, variables of that type can be declared
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
STRUCTURE VARIABLE INITIALIZATION
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
STRUCTURE ELEMENTS IN
MEMORY
struct student
{
int roll_no;
char student_name[10];
char dept[5];
float marks;
};
struct student s1={10, ”Neha”, “IT”, 90.50};
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
STRUCTURE EXAMPLE
Problem statement: Define structure student with members roll_no, student_name,
dept, marks and accept student record from user and display student record.
struct student
{
int roll_no;
char student_name[10];
char dept[5];
float marks;
};
struct student s1={10, ”Neha”, “IT”, 90.50};
int main()
{
struct student s2;
printf(“\n Enter s2 information:”);
scanf(“%d %s %s %f”, &s2.roll_no, s2.student_name, s2.dept, &s2.marks);
printf(“\n Displaying s1 record: %d %s %s %.2f”, s1.roll_no, s1.student_name, s1.dept,s1.marks);
printf(“\n Displaying s2 record: %d %s %s %.2f”, s2.roll_no, s2.student_name, s2.dept,s2.marks);
return 0;
} engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
TYPEDEF
typedef is a keyword used to assign alternative names to existing datatypes
It is mostly used with user defined data types, when names of the data types become complicated to use in programs
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
TYPEDEF
Problem statement: Define structure student with members roll_no, student_name,
dept, marks and accept student record from user and display student record.
typedef struct student
{
int roll_no;
char student_name[10];
char dept[5];
float marks;
}stud;
stud s1={10, ”Neha”, “IT”, 90.50};
int main()
{
stud s2;
printf(“\n Enter s2 information:”);
scanf(“%d %s %s %f”, &s2.roll_no, s2.student_name, s2.dept, &s2.marks);
printf(“\n Displaying s1 record: %d %s %s %.2f”, s1.roll_no, s1.student_name, s1.dept,s1.marks);
printf(“\n Displaying s2 record: %d %s %s %.2f”, s2.roll_no, s2.student_name, s2.dept,s2.marks);
return 0;
}
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
ARRAY OF
STRUCTURES
Problem Statement: Store information of 60 students and print it.
Solution: Use Array of Structure
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
ARRAY OF
STRUCTURES
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
ARRAY OF
STRUCTURES
typedef struct student
{
int roll_no;
char student_name[10];
char dept[5];
float marks;
}stud;
stud s[5];
int main()
{
int i;
printf(“\n Enter Student information:”);
for(i=0; i<5; i++)
{
printf(“\n Enter values of s[%d] record: ”, i+1);
scanf(“%d %s %s %f”, &s[i].roll_no, s[i].student_name, s[i].dept, &s[i].marks);
printf(“\n Displaying s[%d] record: %d %s %s %.2f”, i+1, s[i].roll_no, s[i].student_name, s[i].dept, s[i].marks);
}
return 0;
}
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
NESTED STRUCTURES
struct address
{
char city[10];
int pin;
};
struct student
{
int roll_no;
char student_name[10];
char dept[10];
float marks;
struct address add;
};
struct student s1 = {10, “Neha”, “IT”, 90.50, “Mumbai”, 400011};
struct student
{
int roll_no;
char student_name[10];
char dept[10];
float marks;
struct address add;
};
struct student s1 = {10, “Neha”, “IT”, 90.50, “Mumbai”, 400011}
int main()
{
printf(“\n Displaying s1 record: ”);
printf(“\n %d %s %s %f %s %d”, s1.roll_no, s1.student_name, s1.dept, s1.marks, s1.add.city, s1.add.pin);
return 0;
}
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
PASSING STRUCTURE TO
FUNCTION
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
PASSING INDIVIDUAL MEMBER TO A
FUNCTION
#include<stdio.h> int main()
typedef struct student {
{ stud s1 = {11, “Mohit”, 98.7};
int rollno; printf("\nDisplay student information\n");
char name[20]; display(s1.rollno, s1.name, s1.marks); return 0;
float marks; }
}stud; void display(int r, char n[20], float m)
{
printf("%d %s %f ",r, n, m);
}
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
PASSING ENTIRE STRUCTURE TO A
FUNCTION
#include<stdio.h> int main()
typedef struct student {
{ stud s1 = {11, “Mohit”, 98.7};
int rollno; printf("\nDisplay student information\n");
char name[20]; display(s1);
float marks; return 0;
}stud; }
void display(stud s)
{
printf("%d %s %f ",s.rollno, s.name, s.marks);
}
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
PASSING ENTIRE STRUCTURE TO A
FUNCTION
Structure and Pointer
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
PASSING ENTIRE STRUCTURE TO A
FUNCTION
Structure and Pointer
int main()
#include<stdio.h>
{
typedef struct student
stud s1 = {11, “Mohit”, 98.7};
{ printf("\nDisplay student information\n");
int rollno; display(&s1);
char name[20]; return 0;
}
float marks;
void display(stud *ptr)
}stud;
{
printf("%d %s %f ",ptr->rollno, ptr->name, ptr->marks);
}
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
ASSIGNING VALUE OF ONE STRUCTURE TO
ANOTHER
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
Union
Introduction of Union
Union variable
Memory allocation
Union example
Structure vs Union
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
INTRODUCTION OF
UNION
Union is a user-defined data type which can hold collection
of different type of data in the same memory location
Defining a union:
union keyword is used for defining a union having members of different type
union union_name union student
{ {
union element 1; int roll_no;
union element 2; char student_name[10];
union element 3; char dept[10];
. float marks;
. };
.
};
Syntax Example
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
UNION VARIABLE
Union is user defined data type, variables of that type can be declared
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
ACCESSING UNION MEMBERS
union student Union elements can be accessed using dot operator (Using Variable s1)
{
int roll_no; Syntax: unionVariable.unionElement
char student_name[10];
Example:
char dept[5];
float marks; s1.roll_no
};
s1.dept
union student s1, *s2;
Union elements can be accessed using arrow operator
(Using Pointer variable *s2)
Syntax: unionVariable->unionElement
Example:
s2->roll_no
s2->dept
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
MEMORY ALLOCATION
Memory is not allocated while defining a union but it is allocated while declaring a variable of union
All union elements share same memory
Memory equal to the size of largest element is allotted to union
Union can contain different types of elements but not all elements can be initialized at a time
Only one element can be accessed at a time
union student
{ student_name = 10 bytes
int roll_no;
char student_name[10];
char dept[5];
float marks; 101 110
};
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
UNION EXAMPE
union student
{
int roll_no;
char student_name[10]; Invalid Initialization:
};
union student s1={10, “Neha”};
int main()
{
union student s2;
printf(“\n Enter rollno:”);
scanf(“%d”, &s2.roll_no);
printf(“%d”, s2.roll_no);
printf(“\n Enter name”);
scanf(“%s”, s2.student_name);
printf(“%s”, s2.student_name);
return 0;
}
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
STRCTURE VS UNION
struct student
{
char name[40];
int roll_no;
int phone_number
};
union student
{
char name[40];
int roll_no;
int phone_number
};
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©
STRCTURE VS UNION
engineering _munotes
PRPEARED BY: PROF. SAYLEE NARKHEDE ©