C Structures

Group related data with struct.

✍️ 2 exercises in this lesson

A struct groups variables of different types under one name.

  • Define: struct Student { char name[30]; int marks; };
  • Access members with a dot: s.marks.
  • Through a pointer, use an arrow: p->marks.
  • typedef creates a shorter type name.

Key Takeaways: C Structures

Mastering C Structures helps you write clean and reliable C code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

C Structures Code Example

C
#include <stdio.h>
#include <string.h>

typedef struct {
    char name[30];
    int marks;
} Student;

void bonus(Student *s) { s->marks += 5; }

int main() {
    Student list[] = {{"Ali", 81}, {"Sara", 94}};
    strcpy(list[0].name, "Ali Khan");
    bonus(&list[0]);

    for (int i = 0; i < 2; i++) {
        printf("%s: %d\n", list[i].name, list[i].marks);
    }
    return 0;
}
Output
Ali Khan: 86
Sara: 94
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

C Structures Practice Exercises

Check what you learned in “C Structures”.

/ 2 solved
Fill in the blank

Access a member through a pointer to a struct.

pmarks = 90;
✓ Correct! Not quite. Try again.
Multiple choice

Which keyword creates a shorter name for a type?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services