C Program Formatting Standards, Explained with Code Examples

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

← Setting Up the C Development EnvironmentNext lesson: Numeric Data Representation 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

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.

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 Program Formatting Standards

Does indentation affect how a C program runs?
No, indentation does not change how a C program runs, because C is a free-form language that treats spaces, tabs and line breaks alike as whitespace. The compiler reads the braces and statements, not the layout. Indentation is for human readers, showing which lines belong inside a block. Four spaces per level is a common choice, and staying consistent matters more than the exact number.
Why should an if statement use braces around a single line?
Braces prevent a common bug, because an if without braces controls only the very next statement. If a second line is added and indented under the if, it looks conditional but runs every time. Clang with -Wall and -Wextra may not warn about this misleading layout. Wrapping even one statement in braces means any line added later really belongs to the if.
What is the difference between K&R style and Allman style in C?
K&R and Allman are two ways to place braces. In K&R style, named after Kernighan and Ritchie's book, the opening brace stays at the end of the line that starts the block. In Allman style, named after programmer Eric Allman, the opening brace sits on its own line, lined up with the closing brace. Both are correct C, and the compiler builds the same program from either.
What makes a good comment in C code?
A good comment explains why the code does something rather than repeating what it does. A comment saying tokens per game is 4 restates the code, while every game costs four tokens explains the business rule behind the number. C has line comments that start with // and block comments between /* and */. The compiler ignores comments completely, so they never change what a program does.

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 →