解釋 C 語言中的 const型別限定符
型別限定符為 C 程式語言中的現有資料型別新增特殊屬性。
C 語言中有三種類型限定符,如下所示解釋了常量型別限定符 −
const
共有三種類型的常量,如下所示 −
文字常量
定義常量
內在常量
文字常量 − 這是用來指定資料的未命名常量。
例如,
a=b+7 //Here ‘7’ is literal constant.
定義常量 − 這些常量使用帶有 # 的預處理器命令“define”
例如, #define PI 3.1415
內在常量 − 這些常量使用“C”限定符“const”,它表示無法更改資料。
語法如下 −
const type identifier = value
例如,
const float pi = 3.1415
如你所見,它只是給出一個文字名稱。
示例
以下是常量型別限定符的 C 程式 −
#include<stdio.h> #define PI 3.1415 main ( ){ const float cpi = 3.14 printf ("literal constant = %f",3.14); printf ("defined constant = %f", PI); printf ("memory constant = %f",cpi); }
輸出
執行以上程式後,會得到以下結果 −
literal constant = 3.14 defined constant = 3.1415 memory constant = 3.14
廣告