Write a program to print size of a structure.
#include<stdio.h> #include<string.h> #include<stdlib.h> struct p0 { }; struct p1 { char ch; }; struct p2 { char ch; short int d; }; struct p3 { char ch; int d; }; struct person { char name[10]; int age; float salary; }v0={"Start"},v1,v2={"IDC",25,52000.00}; int main() { struct person v3 = {"AB"}; struct person v4 = {98}; struct person v5; struct person v6; static struct person v7; struct person v8; struct person v9; struct person v10; struct person v11 = {.salary = 35}; struct person *ptr; struct person *ptr2; v5.age = 43; strcpy(v6.name,"What"); printf("Size of structure p0 = %d\n",sizeof(struct p0)); printf("Size of structure p1 = %d\n",sizeof(struct p1)); printf("Size of structure p2 = %d\n",sizeof(struct p2)); printf("Size of structure p3 = %d\n",sizeof(struct p3)); printf("Size of structure person = %d\n",sizeof(struct person)); printf("v0 = %s %d %f\n",v0.name,v0.age,v0.salary); printf("v1 = %s %d %f\n",v1.name,v1.age,v1.salary); printf("v2 = %s %d %f\n",v2.name,v2.age,v2.salary); printf("v3 = %s %d %f\n",v3.name,v3.age,v3.salary); printf("v4 = %s %d %f\n",v4.name,v4.age,v4.salary); printf("v5 = %s %d %f\n",v5.name,v5.age,v5.salary); printf("v6 = %s %d %f\n",v6.name,v6.age,v6.salary); printf("v7 = %s %d %f\n",v7.name,v7.age,v7.salary); printf("Enter Name = "); scanf("%[^\n]",v8.name); printf("Enter Age = "); scanf("%d",&v8.age); printf("Enter Salary = "); scanf("%f",&v8.salary); printf("v8 = %s %d %f\n",v8.name,v8.age,v8.salary); printf("Enter Age = "); scanf("%d",&v9.age); printf("v9 = %s %d %f\n",v9.name,v9.age,v9.salary); ptr = (struct person*)malloc(sizeof(struct person)); ptr = &v2; printf("v2 = %s %d %f\n",ptr->name,ptr->age,ptr->salary); v10 = v2; printf("v10 = %s %d %f\n",v10.name,v10.age,v10.salary); printf("v11 = %s %d %f\n",v11.name,v11.age,v11.salary); ptr2 = (struct person*)malloc(sizeof(struct person)); printf("Enter Name = "); scanf(" %[^\n]",ptr2->name); printf("Enter Age = "); scanf("%d",&ptr2->age); printf("Enter salary = "); scanf("%f",&ptr2->salary); printf("ptr2 = %s %d %f\n",ptr2->name,ptr2->age,ptr2->salary); return 0; }
Output
Size of structure p0 = 0 Size of structure p1 = 1 Size of structure p2 = 4 Size of structure p3 = 8 Size of structure person = 20 v0 = Start 0 0.000000 v1 = 0 0.000000 v2 = IDC 25 52000.000000 v3 = AB 0 0.000000 v4 = b 0 0.000000 v5 = ��� 43 0.000000 v6 = What 0 0.000000 v7 = 0 0.000000 Enter Name = Joe Enter Age = 23 Enter Salary = 23453 v8 = Joe 23 23453.000000 Enter Age = 54 v9 = Љ�� 54 -0.000015 v2 = IDC 25 52000.000000 v10 = IDC 25 52000.000000 v11 = 0 35.000000 Enter Name = etr Enter Age = 43 Enter salary = 94764 ptr2 = etr 43 94764.000000