C 語言中區域性範圍內的結構是什麼?
結構是不同資料型別變數的集合,以單一名稱組合在一起。
結構宣告的一般形式
結構宣告如下 −
struct tagname{
datatype member1;
datatype member2;
datatype member n;
};此處,struct 是關鍵字。
標籤名指定結構的名稱。
member1、member2 指定構成結構的資料項。
示例
以下示例演示了在區域性作用域中使用結構。
struct book{
int pages;
char author [30];
float price;
};示例
以下程式演示了在區域性作用域中使用結構。
#include<stdio.h>
struct{
char name[20];
int age;
int salary;
char add[30];
}emp1,emp2;
int manager(){
struct{ //structure at local scope
char name[20];
int age;
int salary;
char add[50];
}manager ;
manager.age=27;
if(manager.age>30)
manager.salary=650000;
else
manager.salary=550000;
return manager.salary;
}
int main(){
printf("enter the name of emp1:");
//gets(emp1.name);
scanf("%s",emp1.name);
printf("
enter the add of emp1:");
scanf("%s",emp1.add);
printf("
enter the salary of emp1:");
scanf("%d",&emp1.salary);
printf("
enter the name of emp2:");
// gets(emp2.name);
scanf("%s",emp2.name);
printf("
enter the add of emp2:");
scanf("%s",emp2.add);
printf("
enter the salary of emp2:");
scanf("%d",&emp2.salary);
printf("
emp1 salary is %d",emp1.salary);
printf("
emp2 salary is %d",emp2.salary);
printf("
manager salary is %d",manager());
return 0;
}輸出
執行以上程式時,它會生成以下結果 −
enter the name of emp1:Bob enter the add of emp1:Hyderabad enter the salary of emp1:500000 enter the name of emp2:Hari enter the add of emp2:Chennai enter the salary of emp2:450000 emp1 salary is 500000 emp2 salary is 450000 manager salary is 550000
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP