Pre-defined or Library Functions and User-Defined Functions in C

In this class, we will understand Pre-defined or Library Functions and User-Defined Functions in C.

For Complete YouTube Video: Click Here

We have already discussed how to declare and define functions.

Pre-defined or Library Functions and User-Defined Functions in C

Pre-defined or Library Functions

Example 1

Pre-defined functions are already defined in C Compiler system libraries. The definitions of the pre-defined functions are written in header files. Ex: printf(), scanf(), sqrt(), strlen() etc.

The image below is an example of pre-defined functions.

Pre-defined or Library Functions and User Defined Functions in C 1
Pre-defined or Library Functions and User-Defined Functions in C 1

So far, we have used printf() and scanf() functions, but we have never discussed where those functions are defined.

The definitions of printf and scanf functions are in the stdio header file.

Now, we will closely observe the first line of code #include.

In the above code, “include” is a pre-processor directive, and “stdio” is a standard input-output header file.

The pre-processor directive directs the compiler to include the corresponding header file.

A header file is a file where a compiler programmer will write all the pre-defined functions.

Coming back to our program printf() function is assigned to an integer variable, as shown below.

i = printf(“Hello World!\n”);

What will the printf() and scanf() function return?

So far, we haven’t discussed anything about the return values of the printf(), and scanf() functions.

The printf() function will return the number of characters in the string literal.

In the above example, the printf() function will return 13 as there are 13 characters in the string and will get assigned to i.

Similarly, the scanf() function will return the number of successfully scanned elements.

Example 2

The image below is the second example of pre-defined functions.

Pre-defined or Library Functions and User Defined Functions in C 2
Pre-defined or Library Functions and User-Defined Functions in C 2

This example is the same as that of the previous model.

In this example, the printf() function will return 21, as the characters in the printf() are 21.

Example 3

The image below is the third example of pre-defined functions.

Pre-defined or Library Functions and User Defined Functions in C 3
Pre-defined or Library Functions and User-Defined Functions in C 3

In the above example, “i” is assigned the scanf() function as shown below.

The scanf() function will return the number of successfully scanned elements.

In our case, we have scanned two elements, and two will get assigned to I.

User-defined Functions

User defined functions are defined by the user. Ex: abc(), sum() etc.

The concepts of user-defined functions has been discussed in our previous classes.