write a c program to Scan the structure variables and print on the screen

17 Dec 2022 Balmiki Mandal 0 C Programming

To scan the structure variables and print them on the screen, you first need to define a structure that contains the variables you want to scan and print. Here's an example of a structure definition:

struct student {
    char name[50];
    int rollNo;
    float marks;
};

This structure defines a student with three variables: a character array name, an integer rollNo, and a float marks.

Now, to scan the values of these variables from the user, you can use the scanf() function as follows:

#include <stdio.h>

int main() {
    struct student s;

    printf("Enter name: ");
    scanf("%s", s.name);

    printf("Enter roll number: ");
    scanf("%d", &s.rollNo);

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

    printf("Student details:\n");
    printf("Name: %s\n", s.name);
    printf("Roll number: %d\n", s.rollNo);
    printf("Marks: %.2f\n", s.marks);

    return 0;
}

In this program, we first define a variable s of the student structure type. Then we prompt the user to enter the name, roll number, and marks of the student using printf() and scanf() functions. Finally, we print the details of the student using printf() function.

Note that when scanning integer and float variables, we use the & operator to get the address of the variable, since scanf() requires the address of the variable to modify its value. However, in the case of a character array like name, we don't need to use the & operator since the array name itself represents the address of the first element of the array.

 

#include<stdio.h>
struct one
{
    int i;
    char ch;
    float f;
};
void main()
{
    struct one s1;
    printf("Enter the Any Number:");
    scanf("%d",&s1.i);
    printf("Enter any charcater:");
    scanf("%s",&s1.ch);
    printf("Enter any Floating Number:");
    scanf("%f",&s1.f);
    printf("%d %c %f\n",s1.i,s1.ch,s1.f);
}

Output:

Enter the Any Number: 10

Enter any character: B

Enter any Floating Number: 23.4

10  23.400000

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.