Structure example

 1. Write a c program to display name and price of car using structure.

#include<stdio.h>

int main()

{

    struct car

    {

        char name[20];

        int price

    };

    struct car c1={"Lambo",2000};

    printf("The name of car is %s \n",c1.name);

    printf("The price of car is %d",c1.price);

    return 0;

}

or
#include<stdio.h>
struct car
    {
        char name[20];
        int price;
    }c1;
int main()
{
    strcpy(c1.name,"lambo"); // copying string in char array
    c1.price=12345;
    printf("The name of car is %s",c1.name);
    printf("The price of car is %d",c1.price);
    return 0;
}

structure example with asking input from user
#include<stdio.h>
int main()
{
    struct emp
    {
        char name[20];
        char add[15];
        int ag;
    };

    struct emp e;
    printf("Enter the name?");
    scanf("%s",&e.name);
    printf("Enter the address?");
    scanf("%s",&e.add);
    printf("Enter the age?");
    scanf("%d",&e.ag);

    printf("The name of employee is %s\n",e.name);
    printf("THe address of employee is %s\n",e.add);
    printf("The age is %d\n",e.ag);
    return 0;
}

Comments