A structure in C is a user-defined data type that groups different variables under one name. These variables can be of different types and are called members.
Click to reveal answer
beginner
How do you define a structure in C?
You use the keyword struct followed by the structure name and curly braces containing member declarations. For example:
struct Point { int x; int y; };
Click to reveal answer
beginner
How do you create a variable of a structure type?
After defining a structure, you create variables by specifying the structure name. For example:
struct Point p1;
creates a variable p1 of type struct Point.
Click to reveal answer
beginner
How do you access members of a structure variable?
Use the dot operator . to access members. For example, p1.x = 10; sets the x member of p1.
Click to reveal answer
beginner
Can structures contain different data types as members?
Yes, structures can have members of different types, like int, float, char, or even other structures.
Click to reveal answer
Which keyword is used to define a structure in C?
Aclass
Btypedef
Cunion
Dstruct
✗ Incorrect
The keyword struct is used to define structures in C.
How do you access the member 'age' of a structure variable 'person'?
Aperson.age
Bperson->age
Cage.person
Dperson:age
✗ Incorrect
Use the dot operator: person.age to access members of a structure variable.
What is the correct way to declare a structure variable named 'car' of type 'struct Vehicle'?
Astruct Vehicle car;
Bstruct car Vehicle;
CVehicle car;
Dcar struct Vehicle;
✗ Incorrect
The correct syntax is struct Vehicle car;.
Can a structure contain members of different data types?
AOnly if they are all floats
BNo
CYes
DOnly if they are all integers
✗ Incorrect
Structures can contain members of different data types.
Which of the following is a valid structure definition?
Astruct Person [ int age; float height; ];
Bstruct Person { int age; float height; };
Cstruct Person ( int age; float height; );
Dstruct Person < int age; float height; >;
✗ Incorrect
Structures are defined using curly braces { }, like in option C.
Explain how to define a structure and create a variable of that structure in C.
Think about the syntax using 'struct' and how you name and use the structure.
You got /5 concepts.
Describe how you can use structures to group different types of data together.
Imagine a structure as a box holding different things related to one object.
You got /4 concepts.
Practice
(1/5)
1. What is the purpose of using struct in C programming?
easy
A. To allocate memory dynamically
B. To create a loop structure
C. To group different data types under one name
D. To define a function
Solution
Step 1: Understand the role of struct
The struct keyword 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 to struct.
Final Answer:
To group different data types under one name -> Option C
Quick 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
A. struct Person { int age; char name[20]; };
B. struct Person ( int age; char name[20]; );
C. struct Person = { int age; char name[20]; };
D. struct Person: int age; char name[20];
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 A
Quick 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
A. 0 0
B. 3 4
C. 4 3
D. Compilation error
Solution
Step 1: Understand struct initialization
The struct Point is initialized with p = {3, 4}, so p.x = 3 and p.y = 4.
Step 2: Analyze printf output
The printf prints p.x and p.y in order, so output is "3 4".
Final Answer:
3 4 -> Option B
Quick 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
A. Cannot assign string directly to array member
B. Missing semicolon after struct definition
C. Incorrect struct keyword usage
D. Variable c not declared
Solution
Step 1: Check struct definition and declaration
The struct is correctly defined and variable c declared.
Step 2: Analyze assignment to c.model
Arrays cannot be assigned directly with = operator; string copy functions like strcpy are needed.
Final Answer:
Cannot assign string directly to array member -> Option A
Quick 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:
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?
B. struct Employee e = {id:101, name:"Alice", salary:75000.0};
C. struct Employee e = (101, "Alice", 75000.0);
D. struct Employee e = {101, "Alice", 75000.0};
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 D
Quick 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 unknownstrongwhile earning +1 Grip toward your certificate.