UPI Study › Courses › Programming in C › Free lessons › Lesson 3.6 Free with a free account
Building Your First C Program
Programming in C · Chapter 3: Data Types, Variables, Math Library, and Console I/OSection 3.6 · lesson 18 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: Workshop: Variables, Data Types, and Formatted Output →← Console Input and Output: printf() and scanf()Next lesson: Workshop: Variables, Data Types, and Formatted Output →
In this lesson
The parts of a complete C program and what a build reports.
- Identify the comments, include line, main function and statements in a C program
- Explain what the preprocessor does with the stdio.h include line
- Use main's return value as an exit status and check it in the shell
- Distinguish clean builds, warnings and errors
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 (821 words). Read along, or search it.
Opening Question
Clean Compile, So Is It Done
Priya's first finished program builds without a single message. Clean compile, she asks, so it's done? To answer her, you need to know every part of a C program, what the compiler can tell you, and what it can't.
Program Map
Comments, Include and main
Here's Priya's program for the arcade's front desk. It asks for a name and a token count, and says how many games the player can have. At the top, comments for people. Then hash include, which borrows the input and output tools. Then main. When you run the program, the computer starts in main, and nowhere else. Void says main takes nothing in, and the int in front says it hands back a whole number. The braces group statements into one block. A statement is one instruction, and it normally ends with a semicolon, like a sentence ends with a period. Everything between main's braces runs from top to bottom, one statement at a time, ending with return zero. Run it: Maya buys 11 tokens and can play 5 games.
Preprocessor
What the Include Line Pastes In
The include line isn't code for the computer to run. It's an instruction for the preprocessor, the helper that works before the compiler. It pastes the whole named file into yours, at that spot. Standard i o dot h tells the compiler that printf and scanf exist, and what they take. Angle brackets mean a header that came with the compiler. Forget the line, and Clang stops: call to undeclared library function printf. The note names the exact header to include. Read the notes: they're often the cure. Add the line back, and it builds and says hello. Nearly every program you write will start with that line.
Exit Status
Return Zero Means Success
When main finishes, it hands one whole number back to whoever started the program. That's the exit status. By a worldwide habit, zero means success, and any other number means something went wrong. Scripts check it to decide whether to carry on. This program ends with return three, on purpose. Run it, then ask the shell with echo dollar question mark. It prints the last command's status: three. A program that ends with return zero reports zero. And modern C lets main end with no return at all, which also counts as zero. Writing return zero makes your intent clear.
Quick Check
Screen Says Passed, Status Says One
Quick check. Sam's test program prints all tests passed, and ends with the return line on screen. He runs it, then types echo dollar question mark. What does that print? Pause and predict. One. The exit status comes only from return, not from what the screen says. The text can claim success while the status reports a failure. Zero would need return zero. The message is for the person at the screen; the status is for other programs.
Comments
Notes and Switched-Off Lines
A comment is text for human readers that the compiler skips completely. A two-slash comment runs to the end of its line, even after code. A slash star comment runs until the closing star slash, even across several lines. A comment can also switch a line off for a while, which is handy while testing. Run it: only the tokens line prints. The commented-out printf never runs.
Build Outcomes
Silence, Warnings or Errors
Now, how does a build talk back to you? There are three outcomes. Nothing at all is a clean build: a program is made, so run it. Warning lines mean: this looks risky. A program is still made, but fix the warning. Error lines mean the rules were broken, and no program is made.
Warning Example
An Unused Variable Left Behind
Here's one more warning, a harmless-looking leftover. Clang noticed a variable that nobody uses: unused variable spare tokens. That usually means a forgotten line or a typo. The program was still built, and it runs. But a clean file has no warnings at all. Delete the extra variable, build again, and Clang says nothing. That's what a finished file looks like.
Quick Check
A Missing Semicolon Makes No Program
Quick check. Sam left the semicolon off the end of this printf line, and builds it. Which outcome is it, and is there a program to run afterwards? Pause and decide. An error: expected semicolon after expression. A statement must end with a semicolon, so the rules were broken, and Clang even shows where it goes. No program file is made. Asking for it by name finds nothing. A missing semicolon is never just a warning.
Story Ending
Testing Proves the Answer
Back to Priya's question. Clean compile, so it's done? Not yet. A clean build means the rules were followed, not that the answers are right. Sam typed 11 tokens and checked by hand: 5 games, because 11 divided by 2 in whole numbers is 5. Now it's done, he said.
Key Takeaways
Parts, Status and Build Results
To recap. A program has comments, includes, and a main function whose statements run top to bottom, each ending with a semicolon. Main's return value is the exit status: zero for success, read with echo dollar question mark. A build prints nothing, warnings, or errors. Only errors stop the program being made, and only testing proves the answers.
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 Building Your First C Program
What does return 0 mean in a C main function?
Why does a C program need #include <stdio.h>?
What is the difference between a compiler warning and an error?
Where does a C program start running?
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 →