UPI Study › Courses › Programming in C › Free lessons › Lesson 2.3 Free with a free account
C Program Formatting Standards
Programming in C · Chapter 2: C Language Setup, Data Types, Variables, and Console I/OSection 2.3 · lesson 10 of 787 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: Numeric Data Representation in C →← Setting Up the C Development EnvironmentNext lesson: Numeric Data Representation in C →
In this lesson
Indent, space, brace, name and comment C code for human readers.
- Explain why whitespace and layout matter to readers but not to the compiler
- Apply consistent indentation, spacing and naming conventions
- Explain why an if without braces controls only the next statement
- Compare K&R and Allman brace styles and write comments that explain why
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 (928 words). Read along, or search it.
Opening Analogy
A Recipe in One Sentence
Imagine a recipe written as one giant sentence, with no line breaks, no numbers, and ingredients called x and thing two. A cook could probably still make the dish, but slowly, and likely wrong. Code is the same. The compiler doesn't care how your program looks. The person who reads it tomorrow does, and that's usually you.
Free-Form Code
Whitespace the Compiler Ignores
C is a free-form language. The compiler treats a space, a tab and a line break as the same kind of gap, called whitespace, and ignores extra ones. That freedom is the problem. Here's a tiny arcade program written with no care at all. It works out the tokens for three games at four tokens each, and says whether it's a big order. It compiles with no warnings, and it runs correctly. But what are T and p?
Same Program, Cleaned Up
Clear Names, Same Output
Now the same program, with the same meaning, laid out with care. Every name says what it holds. Compile and run: exactly the same output. The compiler built the same program from both, because it doesn't read the layout. But which one would you rather debug at midnight? Sam, the tester, says a bug in messy code is one sock in a pile of laundry.
Indentation
One Step In for Each Block
Indentation means starting a line further right, to show it belongs inside something. Everything between an opening brace and its matching closing brace is one block, and everything inside moves in by one step. Four spaces per step is the most common choice. Some teams use two, or eight. Pick one and stick to it. Inside the if block, the printf call moves in a second step. You can see the shape of the program at a glance.
Spacing Habits
Spaces Around Operators and Commas
Spaces help the eye, too. A few habits almost every C programmer follows. One space on each side of an operator: total equals games times cost, not all squeezed together. One space after a comma, and a blank line between groups of related lines, like paragraphs in an essay. One statement per line. Two on one line hide a step. And a space after if, for and while, but not between a function's name and its bracket.
Quick Check
When Braces and Indents Disagree
Here's the rule that matters most. The compiler follows only the braces, never your spacing. Here is a small program where the braces and the spacing disagree. Quick check. This program has three tokens, so the if test is false. Two printf calls are indented under it, with no braces. Read the if as: if this, then that. What does the program print? Pause and predict.
Answer and Fix
An if Controls One Statement
It prints Bonus game! With no braces, an if controls only the very next statement. The test is false, so the Big order line is skipped. The Bonus line just looks inside, because of its indentation. It runs every time. And look: the build is clean. With dash Wall and dash W extra, Clang says nothing about the misleading indentation. The fix is braces, even around one line. Then a line you add later really belongs, and the program prints nothing. That one habit prevents the whole bug.
Brace Styles
K&R or Allman
Where does the opening brace go? There are two famous places. In K and R style, named after the book from lesson 2.1, the opening brace stays at the end of the line that starts the block. In Allman style, named after the programmer Eric Allman, the opening brace gets a line of its own, lined up with the closing one. Both are correct C. Neither one is the right one.
Consistency
Pick One Style and Keep It
Here's the whole arcade program in Allman style. Compile and run it: the same output again. The compiler can't tell the two apart, so choose by taste, or by your team's rules. These lessons use K and R style, with the function's brace on the same line too. The one rule that matters is consistency. Use one style through a whole program. A file that mixes styles is hard to read.
Naming
Names That Tell the Story
Good names tell the reader what a value is. In the messy version, T and p told you nothing. Here, games and tokens per game tell the story. Use words from the real world: total tokens, not t t or x 1. Pick one way to join words, and keep it. Many C programmers write total underscore tokens. Another style is total Tokens, with a capital T. Don't mix the two in one program. And keep names as short as they can be while still clear.
Comments
Explain Why, Not What
A comment is a note for people. The compiler ignores it completely. C has two kinds. A line comment starts with two slashes, and runs to the end of the line. A block comment starts with slash star, ends with star slash, and can cover several lines. The best comments say why, not what. A comment that says tokens per game is 4 just repeats the code. Every game costs four tokens tells the reader something the code can't: the arcade's price. Compile and run once more. The comments changed nothing the program does.
Story Ending
What Was b Again
Priya hurried one night and wrote a program with names like a, b and c, and no comments. Next morning, Sam asked why the ticket count looked wrong. Priya stared at her own code for ten minutes before she remembered what b was. She renamed everything that afternoon.
Key Takeaways
Layout Is for the Reader
To recap. Whitespace and layout change nothing for the compiler, and everything for the reader. Indent one step per block, and follow braces, not indentation. Put braces even around one line. Pick K and R or Allman, one naming style, and stay consistent. Comment the why, not the what.
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 Program Formatting Standards
Does indentation affect how a C program runs?
Why should an if statement use braces around a single line?
What is the difference between K&R style and Allman style in C?
What makes a good comment in C code?
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 →