Lecture 2.3.1 Function
Lecture 2.3.1 Function
CONTENT Types
Predefined library
functions
Built-in-string
functions
Examples
1
A function is a block of code that performs a
specific task.
2
There are two types of function in C programming:
Types of
Function
Example: If you want to use the printf function, the file header
Predefined <stdio.h> should be included.
Library #include <stdio.h>
Functions int main()
{
printf("Hello");
}
If you try to use printf without including the stdio.h header file,
you will get an error.
5
1. They are simple because they work
One of the most important reasons you should use library
functions is simply because they work. These functions have
gone through multiple rigorous testing and are easy to use.
2. The functions are optimized for performance
Since, the functions are "standard library" functions, a
dedicated group of developers constantly make them better.
Advantages of In the process, they are able to create the most efficient code
optimized for maximum performance.
using C library 3. It saves considerable development time
Function Since the general functions like printing to a screen,
calculating the square root, and many more are already
written. You shouldn't worry about creating them once again.
4. The functions are portable
With ever-changing real-world needs, your application is
expected to work every time, everywhere. And, these library
functions help you in that they do the same thing on every
computer.
6
Example: Write a program in C to find square root of a number.
You can compute the square root of a number, you can use the
sqrt() library function. The function is defined in
the <math.h> header file
#include <stdio.h>
Example of #include <math.h>
int main()
Library {
Function float num, root;
printf("Enter a number: ");
scanf("%f", &num);
root = sqrt(num);
printf("Square root of %.2f = %.2f", num, root);
return 0;
}
7
<assert.h> Program assertion functions
if( strcmp(a,b) == 0 )
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
return 0;
}
9
Function Syntax (or) Example Description
strcpy() strcpy(string1, string2) Copies string2 value into string1
10
Function Syntax (or) Example Description
strrev() strrev(string1) It reverses the value of string1
strchr() strchr(string1, 'b') Returns a pointer to the first occurrence of
character 'b' in string1
strrchr() 'strrchr(string1, 'b') Returns a pointer to the last occurrence of
character 'b' in string1
strstr() strstr(string1, string2) Returns a pointer to the first occurrence of string2
in string1
String
strset() strset(string1, 'B') Sets all the characters of string1 to given character
Handling 'B'.
11
SUMMARY…..
12
PROGRAMS
ASKED
QUESTIONS
13
1. Which standard library function will you use to find the last occurance of a
character in a string in C?
A. strnchar()
B. strchar()
C. strrchar()
UTILISE YOUR D. strrchr()
KNOWLEDGE TO 2. Input/output function prototypes and macros are defined in which header
ANSWER file?
A. conio.h
B. stdlib.h
Let us see how much you have
C. stdio.h
learned from the lecture and how
D. dos.h
effectively you can apply your
knowledge…!!
3.What is stderr ?
A. standard error
B. standard error types
C. standard error streams
D. standard error definitions
14
4. What will be the output of the following C code?
void main()
div_t res;
res = div(34, 4);
UTILISE YOUR printf("quotient part = %d\n", res.quot);
printf("remainder part = %d\n", res.rem);
KNOWLEDGE TO }
ANSWER a) quotient part=0
Let us see how much you have remainder part=4
learned from the lecture and how
effectively you can apply your b) quotient part=8
knowledge…!! remainder part=2
c) quotient part=4
remainder part=0
d) quotient part=2
remainder part=8
15
• Video lectures:
• https://nptel.ac.in/courses/106/101/106101208/
• https://spokentutorial.org/watch/C%2Band%2BCpp/S
tring%2BLibrary%2BFunctions/Manipuri/
• REFERENCES
• Weblinks:
• https://www.tutorialspoint.com/cprogramming/c_fun
ctions.htm Vedio Lectures
• https://beginnersbook.com/2014/01/c-strings-string-f
unctions/
• https://beginnersbook.com/2014/01/c-functions-exa Weblinks
mples/
•
• 16
•
•
THANK YOU….
17