我運行下面的代碼,但得到的控制檯屏幕上沒有輸出沒有輸出。請解釋:代碼給出了控制檯
#include <stdio.h>
void main()
{
enum days {sun,mon,tue,wed,thru,fri,sat};
}
我運行下面的代碼,但得到的控制檯屏幕上沒有輸出沒有輸出。請解釋:代碼給出了控制檯
#include <stdio.h>
void main()
{
enum days {sun,mon,tue,wed,thru,fri,sat};
}
#include <stdio.h>
int main()
{
printf("sun, mon, tue, wed, thru, fri, sat\n");
return 0;
}
難道這就是你想幹什麼?
最後添加'\ n'會更好,因爲'stdout'是行緩衝的。 –
哦,非常小的錯誤,我明白了。謝謝你們:) –
不,我試圖打印所有日子的價值sun = 1,mon = 2 ... sat = 7。像這些。 –
枚舉被用作用戶定義的數據類型。您可以使用以下語法創建自己的數據類型。枚舉可以用來設置命名整型常量的集合。
enum datatype_name {val1,val2,val3,...,valN};
默認枚舉值將從0。這裏產生,
val1=0; //val1 is a named constant holding value 0
val2=1; //val2 is a named constant holding value 1
valN=N-1; //valN is a named constant holding value N-1
支票默認枚舉行爲下面的代碼。
#include<stdio.h>
//Define user defined data type. Here days is the datatype. sun,mon,...,sat are named constants.
enum days{sun,mon,tue,wed,thu,fri,sat};
int main()
{
printf("%d",wed); //wed is a named constant with default value 3
return 0;
}
Output: 3
初始化enum的自定義值。
#include<stdio.h>
enum days{sum=100,mon=200,tue=300,wed=400,thu=500,fri=600,sat=700};
int main()
{
printf("%d",wed); //wed is a named constant with user defined value 400
return 0;
}
Output: 400
您可以創建一個布爾值枚舉。
enum boolean{ false,true};
int main()
{
printf("false=%d",false); //false is constant that holds default value 0
printf("\ntrue=%d",true); //true is constant that holds default value 1
return 0;
}
Output:
false=0
true=1
最後一個例子可能會導致編譯時錯誤,如果有人還包括C99 stdbool.h其中虛實是定義爲宏(0和1)我認爲。 – maverik
@maverik是對的。它只是使用stdbool.h的替代方法。 –
你在控制檯上寫下任何東西。試試'printf(「Hello world!\ n」);' – VoidPointer
你還沒打印任何東西。你怎麼能期待輸出到屏幕:) –
歡迎來到C編程世界! http://cplus.about.com/od/introductiontoprogramming/p/enumeration.htm – P0W