C Language
1. Getting Started with CBeginner6 min read

Writing & Running Your First C Program

Anatomy of a C program: preprocessor directives, the main() entry point, statement terminators, return codes, and compiling from terminal.

Key Takeaways & Core Concepts
  • Every C program must contain exactly one main() function.
  • Statements in C must end with a semicolon (;).
  • Preprocessor directives begin with # and do NOT end with a semicolon.
  • Comments in C are written with // (single line) or /* ... */ (multi-line).
  • printf() outputs formatted text to stdout, defined in <stdio.h>.

Anatomy of a C Program

Let's dissect the standard C program line-by-line:

C Code Example
1#include <stdio.h>
2
3int main(void) {
4 printf("Hello, CodeZov Learner!\n");
5 return 0;
6}
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
6 lines
  1. 1
    `#include <stdio.h>`:
  • The # symbol designates a preprocessor directive.
  • include instructs the preprocessor to paste the header file stdio.h (Standard Input/Output) into this file before compiling.
  • stdio.h contains the function declarations for printf, scanf, getchar, and file operations.
  1. 2
    `int main(void)`:
  • The execution entry point of every C program. The OS begins executing instructions from main.
  • int specifies that this function returns an integer status code to the calling operating system shell.
  • void in the parameter list explicitly states that main takes no arguments.
  1. 3
    Curly Braces `{ ... }`:
  • Marks the start and end of a code block / function body.
  1. 4
    `printf(...);`:
  • Calls the library function to format and print text to standard output.
  • \n is an escape character representing a newline.
  • The semicolon ; marks the termination of the statement.
  1. 5
    `return 0;`:
  • Passes exit code 0 back to the host operating system, which conventionally signifies successful execution without errors. Non-zero values indicate error states.
Semicolon Rule in C

Unlike Python or JavaScript, C strictly requires semicolons after statements. Forgetting a semicolon is one of the most common syntax errors for beginners.

How to Compile & Run via Terminal

Save your code in a file named hello.c. Open your command prompt or bash terminal:

BASH Code Example
1# 1. Compile hello.c into an executable named hello
2gcc hello.c -o hello
3
4# 2. Run the compiled executable:
5# On Linux / macOS:
6./hello
7
8# On Windows (Command Prompt or PowerShell):
9hello.exe
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
9 lines

If you want the compiler to catch potential bugs and enforce strict ANSI/ISO standards, compile with helpful warning flags:

BASH Code Example
1gcc -Wall -Wextra -std=c11 hello.c -o hello
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
1 lines
Interactive Greeting Program with Variables
1#include <stdio.h>
2
3int main(void) {
4 int year = 2026;
5 char language[] = "C Language";
6
7 printf("Welcome to %s!\n", language);
8 printf("Year: %d\n", year);
9 printf("CodeZov makes mastering systems programming intuitive.\n");
10
11 return 0;
12}
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
12 lines
Note: %s is a placeholder (format specifier) for a string, and %d is for a decimal integer.
Practice & AssessmentC Programming Complete Course

Test Your Understanding

Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.

Your Current Score

0 / 2 correct(0 attempted)

1

What does returning 0 from main() signal to the operating system?

Beginner
2

Which header file is required to use printf() and scanf() in C?

Beginner