C Tutorial — Learn C Programming
Key takeaway: This free C tutorial teaches you systems programming from scratch — variables, pointers, arrays, functions, structs, and memory management — with practical code examples you can compile and run.
C is a general-purpose, procedural programming language created in 1972. It is used for operating systems, embedded systems, game engines, and performance-critical applications. This tutorial is for beginners, CS students, and developers who want to understand how computers work at a low level.
Last updated: September 2026
Variables and Constants in C
Variables are storage locations with an associated name that can contain data. Constants are fixed values that cannot be changed during program execution.
Variables
A variable is a name given to a memory location where data is stored. Variables must be declared before use.
#include <stdio.h>
int main() {
// Variable declaration
int age;
float salary;
char grade;
// Variable initialization
age = 25;
salary = 50000.50;
grade = 'A';
// Declaration and initialization together
int count = 10;
double pi = 3.14159;
printf("Age: %d\n", age);
printf("Salary: %.2f\n", salary);
printf("Grade: %c\n", grade);
return 0;
}Variable Naming Rules
Good Practice
- Use descriptive names:
studentAgeinstead ofa - Use camelCase:
firstName - Use underscores:
first_name
Avoid
- Single letter names (except for loops)
- Starting with numbers:
2name - Using keywords:
int,float
Constants
Constants are fixed values that cannot be modified during program execution. There are several ways to define constants in C:
#include <stdio.h>
#define PI 3.14159 // Preprocessor constant
int main() {
// const keyword
const int MAX_SIZE = 100;
const float GRAVITY = 9.8;
// Literal constants
int number = 42; // Integer literal
float price = 19.99; // Float literal
char letter = 'X'; // Character literal
char name[] = "John"; // String literal
printf("PI: %.5f\n", PI);
printf("Max Size: %d\n", MAX_SIZE);
printf("Gravity: %.1f\n", GRAVITY);
return 0;
}Tip
Use #define for compile-time constants and const for runtime constants. Constants are typically written in UPPERCASE.
Explanation
- Variables: Named memory locations with a specific type; declare before use and initialize to avoid undefined values.
- Constants: Values that do not change; use
constfor typed, scoped constants and#definefor macros. - Why it matters: Clear variable names and appropriate constants make code safer and easier to maintain.
- Common pitfalls: Modifying a
constvalue, mixing#definemacros with typed constants, or using vague names.
Keywords
Quick Tips
- Prefer
constfor type safety and scope; reserve#definefor simple symbolic values. - Use UPPERCASE names for macros (e.g.,
MAX_SIZE) to distinguish from variables. - Initialize variables at declaration when possible to avoid undefined behavior.