A C struct groups related values that may have different data types. For example, a student record can contain an integer ID, a character array for the name and a floating-point mark in one object.
#include <stdio.h>
struct Student {
int id;
char name[50];
float mark;
};
int main(void) {
struct Student student = {1, "Ravi", 88.5f};
printf("ID: %d\n", student.id);
printf("Name: %s\n", student.name);
printf("Mark: %.1f\n", student.mark);
return 0;
}
Student is the structure tag. student is a variable whose type is struct Student.
student.id
student.name
student.mark
Use the dot operator when you have a structure object directly.
#include <stdio.h>
#include <string.h>
struct Student {
int id;
char name[50];
float mark;
};
int main(void) {
struct Student student;
printf("Enter ID: ");
if (scanf("%d", &student.id) != 1) {
return 1;
}
int ch;
while ((ch = getchar()) != '\n' && ch != EOF) {
/* discard the rest of the line */
}
printf("Enter name: ");
if (fgets(student.name, sizeof student.name, stdin) == NULL) {
return 1;
}
student.name[strcspn(student.name, "\n")] = '\0';
printf("Enter mark: ");
if (scanf("%f", &student.mark) != 1) {
return 1;
}
printf("\nID: %d\n", student.id);
printf("Name: %s\n", student.name);
printf("Mark: %.1f\n", student.mark);
return 0;
}
Using fgets() for the name allows spaces and keeps the input bounded by the array size.
#include <stdio.h>
struct Student {
int id;
char name[50];
float mark;
};
int main(void) {
struct Student students[] = {
{1, "Ronn", 5.6f},
{2, "Geek", 7.3f}
};
size_t count = sizeof students / sizeof students[0];
for (size_t i = 0; i < count; i++) {
printf("ID: %d, Name: %s, Mark: %.1f\n",
students[i].id,
students[i].name,
students[i].mark);
}
return 0;
}
Structures of the same type can be assigned directly. You do not need to copy each member separately.
#include <stdio.h>
struct Student {
int id;
char name[50];
float mark;
};
int main(void) {
struct Student source = {5, "Ronn", 12.5f};
struct Student copy = source;
copy.id++;
copy.mark -= 0.1f;
printf("ID: %d\n", copy.id);
printf("Name: %s\n", copy.name);
printf("Mark: %.1f\n", copy.mark);
return 0;
}
#include <stdio.h>
struct Address {
char city[30];
int pin;
};
struct Employee {
char name[50];
struct Address address;
int salary;
};
int main(void) {
struct Employee employee = {
"John Doe",
{"New York", 10001},
50000
};
printf("Employee: %s\n", employee.name);
printf("City: %s\n", employee.address.city);
printf("PIN: %d\n", employee.address.pin);
return 0;
}
#include <stdio.h>
struct Employee {
char name[50];
int salary;
};
int main(void) {
struct Employee employee = {"John Doe", 50000};
struct Employee *ptr = &employee;
printf("Employee: %s\n", ptr->name);
printf("Salary: %d\n", ptr->salary);
return 0;
}
ptr->name is equivalent to (*ptr).name. Use -> when accessing a member through a pointer to a structure.
#include <stdio.h>
struct Student {
int id;
char name[50];
float mark;
};
void display_student(const struct Student *student) {
printf("%d | %s | %.1f\n",
student->id,
student->name,
student->mark);
}
int main(void) {
struct Student student = {7, "Maya", 91.0f};
display_student(&student);
return 0;
}
Passing a pointer avoids copying the whole structure. Adding const makes it clear that the function only reads the record.
The old structure-csv.php URL redirects to this page, so the CSV/structure example is now kept here instead of leaving that intent behind a redirect.
For a simple CSV file such as:
1,Ravi,88.5
2,Maya,91.0
#include <stdio.h>
struct Student {
int id;
char name[50];
float mark;
};
int main(void) {
FILE *file = fopen("students.csv", "r");
char line[128];
struct Student student;
if (file == NULL) {
perror("students.csv");
return 1;
}
while (fgets(line, sizeof line, file) != NULL) {
if (sscanf(
line,
"%d,%49[^,],%f",
&student.id,
student.name,
&student.mark
) == 3) {
printf("%d | %s | %.1f\n",
student.id,
student.name,
student.mark);
}
}
fclose(file);
return 0;
}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.