Moortiii

Basic Structures in C

Oct 9th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. struct Student {
  5.     long int student_id;
  6.     long int student_age;
  7.     char student_name[255];
  8. };
  9.  
  10. void createStudent( struct Student *my_student );
  11. void printStudent( struct Student *student );
  12.  
  13. int main(){
  14.     struct Student Student_1;
  15.     struct Student Student_2;
  16.  
  17.     createStudent( &Student_1 );
  18.     createStudent( &Student_2 );
  19.  
  20.     printStudent( &Student_1 );
  21.     printStudent( &Student_2 );
  22. }
  23.  
  24. void printStudent( struct Student *student ) {
  25.     printf("Student name: %s", student->student_name);
  26.     printf("Student age: %li\n", student->student_age);
  27.     printf("Student id: %li\n", student->student_id);
  28.     printf("\n");
  29. }
  30.  
  31. void createStudent (struct Student *my_student) {
  32.     fflush(stdin);
  33.     char name[255];
  34.     long int age;
  35.     long int id;
  36.     printf("Please enter a student name:\n\n");
  37.     fgets(name, sizeof(name), stdin);
  38.  
  39.     printf("Please enter a student id:\n\n");
  40.     scanf(" %li", &id);
  41.  
  42.     printf("Please enter a student age:\n\n");
  43.     scanf(" %li", &age);
  44.  
  45.     strcpy(my_student->student_name, name);
  46.     my_student->student_id = age;
  47.     my_student->student_age = id;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment