To use a struct in C, you first declare it. That means you write struct with a name and list its fields inside braces. Then you create a variable of that type and give each field a value, either all at once with { } or one field at a time. You read any field back with a dot, like s1.marks.
In the last post I talked about why the plain C struct matters so much, and how the Linux kernel is built out of structs. This time we write one ourselves. Nothing fancy. We will declare a struct, put values in it and read them back. Then we will look at a tiny struct the Linux kernel uses to keep track of the time, and see that it works the same way.
How do you declare a struct in C?
A struct declaration describes the shape of your data. It says what fields there are and what type each one has.
struct student { int roll_no; char name[50]; float marks; }; A few small things to notice here. The word after struct is the tag, here student. Together, struct student is the name of your new type. Each line inside the braces is one field, also called a member. And there is a semicolon after the closing brace. Beginners forget that one a lot, and the compiler error you get is not always clear.
This code does not create any student yet. It only tells the compiler what a student looks like. Think of it as a form with empty boxes. No memory is used for data until you make a variable.
How do you fill a struct with values?
Now we make a variable of that type and put values into it. There are two common ways.
The first way is to give all the values when you create the variable. The values go inside braces, in the same order as the fields.
struct student s1 = { 1, "Asha", 87.5f }; So roll_no gets 1, name gets "Asha" and marks gets 87.5. If the order is wrong, C will not always complain. It just puts the values in the wrong boxes, so be careful.
The second way is to create the variable first and set each field on its own line. Starting with = { 0 } sets every field to zero, so nothing holds garbage while you fill it.
struct student s2 = { 0 }; s2.roll_no = 2; strcpy(s2.name, "Ravi"); s2.marks = 91.0f; Notice the strcpy for the name. You cannot write s2.name = "Ravi"; because name is an array, and C does not let you assign to an array after it is created. Inside the { } at creation time it is fine, which is why s1 worked.
There is a third style, where you name each field inside the braces. The Linux kernel uses it all over the place. It gets its own post later in this series, so I will leave it for now.
How do you read a struct field? The dot operator
To get a value out, you write the variable name, a dot, then the field name. s1.name is Asha's name. s1.marks is her marks. You can use these anywhere you would use a normal variable. You can print them, or do math with them just like any other number.
You may have seen the arrow, ->, in kernel code. That is for when you have a pointer to a struct instead of the struct itself. We will get to it a few posts from now. For today, everything is a plain variable, so the dot is all we need.
A complete C example you can run
Here is everything together in one program.
#include <stdio.h> #include <string.h> /* Step 1: declare the struct type */ struct student { int roll_no; char name[50]; float marks; }; int main(void) { /* Step 2a: fill it when you create it */ struct student s1 = { 1, "Asha", 87.5f }; /* Step 2b: or create it empty and fill it field by field */ struct student s2 = { 0 }; s2.roll_no = 2; strcpy(s2.name, "Ravi"); s2.marks = 91.0f; /* Step 3: read the fields with the dot operator */ printf("%d %s %.1f\n", s1.roll_no, s1.name, s1.marks); printf("%d %s %.1f\n", s2.roll_no, s2.name, s2.marks); /* You can change a field later too */ s1.marks = s1.marks + 2.5f; printf("%s now has %.1f\n", s1.name, s1.marks); return 0; } Save it as student.c, then compile and run it:
gcc student.c -o student ./student On Windows with MinGW installed, the second line in PowerShell is .\student.exe.
Output:
1 Asha 87.5 2 Ravi 91.0 Asha now has 90.0 That is the whole idea. Declare the shape once. Make as many variables of that type as you like. Each one keeps its own copy of the fields, so changing s1.marks does not touch s2.
A real Linux kernel struct: timespec
Now let us look at the Linux kernel. One of the simplest structs in it is struct timespec. The kernel keeps the current date and time in a variable called xtime, and xtime is a struct timespec. It has just two fields.
/* Simplified from the Linux kernel */ struct timespec { long tv_sec; /* seconds since midnight, 1 January 1970 (UTC) */ long tv_nsec; /* nanoseconds within the current second, 0 to 999,999,999 */ }; That is a struct declaration, exactly like our student. Two fields that belong together, under one name. A time on its own in seconds is not precise enough, and nanoseconds on their own would overflow fast. Together they describe one moment.
The kernel fills xtime when the machine boots, by reading the seconds from the hardware clock. After that it keeps updating it as the timer ticks. On a typical setup, each tick adds 1,000,000 nanoseconds to xtime.tv_nsec. When tv_nsec goes past 999,999,999, a full second has passed, so the kernel bumps tv_sec by one and brings tv_nsec back down.
Look at that sentence again. xtime.tv_nsec. That is the same dot we used for s1.marks.
How the Linux kernel updates a struct field: try it yourself
We can copy that logic in a small program. I named the struct my_timespec so it does not clash with the real struct timespec that your system headers already define.
#include <stdio.h> /* Same two fields as the kernel's struct timespec */ struct my_timespec { long tv_sec; /* seconds since 1 Jan 1970 (UTC) */ long tv_nsec; /* nanoseconds inside the current second */ }; int main(void) { /* Fill it: almost at the end of a second */ struct my_timespec now = { 1700000000, 999500000 }; printf("before: %ld s, %ld ns\n", now.tv_sec, now.tv_nsec); /* One tick adds 1,000,000 ns (one millisecond) */ now.tv_nsec = now.tv_nsec + 1000000; /* If nanoseconds go past one full second, carry into seconds */ if (now.tv_nsec > 999999999) { now.tv_nsec = now.tv_nsec - 1000000000; now.tv_sec = now.tv_sec + 1; } printf("after: %ld s, %ld ns\n", now.tv_sec, now.tv_nsec); return 0; } Output:
before: 1700000000 s, 999500000 ns after: 1700000001 s, 500000 ns We started half a millisecond before the end of a second. One tick pushed tv_nsec past a full second, so the code carried one second over into tv_sec. The real kernel code has more going on, like locking so that two CPUs do not update the time at once. But the struct part is just this: read a field with a dot, change it, write it back.
Filling a struct the Linux kernel reads: struct flock
Here is one more, and this time you fill a struct and the Linux kernel reads it. When a program wants to lock part of a file, it fills a struct flock and hands it to the fcntl() system call. The kernel then reads the fields to find out what you asked for. You can use this one directly on Linux, because it comes from <fcntl.h>.
#include <stdio.h> #include <fcntl.h> int main(void) { struct flock lock = { 0 }; /* start with every field at zero */ lock.l_type = F_WRLCK; /* I want a write lock */ lock.l_whence = SEEK_SET; /* count from the start of the file */ lock.l_start = 0; /* begin at byte 0 */ lock.l_len = 100; /* lock 100 bytes */ printf("type=%d whence=%d start=%ld len=%ld\n", lock.l_type, lock.l_whence, (long)lock.l_start, (long)lock.l_len); /* In a real program you would now call: fcntl(fd, F_SETLK, &lock); and the kernel reads these same fields. */ return 0; } Output on Linux:
type=1 whence=0 start=0 len=100 F_WRLCK happens to be 1 and SEEK_SET is 0 on Linux, which is why you see those numbers. The point is that a struct is how your program and the kernel agree on a message. You put values in the fields, and the other side reads the same fields by name.
The &lock in that comment means "the address of lock". It has to do with pointers, which we will cover properly a bit later.
What comes next
We now know how to declare a struct, fill it and read it. You probably noticed that writing struct student every time is a bit long. In the next post we will look at typedef struct, which lets you give your struct a short name, and why the Linux kernel uses it for some types but avoids it for most.
If you want to practice this with small exercises that check your code as you go, the C programming course at Jupius Learning starts from exactly here.
Frequently asked questions
How do you initialize a struct in C?
You can put the values in braces when you create the variable, in the same order as the fields: struct student s1 = { 1, "Asha", 87.5f };. You can also start with = { 0 } to set every field to zero, then assign each field with a dot. Any field you leave out of the braces is set to zero.
What is the difference between declaring a struct and creating a struct variable?
Declaring the struct, like struct student { ... };, only describes its fields. It does not store any data. Creating a variable, like struct student s1;, sets aside memory for one student with its own copy of every field. You declare the type once and can create as many variables of it as you need.
What does the dot operator do in C?
The dot reads or changes one field of a struct variable. s1.marks gives you the marks field of s1, and s1.marks = 90.0f; changes it. If you have a pointer to a struct instead of the struct itself, you use the arrow -> instead.
Can a C struct hold different data types?
Yes. That is the main reason structs exist. One struct can mix int, float, char arrays, pointers and even other structs. Our student struct holds an int, a char array and a float side by side.
How does the Linux kernel use structs?
The Linux kernel uses structs to describe almost everything it manages. Small ones like struct timespec hold a single value, such as the current time. Big ones like task_struct describe a whole running program. The kernel reads and writes their fields with the same dot and arrow operators you use in your own C code.
Originally published on Jupius Learning.


