Structures let you group different pieces of information together under one name. This helps keep related data organized.
Defining structures
Introduction
Syntax
C
struct StructureName {
dataType member1;
dataType member2;
// more members
};Use the keyword struct followed by a name and curly braces.
Each member inside has a type and a name, separated by semicolons.
Examples
Person with a name and age.C
struct Person {
char name[50];
int age;
};C
struct Point {
int x;
int y;
};C
struct Book {
char title[100];
char author[50];
int pages;
};Sample Program
This program defines a Person structure, creates a variable p1, assigns values, and prints them.
C
#include <stdio.h>
struct Person {
char name[50];
int age;
};
int main() {
struct Person p1;
// Assign values
snprintf(p1.name, sizeof(p1.name), "%s", "Alice");
p1.age = 30;
// Print values
printf("Name: %s\n", p1.name);
printf("Age: %d\n", p1.age);
return 0;
}Important Notes
Remember to use struct keyword when declaring variables unless you use typedef.
Strings in C are arrays of characters, so use char name[50]; for names.
Use functions like snprintf or strcpy to assign strings safely.
Summary
Structures group different data types under one name.
Use struct keyword to define them.
Access members with dot . operator on structure variables.
Practice
1. What is the purpose of using
struct in C programming?easy
Solution
Step 1: Understand the role of
Thestructstructkeyword is used to group variables of different types into a single unit.Step 2: Compare with other options
Options A, B, and C relate to memory allocation, loops, and functions, which are unrelated tostruct.Final Answer:
To group different data types under one name -> Option CQuick Check:
struct groups data types = D [OK]
Hint: Remember: struct bundles variables like a box [OK]
Common Mistakes:
- Confusing struct with loops or functions
- Thinking struct allocates memory dynamically
- Assuming struct defines a single data type only
2. Which of the following is the correct syntax to define a structure named
Person with members int age and char name[20]?easy
Solution
Step 1: Recall correct struct syntax
Structures are defined using curly braces { } with semicolons after each member.Step 2: Check each option
struct Person { int age; char name[20]; }; uses correct braces and semicolons. Options B, C, and D use incorrect symbols or missing braces.Final Answer:
struct Person { int age; char name[20]; }; -> Option AQuick Check:
Correct struct syntax uses braces and semicolons = A [OK]
Hint: Use braces { } and semicolons ; inside struct [OK]
Common Mistakes:
- Using parentheses instead of braces
- Missing semicolons after members
- Using equal sign or colon incorrectly
3. What will be the output of the following code?
struct Point { int x; int y; } p = {3, 4};
printf("%d %d", p.x, p.y);medium
Solution
Step 1: Understand struct initialization
The structPointis initialized withp = {3, 4}, sop.x = 3andp.y = 4.Step 2: Analyze printf output
The printf printsp.xandp.yin order, so output is "3 4".Final Answer:
3 4 -> Option BQuick Check:
p.x=3 and p.y=4 prints as "3 4" [OK]
Hint: Struct members print in order of initialization [OK]
Common Mistakes:
- Mixing order of members in output
- Assuming default zero values
- Thinking code causes compilation error
4. Identify the error in the following code snippet:
struct Car {
int speed;
char model[10];
};
struct Car c;
c.speed = 100;
c.model = "Sedan";
medium
Solution
Step 1: Check struct definition and declaration
The struct is correctly defined and variablecdeclared.Step 2: Analyze assignment to
Arrays cannot be assigned directly with = operator; string copy functions likec.modelstrcpyare needed.Final Answer:
Cannot assign string directly to array member -> Option AQuick Check:
Array assignment needs strcpy, not = [OK]
Hint: Use strcpy to assign strings to char arrays [OK]
Common Mistakes:
- Assigning string literals directly to char arrays
- Forgetting to include string.h for strcpy
- Assuming semicolon or declaration errors
5. Given the structure definition:
Which of the following code snippets correctly creates an
struct Employee {
int id;
char name[30];
float salary;
};Which of the following code snippets correctly creates an
Employee variable and initializes all members in one statement?hard
Solution
Step 1: Check initialization syntax for struct
Structs can be initialized with braces { } listing values in order of members.Step 2: Evaluate each option
struct Employee e = {101, "Alice", 75000.0}; uses correct brace initialization. struct Employee e; e.id = 101; e.name = "Alice"; e.salary = 75000.0; tries to assign string directly to array (invalid). struct Employee e = (101, "Alice", 75000.0); uses parentheses incorrectly. struct Employee e = {id:101, name:"Alice", salary:75000.0}; uses named initialization which is not standard in C.Final Answer:
struct Employee e = {101, "Alice", 75000.0}; -> Option DQuick Check:
Brace list initialization is correct for structs [OK]
Hint: Use braces with values in order to initialize structs [OK]
Common Mistakes:
- Assigning strings directly to arrays after declaration
- Using parentheses instead of braces
- Trying named member initialization (not standard C)
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
