Rumah > Artikel > pembangunan bahagian belakang > Tulis program C untuk memaparkan saiz dan mengimbangi ahli struktur
Tulis atur cara C untuk mentakrifkan struktur dan memaparkan saiz dan mengimbangi pembolehubah ahli#🎜🎜
Structure - Ia ialah koleksi pembolehubah jenis data yang berbeza, dikumpulkan di bawah satu nama.
Bentuk am struktur pengisytiharandatatype member1; struct tagname{ datatype member2; datatype member n; };
struct book{ int pages; char author [30]; float price; };Pembolehubah strukturTerdapat tiga cara untuk mengisytiharkan pembolehubah struktur-
struct book{ int pages; char author[30]; float price; }b;
struct{ int pages; char author[30]; float price; }b;Kaedah 3
struct book{ int pages; char author[30]; float price; }; struct book b;
struct book{ int pages; char author[30]; float price; } b = {100, "balu", 325.75};
Kaedah 2
struct book{ int pages; char author[30]; float price; }; struct book b = {100, "balu", 325.75};#🎜 Operator ahli)
struct book{ int pages; char author[30]; float price; } ; struct book b; b. pages = 100; strcpy (b.author, "balu"); b.price = 325.75;Kaedah empat (menggunakan fungsi scanf)
struct book{ int pages; char author[30]; float price; } ; struct book b; scanf ("%d", &b.pages); scanf ("%s", b.author); scanf ("%f", &b. price);
#include<stdio.h> #include<stddef.h> struct tutorial{ int a; int b; char c[4]; float d; double e; }; int main(){ struct tutorial t1; printf("the size 'a' is :%d</p><p>",sizeof(t1.a)); printf("the size 'b' is :%d</p><p>",sizeof(t1.b)); printf("the size 'c' is :%d</p><p>",sizeof(t1.c)); printf("the size 'd' is :%d</p><p>",sizeof(t1.d)); printf("the size 'e' is :%d</p><p>",sizeof(t1.e)); printf("the offset 'a' is :%d</p><p>",offsetof(struct tutorial,a)); printf("the offset 'b' is :%d</p><p>",offsetof(struct tutorial,b)); printf("the offset 'c' is :%d</p><p>",offsetof(struct tutorial,c)); printf("the offset 'd' is :%d</p><p>",offsetof(struct tutorial,d)); printf("the offset 'e' is :%d</p><p></p><p>",offsetof(struct tutorial,e)); printf("size of the structure tutorial is :%d",sizeof(t1)); return 0; }
the size 'a' is :4 the size 'b' is :4 the size 'c' is :4 the size 'd' is :4 the size 'e' is :8 the offset 'a' is :0 the offset 'b' is :4 the offset 'c' is :8 the offset 'd' is :12 the offset 'e' is :16 size of the structure tutorial is :24
Atas ialah kandungan terperinci Tulis program C untuk memaparkan saiz dan mengimbangi ahli struktur. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!