C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Structures in C

Student structure (input & display)

C Program: Input and Display Student Structure

C

#include <stdio.h>

 

struct Student {

    int rollNo;

    char name[50];

    float marks;

};

 

int main() {

    struct Student s;

 

    // Input student details

    printf("Enter Roll Number: ");

    scanf("%d", &s.rollNo);

 

    printf("Enter Name: ");

    scanf(" %[^\n]", s.name);   // reads string with spaces

 

    printf("Enter Marks: ");

    scanf("%f", &s.marks);

 

    // Display student details

    printf("\n--- Student Details ---\n");

    printf("Roll Number: %d\n", s.rollNo);

    printf("Name       : %s\n", s.name);

    printf("Marks      : %.2f\n", s.marks);

 

    return 0;

}

Output

 
OUTPUT :
Enter Roll Number: 101
Enter Name: Rohan Sharma
Enter Marks: 89.5

--- Student Details ---
Roll Number: 101
Name       : Rohan Sharma
Marks      : 89.50

Explanation

Part

Description

struct Student

Structure definition to store multiple related data (rollNo, name, marks).

scanf(" %[^\n]", s.name)

Reads full name including spaces.

%0.2f

Displays marks with two decimal places.