主要内容:
介绍C语言学习中的6种数据类型,以及对应的格式符,以及相应的字节大小。
一、给出结论
二、代码验证:
#include<stdio.h>
int main(){
//第一章 数据类型
//数据类型、格式符、字节大小
int aa=1;printf("This is an int data type,style symbol is %%d,number is %d ,bitsize is %d \n",aa,sizeof(aa));
//这是一个整型数据类型,格式符是..,数值是..,字节大小是..
float bb=1.1;printf("This is an float data type,style symbol is %%f,number is %f,bitsize is %d \n",bb,sizeof(bb));
double cc=3.3;printf("This is an double data type,style symbol is %%lf,number is %lf,bitsize is %d \n",cc,sizeof(cc));
char dd='a';printf("This is an char data type,style symbol is %%c,number is %d ,bitsize is %d \n",dd,sizeof(dd));
long ee=123;printf("This is an long data type,style symbol is %%ld,number is %ld,bitsize is %d \n",ee,sizeof(ee));
short ff=567;printf("This is an short data type,style symbol is %%hd,number is %hd,bitsize is %d \n",ff,sizeof(ff));
return 0;
}
三、windows下结果
四、Linux系统下运行
发现在Linux系统下,sizeof的返回值是无符号长整型,于是格式符改为%ld
新的代码如下:
#include<stdio.h>
int main(){
//第一章 数据类型
//数据类型、格式符、字节大小
int aa=1;printf("This is an int data type,style symbol is %%d,number is %d ,bitsize is %ld \n",aa,sizeof(aa));
//这是一个整型数据类型,格式符是..,数值是..,字节大小是..
float bb=1.1;printf("This is an float data type,style symbol is %%f,number is %f,bitsize is %ld \n",bb,sizeof(bb));
double cc=3.3;printf("This is an double data type,style symbol is %%lf,number is %lf,bitsize is %ld \n",cc,sizeof(cc));
char dd='a';printf("This is an char data type,style symbol is %%c,number is %d ,bitsize is %ld \n",dd,sizeof(dd));
long ee=123;printf("This is an long data type,style symbol is %%ld,number is %ld,bitsize is %ld \n",ee,sizeof(ee));
short ff=567;printf("This is an short data type,style symbol is %%hd,number is %hd,bitsize is %ld \n",ff,sizeof(ff));
return 0;
}
运行结果:
This is an int data type,style symbol is %d,number is 1 ,bitsize is 4
This is an float data type,style symbol is %f,number is 1.100000,bitsize is 4
This is an double data type,style symbol is %lf,number is 3.300000,bitsize is 8
This is an char data type,style symbol is %c,number is 97 ,bitsize is 1
This is an long data type,style symbol is %ld,number is 123,bitsize is 8
This is an short data type,style symbol is %hd,number is 567,bitsize is 2