DEV Community

Yunus Emre Güzel
Yunus Emre Güzel

Posted on Edited on Originally published at blog.emreguzel.ca

Introduction to C programming

C programming is one of the best places to start learning how computers really work. It’s fast, it’s everywhere, and almost every modern language borrows from it. This introduction to C programming covers where C came from, why it still matters, and how to write your first programs with text, numbers and variables.

A Short History of C Programming

C was created by Dennis Ritchie at Bell Labs in the early 1970s. He needed a better language to rewrite the Unix operating system. Before C, programmers wrote systems in assembly language, which was hard to read and only worked on one type of machine.

C changed that, because it offered readable code while still giving full control over the hardware. Unix was rewritten in C, so it could move to new computers easily. By the 1980s, C had become the standard language for serious software.

Why We Still Use C

The main reason is speed. C compiles straight into machine code, so it doesn’t need a heavy runtime or virtual machine. It also gives you direct access to memory, so you can manage hardware precisely. And it’s portable: code written on Windows usually runs on Linux with very few changes.

That’s why C sits at the core of Windows, Linux and macOS. It also runs embedded devices such as smartwatches, routers and car computers, where memory is tight and speed matters.

C is also called the mother of modern languages. C++, C# and Objective-C grew directly out of it, while Java, JavaScript and PHP copy much of its syntax. So when you learn C, the basic shape of most other languages starts to look familiar.

Set Up VS Code for C Programming

To write C in VS Code, you need a C compiler such as GCC installed on your computer. Then install the Run GCC extension by MushroomFX. It lets you compile and run a C file with one click, instead of typing commands in the terminal.

Run GCC extension by MushroomFX in the VS Code Extensions marketplace

With the extension installed, open a file such as main.c with this code and click the Play button:

#include <stdio.h>

