Help improve SovranCode?

Google Analytics can measure anonymous usage after you choose Allow. Essential account and progress features work either way.

SovranCode
C: Foundations for Systems Programming Your First C Program
This device
Course contentsYour First C Program · 15 titles

C foundations

What C Is and How C Programs RunYour First C ProgramVariables, Types, and OperatorsInput and Output with printf and scanfConditions and Loops

Program structure and data

Functions, Headers, and ScopeArrays and StringsPointers and Memory AddressesStructures, Enumerations, and UnionsFiles and Command-Line Arguments

Memory, tooling, and practice

Dynamic Memory AllocationPreprocessor Directives and Separate CompilationErrors, Debugging, and Undefined BehaviorBuild Tools and MakefilesProject: Command-Line Inventory Manager
C: Foundations for Systems Programming15 complete lessons

C foundations

What C Is and How C Programs RunYour First C ProgramVariables, Types, and OperatorsInput and Output with printf and scanfConditions and Loops

Program structure and data

Functions, Headers, and ScopeArrays and StringsPointers and Memory AddressesStructures, Enumerations, and UnionsFiles and Command-Line Arguments

Memory, tooling, and practice

Dynamic Memory AllocationPreprocessor Directives and Separate CompilationErrors, Debugging, and Undefined BehaviorBuild Tools and MakefilesProject: Command-Line Inventory Manager
PREVIOUS LESSONWhat C Is and How C Programs Run
NEXT LESSONVariables, Types, and Operators
C fundamentals 55 min

Your First C Program

A C program is small enough to hold in your head when you know what every line promises. In this lesson, you will write a complete source file, run it, observe its terminal output, respond to a compiler message, and make a small program of your own.

What you will leave with

You will be able to save a complete .c source file, explain the purpose of #include <stdio.h>, define main, distinguish braces from semicolons, choose between puts and printf, use a warning-enabled local build command, repair a missing statement boundary, and explain return 0;.

Build one reliable mental template

Source fileRecognize the complete file that a compiler receives, from the include directive to the final brace.
Observable outputPredict exactly which terminal lines each output call will create before you run the program.
Compiler feedbackUse a diagnostic as a location clue, then repair the smallest missing boundary.
Repeatable loopWrite, build, run, observe, and change one intentional thing at a time.

Start with a complete source-file template

Begin with the smallest program that has a real purpose: it produces a line of terminal output and reports that it finished normally. Copy the structure into a file called hello.c, then change the greeting before you run it. The browser runner uses a safe beginner subset; a local compiler is still the authority for real C code.

C FUNDAMENTALS RUNNER

Create a complete hello.c

Change the greeting inside the quotes. Predict the exact terminal line, then run the source and compare the output with your prediction.

#include <stdio.h>

int main(void) {
  puts("Hello from C!");
  return 0;
}
TERMINAL OUTPUT
Edit the program, predict its output, then run it.

This safe browser runner supports the lesson subset: scalar and fixed-size int/double arrays, character strings, indexed reads and numeric writes, arithmetic, printf/puts, numeric scanf into scalar or bounded array-element addresses, comparisons, bounded if/else, for/while loops, and return.

Try this next: Replace the greeting with your own sentence, then add one more puts call above return 0;.

Read the shape before reading the words

A first program is easier to debug when you separate its roles. The directive at the top prepares declarations for the compiler. The function header defines where the program starts. Braces mark the body of that function. Ordinary statements inside the braces end with semicolons. The final return statement communicates an exit status to the environment.

#include <stdio.h>

Prepare output declarations

Headers give your source declarations for library facilities. Because this program uses puts and printf, it includes the standard input/output header before main.

int main(void)

State the entry function

In a normal hosted program, the runtime begins at main. This form returns an integer status and accepts no parameters.

{ ... }

Group the program body

Opening and closing braces define the body of main. They express structure, not the end of every individual instruction.

statement;

End each instruction

A semicolon completes ordinary C statements such as an output call or return 0;. Forgetting one changes how the compiler reads the next line.

Use comments and order to make intent visible

C runs the statements in main in order, from top to bottom, unless you later add control flow. Comments are ignored by the compiler, so use them to explain a decision or a non-obvious constraint—not to repeat what a clear line already says. Run this source, then move one output call and predict how the terminal order changes.

C FUNDAMENTALS RUNNER

Trace a linear program

The runner ignores both line and block comments, just as a compiler does. Focus on the order of the two output calls.

#include <stdio.h>

/* A complete C program has one entry function. */
int main(void) {
  // puts adds a newline after this message.
  puts("Build, run, observe.");
  printf("Then make one small change.\n");
  return 0;
}
TERMINAL OUTPUT
Edit the program, predict its output, then run it.

This safe browser runner supports the lesson subset: scalar and fixed-size int/double arrays, character strings, indexed reads and numeric writes, arithmetic, printf/puts, numeric scanf into scalar or bounded array-element addresses, comparisons, bounded if/else, for/while loops, and return.

Try this next: Move the printf call above puts, run again, and explain why the terminal order changed.

Choose an output call on purpose

puts is ideal for one plain line of text: it writes the string and adds a newline. printf gives you more control: it follows the format string exactly, so you place \n where a new terminal line belongs. Later, printf will also display values; for now, use it to make the newline rule visible.

C FUNDAMENTALS RUNNER

Compare puts and printf

Run the program first, then remove one newline escape from a printf call and observe which output fragments join together.

