
结构是不同数据类型变量的集合,以单个名称分组在一起。
结构声明的一般形式
结构声明如下如下 -
struct tagname{
datatype member1;
datatype member2;
datatype member n;
};这里,struct 是关键字。
立即学习“C语言免费学习笔记(深入)”;
tagname 指定结构名称。
member1
strong>、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("</p><p>enter the add of emp1:");
scanf("%s",emp1.add);
printf("</p><p>enter the salary of emp1:");
scanf("%d",&emp1.salary);
printf("</p><p>enter the name of emp2:");
// gets(emp2.name);
scanf("%s",emp2.name);
printf("</p><p>enter the add of emp2:");
scanf("%s",emp2.add);
printf("</p><p>enter the salary of emp2:");
scanf("%d",&emp2.salary);
printf("</p><p>emp1 salary is %d",emp1.salary);
printf("</p><p>emp2 salary is %d",emp2.salary);
printf("</p><p>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
以上就是在C语言中,局部作用域是指在特定代码块内部定义的变量、函数或其他实体的可见范围。局部作用域的实体只能在其所在的代码块内部访问和使用,超出该范围将无法访问的详细内容,更多请关注php中文网其它相关文章!
C语言怎么学习?C语言怎么入门?C语言在哪学?C语言怎么学才快?不用担心,这里为大家提供了C语言速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号