0% found this document useful (0 votes)
4 views

Standard Library Functions in C

The document provides an overview of standard library functions in C, categorized into input/output, string handling, mathematical functions, memory allocation, and file handling. It includes examples of function usage, syntax, and multiple-choice questions to test understanding. Additionally, it highlights a simple calculator project that demonstrates the application of these functions.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Standard Library Functions in C

The document provides an overview of standard library functions in C, categorized into input/output, string handling, mathematical functions, memory allocation, and file handling. It includes examples of function usage, syntax, and multiple-choice questions to test understanding. Additionally, it highlights a simple calculator project that demonstrates the application of these functions.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Topic - Standard

Library Functions in C
Introduction

Standard library functions in C are


predefined functions provided by C
libraries. These functions help in
performing input/output operations,
string handling, mathematical
computations, memory management, and
more.
Types of Standard Library
Functions

A) Input/Output Functions (stdio.h)


These functions are used to perform input and output
operations.
(i) printf() – Prints formatted output to the console.
(ii) scanf() – Reads formatted input from the user.
(iii) getchar() – Reads a single character from standard
input.
(iv) putchar() – Writes a single character to standard
output.
B) String Handling
Functions (string.h)
These functions help manipulate strings.

(i) strlen() – Returns the length of a string.


(ii) strcpy() – Copies one string to another.
(iii) strcat() – Concatenates two strings.
(iv) strcmp() – Compares two strings.
C) Mathematical Functions
(math.h)
These functions are used for mathematical
calculations.

(i) sqrt(x) – Returns the square root of x.


(ii) pow(x, y) – Returns x raised to the power y.
(iii) abs(x) – Returns the absolute value of x.
D) Memory Allocation
Functions (stdlib.h)
These functions manage dynamic memory.

(i) malloc() – Allocates memory dynamically.


(ii) calloc() – Allocates memory and initializes
it to zero.
(iii) free() – Frees allocated memory.
E) File Handling Functions
(stdio.h)
These functions allow working with files.

(i) fopen() – Opens a file.


(ii) fclose() – Closes a file.
(iii)fgetc() – Reads a character from a file.
(iv) fputc() – Writes a character to a file.
Syntax of Standard Library
Functions
• Standard library functions in C are included using header
files.
• #include <header_file>
• return_type function_name(arguments);
• Example :
• #include <stdio.h>
• int main() {
• printf("Hello, World!");
• return 0;
•}
Program 1: String Length

• #include <stdio.h>
• #include <string.h>
• int main() {
• char str[] = "Hello";
• printf("Length: %d", strlen(str));
• return 0;
•}
Program 2: Absolute Value

• #include <stdio.h>
• #include <stdlib.h>
• int main() {
• int num = -10;
• printf("Absolute Value: %d", abs(num));
• return 0;
•}
Program 3: Square Root

• #include <stdio.h>
• #include <math.h>
• int main() {
• double num = 25.0;
• printf("Square Root: %lf", sqrt(num));
• return 0;
•}
MCQ Questions
Which library function is used to find the length of a string?
a) Strlen
b) Sizeof
c) Strcmp
d) Strcat

Which header file is required for mathematical functions?


e) math.h
f) stdio.h
g) string.h
h) stdlib.h
What does the abs() function return?
a) Absolute value
b) Negative value
c) Remainder
d) None of the above

Which function is used to copy one string into another?


e) Strcat
f) strcpy
g) Strcmp
h) strlen
What is the function of calloc()?
a) Allocates memory
b) Allocates memory without initialization
c) Allocates memory and initializes it to zero
d) None of the above

Which function is used to open a file in C?


e) fopen()
f) open()
g) file()
h) startfile()
What does sprintf() do?
a) Prints output to the console
b) Writes formatted data into a string
c) Reads input from the user
d) None of the above

Which function is used to compare two strings?


e) strcmp()
f) strcpy()
g) strcat()
h) strrev()
Which function is used to convert a string into an integer?
a) itoa()
b) atof()
c) atoi()
d) strint()

What is the purpose of malloc()?


e) Allocates memory without initialization
f) Allocates memory and initializes it to zero
g) Deallocates memory
h) Copies memory
Which function is used to read a character from a file?
a) fgetc()
b) getchar()
c) getc()
d) fread()

What does free() do?


e) Allocates memory
f) Deallocates memory
g) Copies memory
h) Reads a file
Which function is used to tokenize a string in C?
a) strtok()
b) strsep()
c) strsplit()
d) strstr()

What is the purpose of fprintf()?


e) Prints data to the console
f) Reads formatted data from a file
g) Prints formatted data to a file
h) Reads data from a file
Which function is used to search for a character in a string?
a) strchr()
b) strstr()
c) strfind()
d) strsearch()
Solutions
1) a) strlen 9) c) atoi()
2) a) math.h 10) a) Allocates memory
3) a)Absolute value without initialization
4) b) strcpy 11) a) fgetc()
12) b) Deallocates memory
5) c)Allocates memory and
initializes it to zero 13) a) strtok()
6) a) fopen() 14) c) Prints formatted data to
a file
7) b) Writes formatted data
into a string 15) a) strchr()
8) a) strcmp()
Running Project

• This Simple Calculator Project


demonstrates the use of Standard Library
Functions in C for handling input/output,
mathematical operations, and dynamic
memory allocation.
• It is a great example of how built-in
functions can simplify coding in C.

You might also like