首頁  >  文章  >  後端開發  >  在C語言中,局部作用域是指在特定程式碼區塊內部定義的變數、函數或其他實體的可見範圍。局部作用域的實體只能在其所在的程式碼區塊內部存取和使用,超出該範圍將無法存取

在C語言中,局部作用域是指在特定程式碼區塊內部定義的變數、函數或其他實體的可見範圍。局部作用域的實體只能在其所在的程式碼區塊內部存取和使用,超出該範圍將無法存取

PHPz
PHPz轉載
2023-09-12 23:29:02853瀏覽

在C語言中,局部作用域是指在特定程式碼區塊內部定義的變數、函數或其他實體的可見範圍。局部作用域的實體只能在其所在的程式碼區塊內部存取和使用,超出該範圍將無法存取

結構是不同資料類型變數的集合,以單一名稱分組在一起。

結構宣告的一般形式

結構宣告如下如下 -

struct tagname{
   datatype member1;
   datatype member2;
   datatype member n;
};

這裡,struct 是關鍵字。

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中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除