Using structures, write a simple declaration for students in a university.
Source Code
Brief explanation is provided after the source code.
#include typedef struct { char *mat_no[8]; char *first_name[45]; char *last_name[45]; char *email[45]; char *phone_number[9]; char *fac[3]; char *dept[4]; int level; } STUDENT; int main(int argc, char ** argv) { STUDENT stud; printf("Mat Number: "); scanf("%s", stud.mat_no); printf("\nFirst Name: "); scanf("%s", stud.first_name); printf("\nLast Name: "); scanf("%s", stud.last_name); printf("\nEmail: "); scanf("%s", stud.email); printf("\nPhone Number: "); scanf("%s", stud.phone_number); printf("\nFaculty: "); scanf("%s", stud.fac); printf("\nDepartment: "); scanf("%s", stud.dept); printf("\nLevel: "); scanf("%d", &stud.level); printf("\n"); printf("Mat Number: "); printf("%s", stud.mat_no); printf("\nFirst Name: "); printf("%s", stud.first_name); printf("\nLast Name: "); printf("%s", stud.last_name); printf("\nEmail: "); printf("%s", stud.email); printf("\nPhone Number: "); printf("%s", stud.phone_number); printf("\nFaculty: "); printf("%s", stud.fac); printf("\nDepartment: "); printf("%s", stud.dept); printf("\nLevel: "); printf("%d\n", stud.level); return 0; }
When you compile and execute the above program it produces the following result on Linux:
Mat Number: FE10A058 First Name: FOKWA Last Name: DIVINE Email: This email address is being protected from spambots. You need JavaScript enabled to view it. Phone Number: 6XXXXXXXX Faculty: FET Department: CENG Level: 4 Mat Number: FE10A058 First Name: FOKWA Last Name: DIVINE Email: This email address is being protected from spambots. You need JavaScript enabled to view it. Phone Number: 6XXXXXXXX Faculty: FET Department: CENG Level: 4
Brief Explanation
- A structure in C is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.
- Using typedef, STUDENT is created as a synonym for the structure.
- A variable stud of type STUDENT (the structure) is defined.
- scanf function reads data from input and assign to the fields define in the structure.
- Using printf function, the inputted data is displayed to the stdout screen.