Translate

important

structure in c programming| What is structure in C? explain with example.

Structure:-In C programming, Structure is a user defined data type which is used for create(store) collection of data with different data types.

c structure syntax



Some important points of structure in C

  • The data is stored in different memory locations.
  • The size of the structure is the sum of its all data members' size.
  • The minimum size of the structure is 1 byte.
  • The structure is declared by the help of struct keyword.

Syntax of structure in C.

struct structure_name

{

data member 1;

data member 2;

data member 3;

.

.

.

};


Structure declaration and definition in C.

below is declaration and definition of structure in c programming.
struct stu_details
{
char nm[20];
char sec;
int roll;
};

C struct (structure) initialization.

This is how you can initialize struct in C programming.

main ()
{
struct stu_details s;
s.nm="Izhar";
s.sec='A';
s.roll=001;
printf ("Name=%s",s.nm);
printf ("\nSection=%c",s.sec);
printf ("\nRoll no=%d",s.roll);
}

OUTPUT

Name=Izhar

Section=A

Roll no=001

C Structure Example(Question).

Structure program for student details in C.


#include<stdio.h>
struct st
{
    char nm[20];
    char cls[20];
    char sec;
    int rn;
    int marks[5];
};
int main()
{
    int total=0;
    int i;
    float avg;
    struct st s;
    printf("Enter Your Full Name\n");
    scanf("%[^\n]s",&s.nm);//"%[^\n]s" to accept name with spaces.
    printf("Enter Your Class\n");
    scanf("%s",&s.cls);
    printf("Enter Your section\n");
    fflush(stdin);
    scanf("%c",&s.sec);
    printf("Enter your Roll no:\n");
    scanf("%d",&s.rn);
    for(i=1; i<=5; i++)
    {
        printf("Enter %d's subject's marks\n",i);
        fflush(stdin);
        scanf("%d",&s.marks[i]);
        total=total+s.marks[i];
    }
    avg=total/5;
    printf("Name:%s",s.nm);
    printf("\nClass:%s",s.cls);
    printf("\nSection:%c",s.sec);
    printf("\nRoll Number:%d",s.rn);
    printf("\nTotal marks:%d",total);
    printf("\nAverage marks:%.2f",avg);
    return 0;
}

OUTPUT

Enter Your Full Name

Izhar Ashraf 

Enter Your Class

B.vocSD 

Enter Your Section

A

Enter Your Roll no.

001

Enter Your 1's Subject's marks

90

Enter Your 2's Subject's marks

80

Enter Your 3's Subject's marks

70

Enter Your 4's Subject's marks

60

Enter Your 5's Subject's marks

70

Name:Ezhar Ashraf

Class:B.vocSD

Section:A

Roll Number:1

Total marks:370

Average marks:74.00


So this is all about structure in C programming, I hope you understand that what is structure in C programming.

If you have any query related to this article than please comment below!! ThankYou!!


Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad