Sunday, October 6, 2019

Standard Library Functions in C Programming

Standard Library Functions in C Programming


Libraries in C allow you to re-use important functions without the need for extra lines of code. In this lesson, you'll learn about the most common standard libraries used in C. Working code examples will be provided.

Standard Libraries

Imagine that you want to display output to the screen, but each time you want to display output you have to re-write 1500 lines of special code to make that happen. This is very inefficient and wasteful - not to mention the 1500 opportunities for bugs in your program!
Thankfully, the C programming language comes with a huge library of functions that you can use in your code, without having to re-write everything. You only need to include the library at the top of your code. When you use a command such as printf (to print to the screen), you're actually using a function that is in a standard C library.
Libraries are included in your code by referencing the header file. Think of it as the link to the library. To add the header file to your code, add the following at the top of your program (before any other statements):
  1. #include <filename.h>
Note that there's no semicolon at the end of this line. One of the most common libraries used is the stdio library, which includes functions to write output to the screen and collect input from the user:
  1. #include <stdio.h>
  2. int main(void) {
  3. /* Rest of program */
  4. }
Now, let's take a look at some of the libraries, starting with stdio.h.

Stdio.h: Standard Input/Output

First, here's an example of some stdio.h code:
  1. #include <stdio.h>
The standard input and output library is stdio.h, and you will find that you include this library in almost every program you write. It allows printing to the screen and collecting input from the user. The functions you will use the most include:
  • printf() is output to the screen
  • scanf() is read input from the screen
  • getchar() is return characters typed on screen
  • putchar() is output a single character to the screen
  • fopen() is open a file, and
  • fclose() is close a file
There are many times when we need to get user input from the screen and output information to the screen. Here is code that asks a user for their login name and then displays it on screen:
  1. #include <stdio.h>
  2. int main(void) {
  3.   char login[50];
  4.   printf("Enter Name: ");
  5.   scanf("%s", login);
  6.   printf("\nWelcome %s\n", login);
  7. }
When the code is compiled and executed, the output is as follows:
Enter Name: Jane
Welcome Jane

String.h: String Functions

First off, here's an example of string.h:
  1. #include <string.h>
The functions in string.h are useful for manipulating strings and characters:
  • strcat(): concatenates (joins) one string to another string
  • strcpy(): copies a string's content to another string, and
  • strcmp(): compares the contents of two strings
For example, the code here concatenates the user's first name and last name into a single string:
  1. #include <stdio.h>
  2. #include <string.h>
  3. int main(void) {
  4.   char fname[30] = "Jane";
  5.   char lname[30] = "Doe";
  6.   char full_name[61];
  7.   strcat(full_name, fname);
  8.   strcat(full_name, " " );
  9.   strcat(full_name, lname);
  10.   printf("%s ", full_name);
  11. }
Output of this program is:
Jane Doe

Math.h: Math Functions

Now, here's an example of a math.h function:
  1. #include <math.h>
The functions in math.h usually require double data type as a parameter. That is, if you're tying to find the square root of 5.325, you'll need to write C code to calculate the square root on that number!
  • floor(double x): Returns the next-lowest value to x (or x, if x is an integer)
  • ceil(double x): Returns the smallest value greater than or equal to x
  • exp(double x): Returns the value of a number raised to the xth power
  • sqrt(double x): Square root of x
C supports all math functions, and we have only covered a few here. The following code shows an example of using the floor and ceiling functions in C:
  1. double age = 15.3445;
  2. /* Floor */
  3. printf("%f", floor(age));
  4. /* Ceiling */
  5. printf("%f", ceil(age));
The output will display 15 and 16, respectively. The closest bottom integer to 15.3445 is 15; the next-highest is 16. This is useful in situations where you would need to work with a whole number for age, but you still need to store the value as is. For life insurance, you may charge rates based on either the floor or ceiling of a person's age.
Math.h also includes functions for trigonometry, fractions, and advanced mathematics.

Time.h: Date & Time Functions

And finally, here's an example of some time and date, or time.h, functions:
  1. #include <time.h>
There are a few date and time functions that are useful for your code. For example, if you need to write log files for a program, you can date and time-stamp that data by accessing the time library.
Some time.h functions:

No comments:

Post a Comment