第二天淺識c語言
阿新 • • 發佈:2022-05-24
1.常量
(1) 字面常量
1 #include<stdio.h> 2 int main(){ 3 3; 4 4; 5 5; //字面常量的意思就是能夠直接寫出的常量的意思,懂得都懂 6 7 return 0; 8 }
(2) const修飾的常變數
1 #include<stdio.h> 2 int main() { 3 int a =10; 4 printf("%d",a); 5 a = 20; 6 printf("%d",a);//這是屬於正常變數的範疇 7 return 0; 8 }
1 #include<stdio.h> 2 int main() {3 const int a =10; 4 printf("%d",a); 5 //a = 20; //這裡會執行報錯,原因是因為"const"已經限定了a的值,a 6 //的值不能再次被改變 7 //printf("%d",a); 8 return 0; 9 }
1 #include<stdio.h> 2 int main() { 3 const int a = 10; //經過const修飾後a具有"常屬性",不能夠對a進行修 4 //改 5 int arr[10] = {0};6 //const arr[a] = {0};//這裡會執行報錯,原因是陣列中的引數不接受變數 7 //說明儘管a已經被修飾成常變數,依然是一個變數 8 return 0; 9 }
(3) define定義的識別符號常量
1 #include<stdio.h> 2 #define num = 10; 3 int main() { 4 int arr[num] = {0};//由於陣列的原因,num只能是常量,而這一行程式碼 5 //能夠正常執行,也說明了"#define"定義的是一個常量6 return 0; 7 }
(4) 列舉常量
1 enum color { 2 red, 3 yellow, 4 blue //列舉的意思就是意思就是列舉 5 } 6 #include<stdio.h> 7 int main(){ 8 enum color x = red;//x為變數 9 x = blue; //變數可以被改變 10 //blue = 6; //列舉常量不可以被改變 11 return 0; 12 }