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
Input and Output in C
Input and output operations are essential for interactive programs. C provides several functions for reading input from users and displaying output.
Output Functions
printf() Function
Used to display formatted output to the screen.
#include <stdio.h>
int main() {
int age = 25;
float height = 5.9;
char grade = 'A';
printf("Hello, World!\n");
printf("Age: %d\n", age);
printf("Height: %.1f feet\n", height);
printf("Grade: %c\n", grade);
printf("Multiple values: %d, %.2f, %c\n", age, height, grade);
return 0;
}Input Functions
scanf() Function
Used to read formatted input from the user.
#include <stdio.h>
int main() {
int age;
float salary;
char grade;
printf("Enter your age: ");
scanf("%d", &age);
printf("Enter your salary: ");
scanf("%f", &salary);
printf("Enter your grade: ");
scanf(" %c", &grade); // Note the space before %c
printf("\nYou entered:\n");
printf("Age: %d\n", age);
printf("Salary: %.2f\n", salary);
printf("Grade: %c\n", grade);
return 0;
}Format Specifiers
| Specifier | Data Type | Example |
|---|---|---|
%d | int | printf("%d", 42); |
%f | float | printf("%.2f", 3.14); |
%c | char | printf("%c", 'A'); |
%s | string | printf("%s", "Hello"); |
%lf | double | scanf("%lf", &num); |
Important
Always use the address operator (&) with scanf() for variables, except for strings.
Explanation
- printf: Prints formatted output using format specifiers like
%d,%f,%s. - scanf: Reads user input; pass variable addresses (e.g.,
&age) except for strings, which are arrays. - Format specifiers: Must match the variable type to avoid undefined behavior or crashes.
- Whitespace: When reading
char, use a leading space in" %c"to eat leftover newlines.
Keywords
Quick Tips
- Validate
scanfreturn value to ensure inputs were read successfully. - Use
fgetsfor safer string input and parse withsscanf. - Prefer explicit widths (e.g.,
%.2f) to format numeric output neatly.