Copyright
Computer Science Courses / Course / Chapter

Standard Library Functions in C Programming

Lesson Transcript
Instructor: Martin Gibbs

Martin has 21 years experience in Information Systems and Information Technology, has a PhD in Information Technology Management, and a master's degree in Information Systems Management. He is an adjunct professor of computer science and computer programming.

C programming is a language used in computing to serve general purposes. Learn about standard library functions in C programming and review standard input and output, as well as math functions, to gain understanding. Updated: 01/24/2022

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):

#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:

#include <stdio.h>
int main(void) {
/* Rest of program */
}

Now, let's take a look at some of the libraries, starting with stdio.h.

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Variable Scope in C Programming

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Standard Libraries
  • 1:06 Stdio.h: Standard Input/Output
  • 1:59 String.h: String Functions
  • 2:29 Math.h: Math Functions
  • 3:45 Time.h: Date & Time Functions
  • 5:01 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed

Stdio.h: Standard Input/Output

First, here's an example of some stdio.h code:

#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:

#include <stdio.h>
int main(void) {
  char login[50];
  printf("Enter Name: ");
  scanf("%s", login);
  printf("\nWelcome %s\n", login);
}

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:

#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:

#include <stdio.h>
#include <string.h>
int main(void) {
  char fname[30] = "Jane";
  char lname[30] = "Doe";
  char full_name[61];
  strcat(full_name, fname);
  strcat(full_name, " " );
  strcat(full_name, lname);
  printf("%s ", full_name);
}

Output of this program is:

Jane Doe

Math.h: Math Functions

Now, here's an example of a math.h function:

#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:

double age = 15.3445;
/* Floor */
printf("%f", floor(age));
/* Ceiling */
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:

#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:

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back

Resources created by teachers for teachers

Over 30,000 video lessons & teaching resources‐all in one place.
Video lessons
Quizzes & Worksheets
Classroom Integration
Lesson Plans

I would definitely recommend Study.com to my colleagues. It’s like a teacher waved a magic wand and did the work for me. I feel like it’s a lifeline.

Jennifer B.
Teacher
Jennifer B.
Create an account to start this course today
Used by over 30 million students worldwide
Create an account