Below is the program to create Student I-Card using a Structure in C Programming with step-by-step explanation.
#include<stdio.h>
struct student {
char name[50];
int rollno;
char branch[20];
};
int main() {
struct student s;
printf("Enter name: ");
scanf("%s", s.name);
printf("Enter roll number: ");
scanf("%d", &s.rollno);
printf("Enter branch: ");
scanf("%s", s.branch);
printf("\nStudent I-Card\n");
printf("Name: %s\n", s.name);
printf("Roll Number: %d\n", s.rollno);
printf("Branch: %s\n", s.branch);
return 0;
}
Output-
Enter name: Sunita
Enter roll number: 1
Enter branch: IT
Student I-Card
Name: Sunita
Roll Number: 1
Branch: IT
Following is the Step-by-step explanation of the program:
- Firstly, we need to include the standard input-output library <stdio.h> which contains the functions for input and output operations.
- Then, we declare a struct named student which covered three members such as – name (string), rollno (integer), and branch (string). This structure will be used to store the student details.
- In the main() function, we declare a variable s of type struct student which will store the details of a single student.
- We use the printf() function to prompt the user to enter the student’s name, roll number, and branch.
- We use the scanf() function to read the user input and store it in the corresponding members of the s structure.
- Then we print the details of the student using the printf() function.
- Finally, we return 0 to indicate successful completion of the program.
The program prompts the user to enter the student’s details, which are then stored in the struct s. Then, the program prints the details of the student in a formatted manner. This program demonstrates the use of a struct to store related data and retrieve it later.
Once you have written the code, you can compile and run it using a C compiler, such as GCC, on your computer. The program will then display output on the screen when executed.
For more information about C programming join our course of C programming.