一个变量是一个具有值的占位符。所有变量都有一些与它们相关联的类型,它们表示可以分配什么类型的值。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语言算术运算示例程序