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
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Programming in C.
Lesson complete
Earn the 3 college credits for Programming in C
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Variable Scope in C →← Function Definition in CNext lesson: Variable Scope in C →
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.
Enroll now → Course details Download the syllabus All Computer Science courses Every course with PRO →
Compare: C vs C++
Why take it here
Why students take Programming in C with UPI Study
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.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →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?
How do you read a C function signature?
Which header do you need for toupper, strlen and sqrt in C?
Why does rand give the same numbers every time a C program runs?
About the Programming in C course
How many college credits is Programming in C?
How much does Programming in C cost and what is included?
Which universities accept UPI Study credits?
How long does Programming in C take?
How are the exams done?
How do my credits reach my college?
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.
3 credits · NCCRSEnroll →