Chapter 9: Structures and Enums
Learn about structures and enumerations in C.
Chapter 9: Structures and Enums
Table of Contents
- Table of Contents
- 9.1 Structures
- 9.2 Enumerations
- 9.3 Exercises
- 9.4 Solutions
- 9.5 What’s Coming in the Next Chapter
9.1 Structures
Structures group related variables:
1
2
3
4
struct Point {
int x;
int y;
};
9.2 Enumerations
Enums define named integer constants:
1
enum Color { RED, GREEN, BLUE };
9.3 Exercises
- Define a structure for a student with name and age.
- Write a function that takes a struct as a parameter.
- What is the default value of the first enum constant?
9.4 Solutions
9.4.1 Solution 1
1
2
3
4
struct Student {
char name[50];
int age;
};
9.4.2 Solution 2
1
2
3
void printStudent(struct Student s) {
printf("%s %d\n", s.name, s.age);
}
9.4.3 Solution 3
Zero (0).
9.5 What’s Coming in the Next Chapter
Next, you’ll learn about file I/O in C!
This post is licensed under CC BY 4.0 by the author.