int main(){
    printf("Hello, World!\n");
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

VS Code terminal showing Hello, World! output from a C program

Your First C Program: Hello World

Printing “Hello, World!” is the traditional first program in any language. It’s short, but it checks that your compiler works and shows the basic structure every C program needs:

  • #include <stdio.h> loads the standard input/output library, which provides printf.

  • int main() is where every C program starts. The operating system looks for it first.

  • printf("Hello, World!\n"); prints the text. \n moves to a new line, and the semicolon ends the statement.

  • return 0; ends the program and tells the system that everything went well.

#include <stdio.h>

int main(){
    printf("Hello, World!\n");
    return 0;
}
Enter fullscreen mode Exit fullscreen mode
Compile and run from the terminal

If you prefer the terminal, you can also compile the file yourself:

gcc .\main.c -o main.exe
Enter fullscreen mode Exit fullscreen mode
  • gcc is the GNU Compiler Collection, which turns C code into machine code.

  • .\main.c is your source file in the current folder.

  • -o main.exe sets the name of the program it creates.

Then run the program:

./main.exe
Enter fullscreen mode Exit fullscreen mode

The ./ tells the terminal to look in the current folder. Terminals ask for this on purpose, so they never run a program with the same name from somewhere else by mistake.

Running main.exe in PowerShell prints Hello, World!

Print more than one line

To print more, add more printf lines. Each one ends with \n, so the next line starts underneath:

#include <stdio.h>

int main(){
    printf( "Emre Guzel\n");
    printf("Favorite place in the world?\n");
    printf("Istanbul\n");
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

C program output printing a name and a favorite place using printf

You can also print an empty line with printf("\n");, which makes the output easier to read:

#include <stdio.h>

int main(){
    printf( "Favorite car? \n");
    printf("Mercedes Maybach S class s 500 \n");
    printf("\n");
    printf("Favorite Resturan\n");
    printf("Nusret\n");
    printf("\n");
    printf("Favorite food\n");
    printf("Adana Kebab \n");
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

C printf output listing favorite car, restaurant and food on separate lines

Doing Maths in C Programming

C can calculate too. Here printf works out 1 + 1 and prints the answer:

#include <stdio.h>

int main(){
    printf( "1 + 1 = %d\n", 1 + 1 );
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

%d is a placeholder for a whole number. When the program runs, C works out 1 + 1 and puts the result where %d is:

C program output showing integer addition: 1 + 1 = 2

Multiplication uses *:

#include <stdio.h>

int main(){
    printf( "100 X 100 = %d\n", 100 * 100 );
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

C program output showing integer multiplication: 100 X 100 = 10000

Division uses /:

#include <stdio.h>

int main(){
    printf( "500 / 5 = %d\n", 500 / 5 );
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

C program output showing integer division: 500 / 5 = 100

Variables and Whole Numbers (int)

A variable is a named box in memory that holds a value. Instead of remembering a memory address, you use the name. For example, a game might keep the score in a variable called playerScore and update it every time you earn points.

An int holds a whole number, such as 42, -100 or 0. It can’t hold 3.14, because that needs a decimal type. On most computers an int uses 32 bits, so it fits numbers from about -2.1 billion to 2.1 billion.

Here two int variables are added together:

#include <stdio.h>
int main(){
    int num1 = 100;
    int num2 = 200;
    printf("100 + 200 is equal to : %d\n", num1 + num2);
}
Enter fullscreen mode Exit fullscreen mode

C program adding two int variables and printing 100 + 200 = 300

The same works for division. Note that dividing two ints gives a whole number, so 7 / 2 would give 3:

#include <stdio.h>
int main(){
    int num1 = 500;
    int num2 = 2;
    int result = num1 / num2;
    printf("500 / 2 is equal to : %d\n", result);
}
Enter fullscreen mode Exit fullscreen mode
Practice: area of a rectangle

The area of a rectangle is length × width. With variables, the formula reads almost like maths:

#include <stdio.h>
//Area of rectangle
//a= lw
int main(){
    int length = 5;
    int width = 4;
    int result = length * width;
    printf("Area of the rectangle is: %d %s\n", result, "mm²");
}
Enter fullscreen mode Exit fullscreen mode

C program output: area of a rectangle calculated with variables

Decimal Numbers (float)

A float holds a number with a decimal point, such as 3.14, -0.001 or 0.5. Use int when you count things, like 5 apples. Use float when you measure them, like 1.75 metres or $9.99.

The name comes from how computers store these numbers: the decimal point can “float”, a bit like scientific notation. That’s how a float can hold both very large and very small values.

#include <stdio.h>
int main(){
    float num1 = 9.9;
    float num2 = 10.4;
    float result = num1 + num2;
    printf("9.9 + 10.4 is equal to: %.2f\n", result);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

%.2f prints a float with exactly two decimals, so the sum shows as 20.30:

C float addition output: 9.9 + 10.4 printed as 20.30

Practice: circumference of a circle

The circumference is 2πr. Since 2π never changes, the code stores it as a constant:

#include <stdio.h>
// Circumference of circle 
//C= 2πr 
int main(){
    const float TAU = 6.28; //3.14 * 2 = 6.28
    float radius = 65.78;
    float Circumference = radius * TAU;
    printf("Circumference of the circle is: %.3f\n", Circumference);
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

const stops any other part of the program from changing the value, and the capital letters tell other programmers it’s a constant. Then %.3f prints the result with three decimals:

C program output: circumference of a circle calculated with a float

Practice: area of a triangle

The area of a triangle is base × height ÷ 2:

#include <stdio.h>
// Area of triangle  
// A = 1/2 * b * h 
int main(){
    float base = 140.3;
    float height = 9.9;
    float result =  base * height / 2;
    printf("Area of the triangle is: %.2f\n", result);
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

C program output: area of a triangle printed with two decimal places

Text in C Programming: Strings

A string is text, wrapped in double quotes. C has no separate string type. Instead, a string is an array of characters that ends with a hidden null character, \0, which marks where the text stops.

Strings are how programs talk to people. If you store 42 as an int, you can add to it. If you store “42” as a string, it’s just text to display, like the word “apple”.


#include <stdio.h>
int main(){
    char name[] = "Emre Guzel";
    char favorite_place[] = "Istanbul";
    printf("My name is: %s\n", name);
    printf("My favorite place in the world is: %s\n", favorite_place);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode
  • char name[] = "Emre Guzel"; creates a character array. The empty brackets let C work out the size, including the hidden null character.

  • %s is the placeholder for a string, so printf puts the name into the sentence.

C program printing string variables for a name and a favorite place

Once you know the pattern, you can store as many details as you like:

// about me program with variables 
#include <stdio.h>
int main(){
    char name[] = "Emre Guzel";
    char place[] = "Dubai";
    char favoriteRestaurant[] = "Aga Baba";
    char favoriteFood[] = "Roasted lamb";
    char favoriteCar[] = "Mercedes Maybach S class s 500";
    char favoriteCodingLanguage[] = "Python";
    printf("My name is: %s\n", name);
    printf("My favorite place in the world is: %s\n", place);
    printf("My favorite restaurant is: %s\n", favoriteRestaurant);
    printf("My favorite food is: %s\n", favoriteFood);
    printf("My favorite car is: %s\n", favoriteCar);
    printf("My favorite coding language is: %s\n", favoriteCodingLanguage);
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

C printf output using string variables for name, place, restaurant, food, car and language

A long string can even be split across two lines. C joins the two parts into one string automatically:


// Creating the question about favorite subjects, hobbies, video games, and cars
#include <stdio.h>
int main(){
    char favoriteSubjects[] = "Math, Coding";
    char favoriteHobby[] = "Coding, AI development, and hardware development with AI; finally, I like to play chess";
    char favoriteVideoGame[] = "GTA 5";
    char favoriteCar[] = "Mercedes Maybach S class S 500, and Mercedes G Wagon 4x4, "
                          "Rolls Royce Phantom, and BMW 7 series";
    printf("My favorite subject is: %s\n", favoriteSubjects);
    printf("My favorite hobby is: %s\n", favoriteHobby);
    printf("My favorite video game is: %s\n", favoriteVideoGame);
    printf("My favorite car is: %s\n", favoriteCar);
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

C printf output printing longer string variables for subject, hobby, game and cars

Project: Combine Strings, ints and floats

Now let’s mix all three types in one program:

// About me program
#include <stdio.h>
int main(){
    char Name[] = "Alexhander";
    int age = 25;
    float monthsOld = 25 * 12;
    printf("My name is: %s\n", Name);
    printf("I am %d years old.\n", age);
    printf("I am %.1f months old.\n", monthsOld);
    return 0;
}

Enter fullscreen mode Exit fullscreen mode
  • %s prints the name, %d prints the age and %.1f prints the months with one decimal.

  • Because monthsOld is a float, the program prints 300.0 rather than 300.

C program output using char, int and float variables: name, age in years and months

Constants and bigger numbers

This version stores everything as constants and adds two bigger number types:

// Succes and about me program
#include <stdio.h>

int main() {
    // Personal preferences
    const char FAVORITE_CAR[] = "Rolls Royce Cullinan";
    const char FAVORITE_FOOD[] = "Shawarma";
    const int FAVORITE_NUMBER = 9;

    // Performance and financial metrics
    const float SUCCESS_RATE = 99.9f;
    const long long COMPANY_NET_WORTH = 10000000000000000LL;
    const double PERSONAL_NET_WORTH = 999999999.999;

    // Displaying the data
    printf("My favorite car is: %s\n", FAVORITE_CAR);
    printf("My favorite food is: %s\n", FAVORITE_FOOD);
    printf("My favorite number is: %d\n", FAVORITE_NUMBER);

    // Output formatting includes relevant symbols (%, $) for readability
    printf("My success rate is: %.1f%%\n", SUCCESS_RATE);
    printf("My company's net worth is: $%lld\n", COMPANY_NET_WORTH);
    printf("My net worth is: $%.2f\n", PERSONAL_NET_WORTH);

    return 0;
}



Enter fullscreen mode Exit fullscreen mode
  • Every variable uses const, so none of them can change by accident. Constant names are written in UPPER_SNAKE_CASE by convention.

  • The f in 99.9f marks a float, while LL marks a long long, which holds numbers far bigger than an int.

  • A double is like a float with more precision, so it can store a long number without rounding it.

  • %lld prints a long long, and %% prints a real percent sign.

C printf format specifiers output with integers, percentages and dollar amounts

Practice: job title and salary
// Job title and salary program
#include <stdio.h>
int main() {
    const char JOB_TITLE[] = "Software Engineer";
    const char COMPANY_NAME[] = "OpenAI";
    const float SALARY = 2500000.00;

    printf("My job title is: %s\n", JOB_TITLE);
    printf("I work at: %s\n", COMPANY_NAME);
    printf("My annual salary is: $%.2f\n", SALARY);

    return 0;
}

Enter fullscreen mode Exit fullscreen mode

The salary uses %.2f, so it prints with cents. Strictly speaking, 2500000.00 is a double in C, so writing 2500000.00f would avoid a conversion warning on some compilers.

C program printing a job title, company and salary using string and double variables

Next Steps in C Programming

You’ve now written C programs that print text, do maths and store ints, floats and strings. The next step is to let your programs react to the person using them.

Next up: User Input in C: A Complete Guide for Beginners, where your programs start reading what the user types. For a full reference of the standard library, see cppreference.

What was the first C program you got running? Tell me in the comments, or ask if you got stuck.


Originally published on my blog. I post new C, Python and AI-agent guides there regularly.


More from my lab: IboChat — talk to AI characters by voice in 24+ languages (AI parodies, not real people) — and free web tools on emreguzel.ca. Tutorials like this one are published first on my Coding Blog.

Top comments (0)