Moortiii icon

Basic Structures in C

Moortiii | PRO | 10/10/17 12:00:53 AM UTC | 0 ⭐ | 331 👁️ | Never ⏰ | []
C |

1.16 KB

|

None

|

0 👍

/

0 👎

#include <stdio.h>
#include <string.h>
 
struct Student {
    long int student_id;
    long int student_age;
    char student_name[255];
};
 
void createStudent( struct Student *my_student );
void printStudent( struct Student *student );
 
int main(){
    struct Student Student_1;
    struct Student Student_2;
 
    createStudent( &Student_1 );
    createStudent( &Student_2 );
 
    printStudent( &Student_1 );
    printStudent( &Student_2 );
}
 
void printStudent( struct Student *student ) {
    printf("Student name: %s", student->student_name);
    printf("Student age: %li\n", student->student_age);
    printf("Student id: %li\n", student->student_id);
    printf("\n");
}
 
void createStudent (struct Student *my_student) {
    fflush(stdin);
    char name[255];
    long int age;
    long int id;
    printf("Please enter a student name:\n\n");
    fgets(name, sizeof(name), stdin);
 
    printf("Please enter a student id:\n\n");
    scanf(" %li", &id);
 
    printf("Please enter a student age:\n\n");
    scanf(" %li", &age);
 
    strcpy(my_student->student_name, name);
    my_student->student_id = age;
    my_student->student_age = id;
}

Comments