C Get Started
Getting started with C means learning a small but important workflow: write source code, compile it, and run the executable program. C is not normally interpreted line by line, so the compiler is part of everyday development. Once you understand this edit-build-run cycle, every later C topic becomes easier to test and reason about.
Overview: How C Development Works
A C program begins as plain text in a source file, usually with a .c extension. That file is readable by people, but the processor in your computer cannot execute it directly. A compiler such as gcc reads the source, checks the grammar and types, and translates the program into machine code for your operating system and CPU.
The full build process has a few stages. First, the preprocessor handles lines that begin with #, such as #include <stdio.h>. Including stdio.h gives the compiler declarations for standard input and output functions, including printf. Next, the compiler translates your C code into lower-level instructions. Then the linker connects your program with library code, such as the implementation of printf. The result is an executable file that the operating system can load and run.
Every hosted C program starts in a function named main. When you run the executable, the operating system creates a process, prepares memory for the program, and transfers control to main. Statements inside main run from top to bottom unless control-flow statements change the order. Returning 0 from main conventionally means the program completed successfully.
To follow this course you need a text editor and a C compiler. On Linux, gcc is commonly available through the system package manager. On macOS, the command line developer tools provide a compatible compiler. On Windows, common options include MSYS2, MinGW-w64, or WSL. The commands in this lesson use gcc, but the same ideas apply to other C compilers.
Syntax
A minimal C program has this general shape:
#include <stdio.h>
int main(void)
{
printf("Message goes here\n");
return 0;
}
| Part | Purpose |
|---|---|
#include <stdio.h> |
Makes standard input and output declarations visible to the compiler. |
int main(void) |
Defines the starting function. int means it returns an integer status code. |
{ ... } |
Groups the statements that belong to the function. |
printf(...); |
Calls a library function to print formatted text. |
return 0; |
Ends main and reports success to the operating system. |
After saving a source file as main.c, a typical build command is gcc main.c -o main. The -o main option names the output executable main. On Linux and macOS, run it with ./main. In many Windows terminal environments, the executable may be named main.exe.
Examples
Example 1: Your First Complete Program
#include <stdio.h>
int main(void)
{
printf("Hello, C!\n");
return 0;
}
Output:
Hello, C!
This program includes the standard I/O header, defines main, prints one line, and returns success. The \n inside the string is a newline character, so the terminal prompt appears on the next line after the program finishes.
Example 2: Compile And Run A Program That Calculates
#include <stdio.h>
int main(void)
{
int apples = 6;
int oranges = 4;
int total = apples + oranges;
printf("Apples: %d\n", apples);
printf("Oranges: %d\n", oranges);
printf("Total fruit: %d\n", total);
return 0;
}
Output:
Apples: 6
Oranges: 4
Total fruit: 10
This example shows why compiling matters. The compiler checks that variables are declared before use and that expressions such as apples + oranges make sense for their types. %d is a conversion specifier that tells printf to print an int value.
Example 3: A Small Program With A Success Check
#include <stdio.h>
int main(void)
{
double price = 19.99;
int quantity = 3;
double total = price * quantity;
printf("Unit price: $%.2f\n", price);
printf("Quantity: %d\n", quantity);
printf("Total: $%.2f\n", total);
return 0;
}
Output:
Unit price: $19.99
Quantity: 3
Total: $59.97
This program uses both double and int. The %.2f format prints a floating-point value with two digits after the decimal point. A compiler will not automatically prove that your business logic is correct, but it catches many structural mistakes before the program runs.
How It Works Step By Step
- You create a text file such as
main.cand write C source code in it. - You run a compiler command such as
gcc main.c -o main. - The preprocessor expands included headers and prepares the source for compilation.
- The compiler checks syntax and types, then generates object code for your platform.
- The linker combines your object code with needed library code and writes an executable.
- You run the executable. The operating system loads it into memory and starts
main. - The program writes output, returns a status code, and the operating system releases its resources.
Compiler diagnostics are part of the workflow, not a failure. If a build reports multiple errors, fix the first clear error first. One missing semicolon or brace can confuse the compiler and create several follow-up messages.
Common Mistakes
Forgetting That C Must Be Compiled
New learners sometimes try to run the .c file itself. The source file is input for the compiler; it is not the finished program. Compile first, then run the executable that the compiler creates.
Leaving Out Required Punctuation
C uses semicolons to end most statements and braces to mark blocks. A line like printf("Hi\n") is incomplete because it is missing the final semicolon. The corrected statement is printf("Hi\n");.
Using printf Without The Right Header
Modern C compilers expect a declaration before a function is called. If you call printf, include stdio.h at the top of the file. That lets the compiler check the call correctly.
Confusing Source Names And Output Names
In gcc main.c -o app, main.c is the input source file and app is the output executable name. Changing the output name does not rename the source file.
Best Practices
- Use a plain-text editor that shows line numbers and does not hide file extensions.
- Save C source files with a
.cextension, such asmain.corpractice.c. - Compile often. Small changes are easier to debug than a large file full of untested code.
- Read compiler messages from the top. The first real error is often the cause of later errors.
- Enable warnings as you progress, for example with
gcc -Wall -Wextra main.c -o main. - Keep examples complete while learning: include headers,
main, andreturn 0;. - Use clear file names and avoid spaces in beginner command-line projects.
Practice Exercises
- Write a program that prints your name on one line and your favorite programming goal on the next line.
- Create a program with two
intvariables namedwidthandheight, then print the area of a rectangle. - Modify the price example so it uses a different item price and quantity. Predict the output before compiling.
Summary
- C programs are written as source code, compiled, linked, and then run as executables.
mainis the starting point of a hosted C program.#include <stdio.h>is needed when using standard I/O functions such asprintf.gcc main.c -o maincompilesmain.cand names the executablemain.- Compiler errors and warnings are feedback that help you fix your program before running it.
