Building Your First C Program, Explained with Code Examples

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

← Console Input and Output: printf() and scanf()Next lesson: Workshop: Variables, Data Types, and Formatted Output →

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

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.

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 Building Your First C Program

What does return 0 mean in a C main function?
return 0 in main sends an exit status of 0 back to whatever started the program, and 0 means success by convention. Any other value signals that something went wrong, and scripts check this status to decide whether to continue. In a Unix shell, echo $? prints the last command's exit status. Since C99, reaching the end of main without a return also gives 0.
Why does a C program need #include <stdio.h>?
The line #include <stdio.h> tells the preprocessor to paste in the standard input and output header, which declares functions such as printf and scanf. Without it, Clang stops with call to undeclared library function printf, because since C99 a function must be declared before it is called. Angle brackets mean a header that comes with the compiler. Nearly every C program that prints text starts with this line.
What is the difference between a compiler warning and an error?
An error means the code breaks C's rules, so no program is made, while a warning means the compiler can still build it but something looks risky, so a program is still made. A missing semicolon is an error. An unused variable or a variable read before it is set produces a warning. A clean build prints nothing at all. Warnings deserve fixing, because they often point to forgotten lines or typos.
Where does a C program start running?
A C program starts running in the main function. In int main(void), void says main takes no arguments, and int says it returns a whole number to whoever started the program. The statements between main's braces run from top to bottom, one at a time, and each statement normally ends with a semicolon. Comments above main are ignored by the compiler.

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 →