C Library Functions: A Fun, Free and Easy Tutorial

Hello there, fellow coder! Today, we’re diving into the world of C Library Functions. These functions are like the secret sauce in your programming recipe, making your code tastier (read: more efficient and easier to manage). So, let’s get cooking!

What are C Library Functions?

C Library Functions are like your programming cheat codes. They are pre-defined functions in C programming that are stored in a library. These functions help you perform various operations such as input/output processing, string handling, mathematical computations, and more, without having to write the code from scratch.

Examples of C Library Functions

Let’s take a look at a few examples of C Library Functions and see them in action:

1. printf() library function

This function is like your program’s mouth. It helps your program talk to the user by printing output on the screen.

   #include<stdio.h>
   int main() {
      printf("Hello, World!");
      return 0;
   }
C

Output: Hello, World!

Here, we’re using the printf() function to make our program say “Hello, World!”.

2. sqrt() library function

The sqrt() function is your program’s math whiz. It calculates the square root of a given number.

   #include<stdio.h>
   #include<math.h>
   int main() {
      double num = 9.0, squareRoot;
      squareRoot = sqrt(num);
      printf("Square root of %.2lf = %.2lf", num, squareRoot);
      return 0;
   }
C

Output: Square root of 9.00 = 3.00

In this example, we’re using the sqrt() function to find the square root of the number 9.

3. strlen() library function

The strlen() function is your program’s string measurer. It calculates the length of a string.

   #include<stdio.h>
   #include<string.h>
   int main() {
      char str[] = "Hello, World!";
      printf("Length of string = %d", strlen(str));
      return 0;
   }
C

Output: Length of string = 13

Here, we’re using the strlen() function to find the length of the string “Hello, World!”.

4. scanf() library function

The scanf() library function is your program’s ears. It listens to the user’s input.

   #include<stdio.h>
   int main() {
      int num;
      printf("Enter a number: ");
      scanf("%d", &num);
      printf("You entered: %d", num);
      return 0;
   }
C

Output: Depends on the number entered by the user.

In this example, we’re using the scanf() function to read an integer input from the user.

5. strcat()

The strcat() library function is your program’s string joiner. It concatenates (joins) two strings together.

   #include<stdio.h>
   #include<string.h>
   int main() {
      char str1[50] = "Hello, ";
      char str2[50] = "World!";
      strcat(str1, str2);
      printf("%s", str1);
      return 0;
   }
C

Output: Hello, World!

Here, we’re using the strcat() function to join the strings “Hello, ” and “World!” together.

6. strcmp() library function

The strcmp() library function is your program’s string comparer. It compares two strings and tells you if they are the same or not.

   #include<stdio.h>
   #include<string.h>
   int main() {
      char str1[50] = "Hello";
      char str2[50] = "World";
      int result = strcmp(str1, str2);
      printf("Result: %d", result);
      return 0;
   }
C

Output: Result: -15 (The output will vary depending on the strings compared)

In this example, we’re using the strcmp() function to compare the strings “Hello” and “World”. The function returns a negative value if the first string is less than the second, zero if they are equal, and a positive value if the first string is greater than the second.

7. strcpy()library function

This strcpy() function is your program’s string copier. It copies one string into another.

   #include<stdio.h>
   #include<string.h>
   int main() {
      char str1[50] = "Hello, World!";
      char str2[50];
      strcpy(str2, str1);
      printf("Copied string: %s", str2);
      return 0;
   }
C

Output: Copied string: Hello, World!

Here, we’re using the strcpy() function to copy the string “Hello, World!” into another string.

8. toupper() library function

  1. toupper(): This function is like your program’s personal trainer. It helps your program pump up lowercase characters to uppercase.
   #include<stdio.h>
   #include<ctype.h>
   int main() {
      char ch = 'a';
      ch = toupper(ch);
      printf("%c", ch);
      return 0;
   }
C

Output: A

In this example, we’re using the toupper() function to convert the lowercase character ‘a’ to uppercase ‘A’.

9. tolower()library function

This tolower() library function is your program’s chill pill. It helps your program calm down uppercase characters to lowercase.

   #include<stdio.h>
   #include<ctype.h>
   int main() {
      char ch = 'A';
      ch = tolower(ch);
      printf("%c", ch);
      return 0;
   }
C

Output: a

Here, we’re using the tolower() function to convert the uppercase character ‘A’ to lowercase ‘a’.

10. abs()library function

The abs() library function is your program’s reality check. It returns the absolute (positive) value of an integer.

#include<stdio.h>
#include<stdlib.h>
int main() {
   int num = -10;
   printf("Absolute value of %d = %d", num, abs(num));
   return 0;
}
C

Output: Absolute value of -10 = 10

In this example, we’re using the abs() function to find the absolute value of the number -10.

11. floor()

