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.
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;
}
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 providesprintf.int main()is where every C program starts. The operating system looks for it first.printf("Hello, World!\n");prints the text.\nmoves 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;
}
Compile and run from the terminal
If you prefer the terminal, you can also compile the file yourself:
gcc .\main.c -o main.exe
gccis the GNU Compiler Collection, which turns C code into machine code..\main.cis your source file in the current folder.-o main.exesets the name of the program it creates.
Then run the program:
./main.exe
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.
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;
}
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;
}
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;
}
%d is a placeholder for a whole number. When the program runs, C works out 1 + 1 and puts the result where %d is:
Multiplication uses *:
#include <stdio.h>
int main(){
printf( "100 X 100 = %d\n", 100 * 100 );
return 0;
}
Division uses /:
#include <stdio.h>
int main(){
printf( "500 / 5 = %d\n", 500 / 5 );
return 0;
}
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);
}
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);
}
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²");
}
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;
}
%.2f prints a float with exactly two decimals, so the sum shows 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;
}
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:
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;
}
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;
}
char name[] = "Emre Guzel";creates a character array. The empty brackets let C work out the size, including the hidden null character.%sis the placeholder for a string, soprintfputs the name into the sentence.
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;
}
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;
}
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;
}
%sprints the name,%dprints the age and%.1fprints the months with one decimal.Because
monthsOldis a float, the program prints 300.0 rather than 300.
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;
}
Every variable uses
const, so none of them can change by accident. Constant names are written in UPPER_SNAKE_CASE by convention.The
fin99.9fmarks a float, whileLLmarks along long, which holds numbers far bigger than an int.A
doubleis like a float with more precision, so it can store a long number without rounding it.%lldprints a long long, and%%prints a real percent sign.
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;
}
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.
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)