一個變數是一個具有值的占位符。所有變數都有一些與它們相關聯的類型,它們表示可以分配什麼類型的值。C語言提供了一組豐富的變數 -
類型 | 格式字串 | 說明 |
---|---|---|
char |
%c |
字元類型變數(ASCII值) |
int |
%d |
整數的大小 |
float |
%f |
單精確度浮點值 |
double |
%e |
雙精度浮點值 |
void |
N/A |
代表不存在類型 |
字元(char)變數
字元(char)變數包含一個字元。如下示例代碼 -
#include <stdio.h>
int main() {
char c; // char variable declaration
c = 'A'; // defining a char variable
printf("value of c is %c", c);
return 0;
}
執行上面示例代碼,得到以下結果 -
value of c is A
整數(int)變數
int
變數保存單個字元的有符號整數值。如下示例代碼 -
#include <stdio.h>
int main() {
int i; // integer variable declaration
i = 123; // defining integer variable
printf("value of i is %d", i);
return 0;
}
執行上面示例代碼,得到以下結果 -
value of i is 123
浮點(float)變數
float
變數保存單精確度浮點值。如下示例代碼 -
#include <stdio.h>
int main() {
float f; // floating point variable declaration
f = 12.001234; // defining float variable
printf("value of f is %f", f);
return 0;
}
執行上面示例代碼,得到以下結果 -
value of f is 12.001234
雙精度(double)浮點變數
double
變數保存雙精度浮點值。如下示例代碼 -
#include <stdio.h>
int main() {
double d; // double precision variable declaration
d = 12.001234; // defining double precision variable
printf("value of d is %e", d);
return 0;
}
執行上面示例代碼,得到以下結果 -
value of d is 1.200123e+01
Void(void)數據類型
C語言中的空白(void
)意味著“無”或“無值”。這可以使用指針聲明或函數聲明。
如下示例代碼 -
// declares function which takes no arguments but returns an integer value
int status(void)
// declares function which takes an integer value but returns nothing
void status(int)
// declares a pointer p which points to some unknown type
void * p
上一篇:
C語言Hello World示例程式
下一篇:
C語言算術運算示例程式