This floor() function is your program’s down-to-earth friend. It rounds down a floating-point number to the nearest whole number.

#include<stdio.h>
#include<math.h>
int main() {
   double num = 13.65;
   printf("Floor of %.2lf = %.2lf", num, floor(num));
   return 0;
}
C

Output: Floor of 13.65 = 13.00

In this example, we’re using the floor() function to round down the number 13.65 to the nearest whole number, which is 13.

12.ceil()

This ceil() function is your program’s ambitious buddy. It rounds up a floating-point number to the nearest whole number.

#include<stdio.h>
#include<math.h>
int main() {
   double num = 13.65;
   printf("Ceil of %.2lf = %.2lf", num, ceil(num));
   return 0;
}
C

Output: Ceil of 13.65 = 14.00

Here, we’re using the ceil() function to round up the number 13.65 to the nearest whole number, which is 14.

13. free()

This free() function is your program’s housekeeper. It cleans up memory that was previously allocated using malloc or calloc.

#include<stdio.h>
#include<stdlib.h>
int main() {
   int* ptr = (int*) malloc(10 * sizeof(int));
   if(ptr != NULL) {
      printf("Memory allocated successfully.\n");
      free(ptr);
      printf("Memory deallocated successfully.");
   }
   return 0;
}
C

Output:

Memory allocated successfully.
Memory deallocated successfully.

Here, we’re using the malloc() function to allocate memory for 10 integers. Then, we use the free() function to deallocate that memory. It’s like cleaning up after a party. Always remember to clean up!

14. exit()

This function is your program’s emergency exit. It immediately terminates the program.

#include<stdio.h>
#include<stdlib.h>
int main() {
   printf("Start of the program.\n");
   exit(0);
   printf("End of the program."); // This line will not be printed
   return 0;
}
C

Output: Start of the program.

In this example, we’re using the exit() function to terminate the program. The line after exit() is not printed because the program has already ended. It’s like saying “I’m out!” and leaving the room.

15. rand()

This rand() function is your program’s lucky charm. It generates a random number.

#include<stdio.h>
#include<stdlib.h>
int main() {
   printf("Random Number: %d", rand());
   return 0;
}
C

Output: A random number

In this example, we’re using the rand() function to generate a random number. Each time you run this program, you’ll get a different number. It’s like a surprise every time!

16. getchar()

This getchar() function is your program’s listener. It reads a character from the user’s input.

#include<stdio.h>
int main() {
   char ch;
   printf("Enter a character: ");
   ch = getchar();
   printf("You entered: ");
   putchar(ch);
   return 0;
}
C

Output: Depends on the character entered by the user.

In this example, we’re using the getchar() function to read a character from the user. The character that the user types is then printed on the screen.

17. putchar()

This putchar() function is your program’s speaker. It writes a character to the screen.

#include<stdio.h>
int main() {
   char ch = 'A';
   putchar(ch);
   return 0;
}
C

Output: A

Here, we’re using the putchar() function to print the character ‘A’ on the screen. It’s like your program saying a single letter out loud.

18. fopen()

This fopen() function is your program’s librarian. It opens a file.

#include<stdio.h>
int main() {
   FILE* fp;
   fp = fopen("test.txt", "w");
   if(fp == NULL) {
      printf("Error opening file.");
      return -1;
   }
   printf("File opened successfully.");
   fclose(fp);
   return 0;
}
C

Output: File opened successfully. (Assuming the file can be opened)

In this example, we’re using the fopen() function to open a file named “test.txt”. If the file is successfully opened, the program prints “File opened successfully.”.

19. fclose()

This fclose() function is your program’s responsible friend. It closes a file that was previously opened.

#include<stdio.h>
int main() {
   FILE* fp;
   fp = fopen("test.txt", "w");
   if(fp == NULL) {
      printf("Error opening file.");
      return -1;
   }
   printf("File opened successfully.\n");
   fclose(fp);
   printf("File closed successfully.");
   return 0;
}
C

Output:

File opened successfully.
File closed successfully.

Here, we’re using the fclose() function to close the file that we opened with fopen(). It’s like saying goodbye after a visit.

20. feof()

This feof() function is your program’s bookworm. It checks if the end of a file has been reached.

#include<stdio.h>
int main() {
   FILE* fp;
   int c;
   fp = fopen("test.txt", "r");
   if(fp == NULL) {
      printf("Error opening file.");
      return -1;
   }
   while(1) {
      c = fgetc(fp);
      if(feof(fp)) {
         break;
      }
      printf("%c", c);
   }
   fclose(fp);
   return 0;
}
C

Output: The content of the file test.txt

In this example, we’re using the feof() function to read a file until the end. It’s like reading a book page by page until you reach the end.

I hope these examples help you understand C Library Functions better! Remember, practice makes perfect. So, keep coding, keep learning, and most importantly, have fun doing it!

Scroll to Top