มีสี่คลาสการจัดเก็บข้อมูลในภาษาการเขียนโปรแกรม C ซึ่งมีดังนี้ -
- อัตโนมัติ
- ภายนอก
- คงที่
- ลงทะเบียน
ตัวแปรส่วนกลาง / ตัวแปรภายนอก
คีย์เวิร์ดอยู่ภายนอก ตัวแปรเหล่านี้ถูกประกาศนอกบล็อก
-
ขอบเขต − ขอบเขตของตัวแปรส่วนกลางสามารถใช้ได้ตลอดทั้งโปรแกรม
-
ค่าเริ่มต้น เป็นศูนย์
อัลกอริทึม
อัลกอริทึมได้รับด้านล่าง −
START Step 1: Declare and initialized extern variable Step 2: Declare and initialized int variable a=3 Step 3: Print a Step 4: Call function step 5 Step 5: Called function Print a (takes the value of extern variable)
ตัวอย่าง
ต่อไปนี้เป็นโปรแกรม C สำหรับ คลาสพื้นที่เก็บข้อมูลภายนอก −
extern int a =5; /* this ‘a’ is available entire program */
main ( ){
int a = 3; /* this ‘a' is valid only in main */
printf ("%d",a);
fun ( );
}
fun ( ){
printf ("%d", a);
} ผลลัพธ์
ผลลัพธ์ระบุไว้ด้านล่าง −
3 1
พิจารณาโปรแกรมอื่นสำหรับคลาสพื้นที่จัดเก็บภายนอก −
ตัวอย่าง
External.h
extern int a=14;
extern int b=8;
externstorage.c file
#include<stdio.h>
#include "External.h"
int main(){
int sub = a-b;
printf("%d -%d = %d ", a, b, sub);
return 0;
} ผลลัพธ์
ผลลัพธ์ระบุไว้ด้านล่าง −
a-b=6