Anna University exam prep
CS 3251 – Programming in C
Past years Questions and Answers – Part 4. (See Part 3 here: 2 marks Q-A on Unit 1. )
Here, 16 marks Q-A on struct (Unit 4) from Nov/Dec 2022. Pls see the images below.


CS3251 – Programming in C
16 Marks – Nov / Dec 2022, Unit 4, topic – structures in C
Question

What is a structure? Create a structure with data members of various types and declare two structure variables. Write a program to read data into these and print the same. Justify the need for structured data type.


Answer

A struct is a user-defined data type in C.
It groups together variables of different data types under a single name.

#include <stdio.h>
// Student data struct
struct Student
{
int rollNum; // Stores roll number
char name[20]; // Stores student name
char grade; // Stores grade
};
int main()
{
struct Student s1, s2; // Two variables of type struct Student
// Reading data into the struct variables
// Details of 1st student
printf("Enter details for 1st student:\n");
printf("Enter roll number: ");
scanf("%d", &s1.rollNum);
printf("Enter name: ");
scanf("%s", s1.name);
printf("Enter grade: ");
scanf(" %c", &s1.grade);
// Details of 2nd student
printf("Enter details for 2nd student:\n");
printf("Enter roll number: ");
scanf("%d", &s2.rollNum);
printf("Enter name: ");
scanf("%s", s2.name);
printf("Enter grade: ");
scanf(" %c", &s2.grade);
// Displaying the data from the struct variables
printf("\nStudent 1 details: \n");
printf("Roll number: %d\n", s1.rollNum);
printf("Name: %s\n", s1.name);
printf("Grade: %c\n", s1.grade);
printf("\nStudent 2 details: \n");
printf("Roll number: %d\n", s2.rollNum);
printf("Name: %s\n", s2.name);
printf("Grade: %c\n", s2.grade);
// End of program
return 0;
}

output:
Student 1 details:
Roll number: 12
Name: Suresh
Grade: A
Student 2 details:
Roll number: 23
Name: Ganesh
Grade: B

Need for Structured Data Type
  • Groups related data together under a single name. (Student’s name, roll number, and grade can be stored in a single variable.)
  • Represents real-world entities (which contain attributes of different types). Examples: student, employee, book, etc.
  • Makes programs organized and makes records easier to handle.

Tip:
Note the blank space before %c in the scanf() statement for reading the grade:
scanf(” %c”, &s1.grade);

The blank space before %c is necessary. Otherwise, we will get erratic behaviour.
Why so?

  • There is another scanf() statement before this one to read a string (name).
  • A newline character (\n) gets entered for the string scanf() call.
  • Then there is another scanf() call for reading the grade. If we don’t put a blank space before %c in the scanf() call, the newline character (\n) left in the input buffer will be read instead of the actual grade.

Part 5 of this series on Anna Univ Q-A is here: Loops in C, Unit 1, 16 marks, Nov/Dec 2022.

Posted in

2 responses to “Anna Univ Past Years Q-A, CS3251 Programming in C- Part 4, struct (Unit 4)”

  1. […] Anna Univ Past Years Q-A, CS3251 Programming in C- Part 4, struct (Unit 4) […]

    Like

  2. […] Anna Univ Past Years Q-A, CS3251 Programming in C- Part 4, struct (Unit 4) […]

    Like

Leave a comment

Is this your new site? Log in to activate admin features and dismiss this message
Log In