C语言中有哪些不同的数据类型?
数据类型是内存位置或变量的声明。数据可以是不同类型的,以下是一些C语言中数据类型的示例:
整数、有理数、自然数、实数、复数、向量、字符等。
对于机器硬件来说,数据都是以有限长度的0和1的二进制数字串进行编码的。在机器中,整数数据在算术逻辑单元(ALU)中处理,分数数据在浮点单元(FPU)中处理。这反映在高级语言的内置或基本数据类型中。
内置数据类型
在C语言中,有不同的内置数据类型,其中一些如下所示:
int、float、char、unsigned int、unsigned char、long int、double等。
数据的使用
在C语言中,可以使用不同的类型以不同的方式存储数据。以下是一些示例:
- char 字符串,grade='A';
- int 计数,index=10;
- float 平均值=6.9;
在上面的示例中,char、int、float 是内置数据类型,而string、grade 是 char 类型的变量。
Grade='A' 将变量 grade 初始化为 'A' 的字符代码。
Count 和 index 是 int 类型的变量。
并且 index=10 将变量初始化为 10 的二进制表示。
示例
下面是C程序,用于**找出变量和内置数据类型的大小**:
#include<stdio.h>
int main(){
int x = 10;
char c;
printf("Size of variable x = %ld bytes
",sizeof(x));
printf("Size of variable c = %ld byte
",sizeof(c));
printf("Size of short is %ld bytes
",sizeof(short));
printf("Size of int is %ld bytes
",sizeof(int));
printf("Size of long is %ld bytes
",sizeof(long));
printf("Size of float is %ld bytes
",sizeof(float));
printf("Size of double is %ld bytes
",sizeof(double));
printf("Size of long double is %ld bytes
",sizeof(long double));
printf("Size of char is %ld bytes
",sizeof(char));
printf("Size of void is %ld bytes
",sizeof(void));
return 0;
}输出
执行上述程序时,会产生以下结果:
Size of variable x = 4 bytes Size of variable c = 1 byte Size of short is 2 bytes Size of int is 4 bytes Size of long is 4 bytes Size of float is 4 bytes Size of double is 8 bytes Size of long double is 16 bytes Size of char is 1 bytes Size of void is 1 bytes
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程
C++
C#
MongoDB
MySQL
Javascript
PHP