C Standard Library Overview, Explained with Code Examples

UPI Study › Courses › Programming in C › Free lessons › Lesson 6.2 Free with a free account

C Standard Library Overview

Programming in C · Chapter 6: Functions, Scope, Storage Classes, and RecursionSection 6.2 · lesson 34 of 786 min · free with a free account

← Function Definition in CNext lesson: Variable Scope in C →

Earn 3 college credits for Programming in CRecommended for credit by NCCRS · Computer Science 240 · self-paced · one payment, lifetime access

In this lesson

Five standard headers and how to read a library function's signature.

  • Explain what #include adds and what the linker supplies
  • Read a function signature to find its return type and parameters
  • Name common functions from stdio.h, stdlib.h, string.h, math.h and ctype.h
  • Fix an undeclared library function error by including the right header

Free study notes

Get this lesson's notes by email

The key points and 4 practice questions with answers, in your inbox. One email, free.

One email. Unsubscribe in one click.

Transcript

Everything said in the video, section by section (796 words). Read along, or search it.

Opening Analogy

Toolboxes Instead of New Hammers

Nobody builds a house by first inventing the hammer. Builders walk into a hardware store, find the right aisle, and pick up a tool that works. C gives you the same thing: a big collection of ready-made functions, called the C standard library. It comes with every C compiler. It's split into groups, and each group has a header file, ending in dot h, that lists its tools. Think of each header as a labeled toolbox. Five toolboxes cover most beginner programs: stdio, stdlib, string, math, and ctype.

Headers and Linking

Prototypes From Headers, Code From the Library

So what does #include stdio.h really do? It pastes the header's list of prototypes into your file. Those are the promises from lesson 6.1. Now the compiler knows each tool's name, inputs, and output, and can check every call. But the real code for each tool is already compiled, inside the library. The linker adds it to your program when you build. One extra step: on Linux, the math tools need the flag dash l m to link. On a Mac they're linked anyway, so the flag is harmless there.

Signatures

Reading a Function's Label

Every tool has a label, called its signature. It's the tool's prototype, and the first thing to read on any reference page. Read it left to right. The type before the name is what comes back. The types in parentheses are what you must hand over. abs takes one int, and gives back an int: the number without its minus sign. pow takes two doubles, and gives back a double: the first raised to the power of the second. And srand takes an unsigned int, a starting number, and gives back nothing at all: void.

Library Tour

Tools From stdio.h and stdlib.h

Here's a tour of the first two toolboxes. puts prints text plus a newline, and putchar prints one single character. abs drops a minus sign. rand gives a number that looks random, and percent 100 keeps it from 0 to 99. srand of 42 picks its starting point, called the seed, first. And exit ends the whole program, on the spot. Build and run. Game over, the letter A, and abs of minus 35 is 35. Then three bonus draws. The last line never printed. Exit, with 0 for finished normally, stopped everything first. The draws, 94, 23, and 9, are real output from this Mac. Another system's library may give a different list for the same seed. Clear the screen and run it again: the same list. rand follows a fixed recipe that only looks random, so one seed always gives one list.

Library Tour

Tools From string.h, math.h and ctype.h

One program can use many toolboxes. Include each header once, at the top. strlen counts the letters in a string. sqrt and pow do math. And toupper and isdigit look at one character at a time. Build it, with dash l m for the math library, and run it. Priya has 5 letters. strlen doesn't count the invisible end mark of a string, which you'll meet in lesson 7.5. The square root of 144 is 12, and 2 to the power 10 is 1024. toupper turns a into capital A, and isdigit says yes for 7. The standard only promises a non-zero number for yes. So test isdigit with if, and never compare it with 1.

Common Mistake

Calling abs Without stdlib.h

What if you forget the header? Here, abs is called, but stdlib.h was never included. Build it. Clang stops: call to undeclared library function abs. It's the rule from lesson 6.1: no prototype, no call. Then read its note: include the header stdlib.h. Add exactly what it says, and build again. Tickets: 12. One missing line, and one clear message telling you how to fix it.

Quick Check

Calling toupper Without ctype.h

Quick check. This program calls strlen and toupper. At the top, it includes stdio.h and string.h. What happens when you build it? Pause and predict. Build it. Clang stops at toupper: call to undeclared library function. Its note names the header, ctype.h. strlen was fine, because string.h was included. toupper works on single characters, so it lives in the character toolbox. Add ctype.h, build again, and it prints 3 letters, then a capital S. Every tool has one home, and the note tells you where it is.

Wrap-Up

A Fixed Seed for Testing

Back at the arcade, Priya wanted the lucky draw to feel random. Sam asked: how do I test something that changes every time? They fixed the seed with srand of 42 while testing, so every test run drew the same numbers. For the real game, they switched to a seed that changes, such as the current time.

Key Takeaways

The Standard Library in Review

To sum up. A header lists a toolbox's prototypes, and the library supplies the compiled code. A signature shows what comes back, and what you must hand over. The same seed gives the same rand list, every run. And a missing header means an undeclared function error, with a note naming the header.

About Programming in C at UPI Study

Programming in C (Computer Science 240) is a self-paced online course of 78 video lessons in 11 chapters, from pseudocode and your first program to data types, decisions, loops, functions, arrays, strings, pointers, files, structures, dynamic memory and linked lists. It carries a NCCRS credit recommendation for 3 semester credits.

Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Compare: C vs C++

Why take it here

Why students take Programming in C with UPI Study

How programs work close to the machineProgramming in C teaches how a program uses memory: variables and data types, control flow, functions, arrays and strings, pointers, files, structures and dynamic memory. C is the base for operating systems, embedded devices and many computer-science courses.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Where the credit goes

Universities that work with NCCRS

Programming in C carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.

FAQ

Questions students ask us

About C Standard Library Overview

What does #include do in a C program?
#include pastes a header file's contents, mainly function prototypes, into your source file before it is compiled. That lets the compiler check each call's name, arguments and return type. The functions' compiled code is already in the library, and the linker adds it when the program is built. On Linux, functions from math.h usually also need the -lm flag when linking.
How do you read a C function signature?
Read it left to right: the type before the name is what the function returns, and the types in parentheses are what you must pass in. int abs(int) takes an int and returns it without its minus sign. double pow(double, double) raises the first argument to the power of the second. void srand(unsigned int) takes a seed and returns nothing.
Which header do you need for toupper, strlen and sqrt in C?
toupper and isdigit are in ctype.h, strlen is in string.h, and sqrt and pow are in math.h. Input and output functions such as printf, puts and putchar are in stdio.h, while abs, rand, srand and exit are in stdlib.h. If a header is missing, clang reports a call to an undeclared library function, and its note names the header to include.
Why does rand give the same numbers every time a C program runs?
rand follows a fixed recipe that only looks random, starting from a seed, so the same seed always produces the same sequence. Calling srand(42) before rand gives one repeatable list on a given system, which is useful for testing. Another C library may give a different list for the same seed. For a real game, seed with a value that changes, such as the current time.

About the Programming in C course

How many college credits is Programming in C?
Programming in C (Computer Science 240) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does Programming in C cost and what is included?
$250, paid once, with lifetime access: all 78 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does Programming in C take?
It is self-paced with no deadlines. The 78 lessons total about 9 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

Ready for the full course? Earn 3 credits in Programming in C.

78 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.

Programming in C
3 credits · NCCRS
Enroll →