ホームページ  >  記事  >  バックエンド開発  >  C言語では構造体変数に以下のようにアクセスします。

C言語では構造体変数に以下のようにアクセスします。

WBOY
WBOY転載
2023-08-31 19:29:06573ブラウズ

C言語では構造体変数に以下のようにアクセスします。

構造体はユーザー定義のデータ型であり、さまざまなデータ型のデータのコレクションを格納するために使用されます。

構造体は配列に似ています。唯一の違いは、配列は同じデータ型を格納するために使用されるのに対し、構造体は異なるデータ型を格納するために使用されることです。

キーワード struct は構造体を宣言するためのものです。

内部の変数構造体は構造体のメンバーです。

構造体は次のように宣言できます。-

Struct structurename{
   //member declaration
};

Example

以下は、構造体変数にアクセスするための C プログラムです。-

ライブデモ

struct book{
   int pages;
   float price;
   char author[20];
};
Accessing structure members in C
#include<stdio.h>
//Declaring structure//
struct{
   char name[50];
   int roll;
   float percentage;
   char grade[50];
}s1,s2;
void main(){
   //Reading User I/p//
   printf("enter Name of 1st student : ");
   gets(s1.name);
   printf("enter Roll number of 1st student : ");
   scanf("%d",&s1.roll);
   printf("Enter the average of 1st student : ");
   scanf("%f",&s1.percentage);
   printf("Enter grade status of 1st student : ");
   scanf("%s",s1.grade);
   //Printing O/p//
   printf("The name of 1st student is : %s</p><p>",s1.name);
   printf("The roll number of 1st student is : %d</p><p>",s1.roll);
   printf("The average of 1st student is : %f</p><p>",s1.percentage);
   printf("The student 1 grade is : %s and percentage of %f</p><p>",s1.grade,s1.percentage);
}

出力

上記のプログラムを実行すると、次の結果が生成されます -

enter Name of 1st student: Bhanu
enter Roll number of 1st student: 2
Enter the average of 1st student: 68
Enter grade status of 1st student: A
The name of 1st student is: Bhanu
The roll number of 1st student is: 2
The average of 1st student is: 68.000000
The student 1 grade is: A and percentage of 68.000000

以上がC言語では構造体変数に以下のようにアクセスします。の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。