Types of Functions in C: Library and User-Defined Functions
Complete guide to library functions (printf, scanf, fgets, puts) and user-defined functions with examples
Last Updated: September 6, 2026
About the Author
Sankalan Data Technologies Team
Programming and Data Technology Tutorials
This tutorial was created and reviewed by the Sankalan Data Technologies training team, with experience teaching C, SQL, Python and other programming technologies to students and working professionals. Content is written and reviewed by experienced trainers and developers.
Quick Answer: Types of Functions in C
C functions are commonly divided into two categories:
- 1. Library functions – predefined functions provided by C libraries, such as
printf(),scanf(),strlen(), andfopen(). - 2. User-defined functions – functions created by the programmer for a specific task.
User-defined functions are commonly classified into four types based on arguments and return values.
What You'll Learn in This Tutorial
- What Is a Function in C?
- Types of Functions in C
- Library Functions
- Library Functions by Header File
- Library Function Examples
- Library vs User-Defined Functions
- User-Defined Functions
- Four Types of User-Defined Functions
- Function Prototype
- Practice Questions
What Is a Function in C?
In C, a function is a reusable block of code designed to perform a specific task. Functions provide reusability and modularity, making programs easier to maintain and debug.
C has two main categories of functions: library functions and user-defined functions.
Library functions such as printf(), scanf(), and strlen() are provided
by the C standard libraries, while user-defined functions are written by programmers for specific tasks.
Types of Functions in C
Types of functions in C: library functions and user-defined functions
Note: The diagram above demonstrates some inbuilt functions but not the complete list of inbuilt functions available in C.
Functions in C - Hierarchy
Functions in C
│
├── Library Functions
│ ├── printf() - Output
│ ├── scanf() - Input
│ ├── strlen() - String length
│ ├── fgets() - Safe input
│ └── fopen() - File operations
│
└── User-Defined Functions
│
├── No Arguments + No Return Value
├── Arguments + No Return Value
├── No Arguments + Return Value
└── Arguments + Return Value
Library Functions (Inbuilt Functions)
What are Library Functions?
Library functions are predefined functions that are part of C libraries. They can be used by including the appropriate header files. These functions provide a wide range of functionalities including string manipulation, input/output operations, mathematical calculations, file handling, and memory management.
Common examples: printf(), scanf(), fgets(), puts(), strlen(), strcpy(), strcmp(), getc(), putc()
Library Functions by Header File
| Header File | Example Functions | Purpose |
|---|---|---|
<stdio.h> |
printf(), scanf(), fopen(), fgets() |
Input/output operations |
<string.h> |
strlen(), strcpy(), strcmp() |
String handling |
<math.h> |
sqrt(), pow(), ceil() |
Mathematical operations |
<stdlib.h> |
malloc(), free(), atoi() |
Memory management, utilities |
<ctype.h> |
toupper(), tolower(), isdigit() |
Character handling |
Library Function Examples
a. printf() Function
printf() is part of the standard input/output library (stdio.h) and is commonly used for printing formatted text on the output console.
Syntax:
printf("format string", argument1, argument2, ...);
The "format string" contains plain text and format specifiers (placeholders for values to be printed). Format specifiers start with a percent sign (%):
%dor%i- for integers%f- for floating-point numbers%c- for characters%s- for strings
b. scanf() Function
scanf() is the standard input function in C. It reads data from the standard input stream (keyboard) and writes the result into the given arguments.
Syntax:
int scanf( const char *format, ... );
C Program to Demonstrate printf() and scanf()
#include <stdio.h>
int main() {
int a;
printf("Enter any number: ");
scanf("%d", &a);
printf("The number is: %d\n", a);
return 0;
}
Output:
Enter any number: 5
The number is: 5
c. fgets() and puts() Functions
fgets() safely reads a line of text from standard input and stores it in a character array.
It allows specifying the maximum number of characters to read, preventing buffer overflow.
puts() outputs a string to the standard output followed by a newline character.
C Program to Demonstrate fgets() and puts()
#include <stdio.h>
#include <string.h>
int main() {
char city[50];
printf("Enter your city name: ");
fgets(city, sizeof(city), stdin);
// Remove trailing newline character
city[strcspn(city, "\n")] = '\0';
printf("Welcome to ");
puts(city);
return 0;
}
Output:
Enter your city name: Pune
Welcome to Pune
Explanation: fgets() reads the entire line including spaces and stores it in city. puts() displays it with a newline.
⚠️ Important: gets() is an obsolete and unsafe function and should not be used in new C programs. It was removed from the C11 standard because it does not provide a way to limit input size. Use fgets() instead.
d. getc() and putc() Functions
getc() reads a single character from a specific input stream. putc() writes a character to a specified output stream.
C Program to Demonstrate getc() and putc()
#include <stdio.h>
int main() {
int ch;
FILE *fptr;
fptr = fopen("poem.c", "r");
if (fptr != NULL) {
while ((ch = getc(fptr)) != EOF) {
putc(ch, stdout);
}
fclose(fptr);
return 0;
}
return 1;
}
Explanation: This program reads a file character by character using getc() and displays it using putc(). Note that ch is declared as int, not char, to properly handle EOF.
Library Function vs User-Defined Function
Example 1: Library Function
printf("Hello, World!");
printf() is a library function. You didn't write it - it's provided by the C standard library.
Example 2: User-Defined Function
void greet() {
printf("Hello, World!");
}
greet() is a user-defined function. You wrote it yourself to perform a specific task.
Common Confusion
A common mistake beginners make is thinking that every function used in a C program is user-defined.
Functions such as printf() and scanf() are already provided by the C library.
When we create greet(), addition(), or calculateSalary(),
those are user-defined functions.
User-Defined Functions
What are User-Defined Functions?
User-defined functions are functions created by programmers to perform specific operations or tasks. These functions are not available in C libraries but are defined by the user to meet specific requirements.
Four Types of User-Defined Functions
User-defined functions are commonly classified into four types based on whether they accept arguments and whether they return a value:
| Arguments | Return Value | Type | Use Case |
|---|---|---|---|
| No | No (void) | No arguments, no return | Simple tasks, no input needed |
| Yes | No (void) | Arguments, no return | When you need input but no output |
| No | Yes | No arguments, return value | When you need output but no input |
| Yes | Yes | Arguments, return value | When you need both input and output |
1. No Arguments, No Return Value (void)
These functions do not take input and do not return a value. The void keyword indicates no return value.
Example: No Arguments, No Return
#include <stdio.h>
void addition() {
int x = 9, y = 8;
int sum = x + y;
printf("%d + %d = %d\n", x, y, sum);
}
int main() {
addition();
return 0;
}
Output:
9 + 8 = 17
2. Arguments, No Return Value (void)
These functions take arguments but do not return a value.
Example: Arguments, No Return
#include <stdio.h>
void addition(int x, int y) {
int sum = x + y;
printf("The sum is: %d\n", sum);
}
int main() {
int x = 5, y = 3;
addition(x, y);
return 0;
}
Output:
The sum is: 8
3. No Arguments, Returns Value
These functions do not take input but return a value.
Example: No Arguments, Returns Value
#include <stdio.h>
int addition() {
int x, y, sum;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
sum = x + y;
return sum;
}
int main() {
printf("Sum = %d\n", addition());
return 0;
}
Output:
Enter two numbers: 5 3
Sum = 8
4. Arguments, Returns Value
These functions take input and return a value. The return type specifies the type of value returned.
Example: Arguments, Returns Value
#include <stdio.h>
int addition(int x, int y) {
int sum = x + y;
return sum;
}
int main() {
int x = 5, y = 3;
printf("%d + %d = %d\n", x, y, addition(x, y));
return 0;
}
Output:
5 + 3 = 8
Function Prototype in C
A function prototype declares the function's name, return type, and parameters before the function is called. It tells the compiler what to expect and allows the function to be called before it is defined.
Example: Function Prototype
#include <stdio.h>
// Function prototype
int addition(int, int);
int main() {
printf("%d", addition(5, 3));
return 0;
}
// Function definition
int addition(int x, int y) {
return x + y;
}
Output:
8
Explanation: The prototype int addition(int, int); tells the compiler about the function before it's called in main().
Common Mistakes to Avoid
- Forgetting to include header files - Use
#include <stdio.h>for printf, scanf, etc. - Mismatched format specifiers - Using
%dfor float or%ffor integer. - Missing & in scanf -
scanf("%d", variable)is wrong; should bescanf("%d", &variable). - Using gets() unsafely - Prefer
fgets()to avoid buffer overflow. - Incorrect function prototype - Declare functions before using them.
- Return type mismatch - Returning a float from a function declared as int.
Practice Questions
Try these exercises to reinforce your understanding:
- Write a program using printf() and scanf() to read and display a student's name and marks.
- Write a user-defined function that takes two integers as arguments and returns their product.
- Write a function without arguments that takes input from the user and returns the sum of three numbers.
- Write a program to demonstrate the difference between gets() and fgets().
- Write a function that takes an array and its size as arguments and returns the sum of all elements.
- Write a program to demonstrate all four types of user-defined functions.
Hint: Use printf() for output, scanf() for input, and proper function declarations. Remember to use & with scanf for non-array variables.
Why Understanding Function Types Matters
- Foundation Concept: Functions are fundamental to modular programming in C.
- Exam Relevance: Function types are frequently asked in C programming exams and interviews.
- Real-World Use: Almost every C program uses both library and user-defined functions.
- Code Reusability: Understanding functions helps in writing reusable, maintainable code.
Frequently Asked Questions
What are the types of functions in C?
C programming supports two main categories of functions: 1) Library functions - predefined functions like printf, scanf, strlen, fopen provided by C libraries. 2) User-defined functions - created by programmers to perform specific tasks.
What is the difference between library and user-defined functions in C?
Library functions are predefined in C libraries and can be used by including header files. User-defined functions are created by programmers to meet specific requirements and are not available in standard libraries.
What are the four types of user-defined functions in C?
The four types based on arguments and return value are: 1) No arguments, no return value; 2) Arguments, no return value; 3) No arguments, return value; 4) Arguments, return value.
Why is gets() unsafe in C?
gets() was removed from the C11 standard because it doesn't perform bounds checking and can cause buffer overflow vulnerabilities. Use fgets() instead which allows limiting input size.
What is the difference between printf() and scanf() in C?
printf() is a library function used for formatted output (printing to console). scanf() is used for formatted input (reading from keyboard). Both are defined in stdio.h header file.
What is the difference between gets() and fgets() in C?
gets() is unsafe and deprecated because it doesn't perform bounds checking and can cause buffer overflow. fgets() is safer as it allows specifying the maximum number of characters to read.
Related Tutorials
Previous Topic: Function Declaration in C | Next Topic: Nesting of Functions in C