#include <stdio.h>

int main(void) {
  puts("One line from puts.");
  printf("A second line from printf.\n");
  printf("A third line from printf.\n");
  return 0;
}
TERMINAL OUTPUT
Edit the program, predict its output, then run it.

This safe browser runner supports the lesson subset: scalar and fixed-size int/double arrays, character strings, indexed reads and numeric writes, arithmetic, printf/puts, numeric scanf into scalar or bounded array-element addresses, comparisons, bounded if/else, for/while loops, and return.

Try this next: Make the second and third messages appear on one terminal line, then restore a readable two-line result.

Build the source on your machine

Save the source as hello.c. Building from a terminal makes the source-to-executable boundary concrete: the compiler reads the file, reports warnings or errors, and produces a program you can start. Use warning flags from the beginning so suspicious code is visible before it becomes a habit.

macOSInstall the command-line tools once, then use Clang.xcode-select --installclang -Wall -Wextra -Wpedantic hello.c -o hello./hello
LinuxUse GCC or Clang from your distribution’s build-tools package.gcc -Wall -Wextra -Wpedantic hello.c -o hello./helloIf gcc is unavailable, install your distribution’s compiler or build-essential package first.
WindowsUse a Visual Studio Developer Command Prompt or another configured C toolchain.cl /W4 hello.chello.exeToolchain installation differs, but the reliable loop stays the same: compile, read diagnostics, then run the executable.
Warnings are useful evidence

A program can build and still deserve attention. -Wall, -Wextra, and -Wpedantic request useful categories of warnings. Read them before adding more code; a small correction is cheaper than a later mystery.

Finish with a meaningful status

When your program reaches return 0;, it returns a conventional success status to the shell or operating system. That status is separate from the terminal text. A program can print a friendly message and still return a non-zero status to signal a failure; you will use those distinctions more deliberately once branches and errors arrive later in the course.

Keep return last in a first program

The browser runner deliberately reports a statement after return as an error. This makes the first program’s order visible: output happens first, then the program finishes. In later lessons, you will learn the precise rules for early returns and control flow.

Repair the missing statement boundary

This source has one missing semicolon. The compiler—or the safe lesson runner—cannot know where the puts statement stops before it sees return. Read the message, inspect the previous line, add only the semicolon, and run it again.

C FUNDAMENTALS RUNNER

Repair one missing semicolon

Make the smallest possible change. The goal is not to guess; it is to connect the diagnostic to a precise source boundary.

#include <stdio.h>

int main(void) {
  puts("Every statement needs a boundary.")
  return 0;
}
TERMINAL OUTPUT
Edit the program, predict its output, then run it.

This safe browser runner supports the lesson subset: scalar and fixed-size int/double arrays, character strings, indexed reads and numeric writes, arithmetic, printf/puts, numeric scanf into scalar or bounded array-element addresses, comparisons, bounded if/else, for/while loops, and return.

Try this next: After the program works, remove the closing brace and compare that structural problem with the missing-semicolon error.

Independent lab: publish a terminal event card

Build a three-line terminal announcement without copying an earlier message. Keep the include directive, main signature, braces, and return 0;. Change the event name, add a plain line with puts, and add a formatted line with printf that ends with \n. Run the result and explain why each terminal line appears where it does.

C FUNDAMENTALS RUNNER

Build a three-line announcement

Use the starter as a structural template, but write your own event information. Make the terminal output easy for a person to scan.

#include <stdio.h>

int main(void) {
  puts("Event: C study session");
  printf("Bring a notebook and arrive at 10:00.\n");
  return 0;
}
TERMINAL OUTPUT
Edit the program, predict its output, then run it.

This safe browser runner supports the lesson subset: scalar and fixed-size int/double arrays, character strings, indexed reads and numeric writes, arithmetic, printf/puts, numeric scanf into scalar or bounded array-element addresses, comparisons, bounded if/else, for/while loops, and return.

Try this next: Add one more puts line, then put the printf call first and predict the changed output order before running it.

Lesson review

You can now read a complete C source file as a set of small contracts: the header prepares declarations, main defines the entry point, braces create the body, semicolons complete statements, output calls create terminal text in order, and return 0; reports normal completion. That is enough structure to begin adding values in the next lesson without treating the program as unexplained punctuation.

  • I can write the smallest complete source file that includes output, main, and a success status.
  • I can explain the different jobs of braces, parentheses, semicolons, and newline escapes.
  • I can predict output order and choose between puts and printf for a plain line.
  • I can compile with warnings enabled, use a diagnostic to find a missing semicolon, and make one targeted repair.
KNOWLEDGE CHECK

Read a complete C program with confidence

Answer every question from the program itself. Then use the explanations to make each punctuation mark, output call, and exit status part of your mental model.

01Which line makes the declarations for puts and printf available to this source file?
02What punctuation ends a normal C statement such as puts("Hello")?
03How does puts("Ready") differ from printf("Ready") in this lesson?
04Why should return 0; remain at the end of this beginner program?
05A compiler reports an expected semicolon before return. What is the best first repair?
PREVIOUS LESSONWhat C Is and How C Programs Run
NEXT LESSONVariables, Types, and Operators
ON THIS PAGEYour First C ProgramLesson mapSource file templateProgram shapeComments and orderOutput callsCompile locallyExit statusRepair an errorIndependent labLesson reviewKnowledge check
Course contents