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 ; } ;
Enter fullscreen mode
Exit fullscreen mode
A few small things to notice here. The word after struct is the tag, here student .…