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
Structures in C
Structures allow you to group related data of different types under a single name. They are user-defined data types that help organize complex data.
Structure Declaration and Definition
#include <stdio.h>
#include <string.h>
// Structure declaration
struct Student {
int id;
char name[50];
float gpa;
int age;
};
int main() {
// Structure variable declaration
struct Student student1;
// Assigning values
student1.id = 101;
strcpy(student1.name, "Alice Johnson");
student1.gpa = 3.85;
student1.age = 20;
// Displaying values
printf("Student Information:\n");
printf("ID: %d\n", student1.id);
printf("Name: %s\n", student1.name);
printf("GPA: %.2f\n", student1.gpa);
printf("Age: %d\n", student1.age);
return 0;
}Structure Initialization
#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
// Method 1: Initialize during declaration
struct Point p1 = {10, 20};
// Method 2: Designated initializers
struct Point p2 = {.x = 5, .y = 15};
// Method 3: Partial initialization
struct Point p3 = {30}; // y will be 0
printf("Point 1: (%d, %d)\n", p1.x, p1.y);
printf("Point 2: (%d, %d)\n", p2.x, p2.y);
printf("Point 3: (%d, %d)\n", p3.x, p3.y);
return 0;
}Array of Structures
#include <stdio.h>
#include <string.h>
struct Employee {
int id;
char name[30];
float salary;
};
int main() {
struct Employee employees[3] = {
{1, "John Doe", 50000.0},
{2, "Jane Smith", 55000.0},
{3, "Bob Wilson", 48000.0}
};
printf("Employee Records:\n");
printf("%-5s %-15s %-10s\n", "ID", "Name", "Salary");
printf("--------------------------------\n");
for (int i = 0; i < 3; i++) {
printf("%-5d %-15s $%.2f\n",
employees[i].id,
employees[i].name,
employees[i].salary);
}
return 0;
}Nested Structures
#include <stdio.h>
#include <string.h>
struct Address {
char street[50];
char city[30];
int zipCode;
};
struct Person {
char name[50];
int age;
struct Address address; // Nested structure
};
int main() {
struct Person person;
strcpy(person.name, "Alice Cooper");
person.age = 28;
strcpy(person.address.street, "123 Main St");
strcpy(person.address.city, "New York");
person.address.zipCode = 10001;
printf("Person Information:\n");
printf("Name: %s\n", person.name);
printf("Age: %d\n", person.age);
printf("Address: %s, %s %d\n",
person.address.street,
person.address.city,
person.address.zipCode);
return 0;
}Key Points:
- Use . (dot operator) to access structure members
- Structures can contain arrays, pointers, and other structures
- Memory is allocated for all members together
- Structure assignment copies all members