>  기사  >  백엔드 개발  >  C 언어에서는 구조 변수에 다음과 같이 액세스합니다.

C 언어에서는 구조 변수에 다음과 같이 액세스합니다.

WBOY
WBOY앞으로
2023-08-31 19:29:06574검색

C 언어에서는 구조 변수에 다음과 같이 액세스합니다.

구조는 다양한 데이터 유형의 모음을 저장하는 데 사용되는 사용자 정의 데이터 유형입니다.

구조는 배열과 유사합니다. 유일한 차이점은 배열은 동일한 데이터 유형을 저장하는 데 사용되는 반면, 구조는 다른 데이터 유형을 저장하는 데 사용된다는 것입니다.

struct 키워드는 구조를 선언하기 위한 것입니다.

구조체 내부의 변수는 구조의 구성원입니다. .

A 구조체는 다음과 같이 선언할 수 있습니다. −

Struct structurename{
   //member declaration
};

Example

다음은 구조체 변수에 접근하는 C 프로그램입니다. −

 Live Demo

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);
}

Output

위 프로그램을 실행하면 다음과 같은 결과가 나옵니다. −

rreee

위 내용은 C 언어에서는 구조 변수에 다음과 같이 액세스합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제