C语言新手入门之格式化输出和变量类型

2020-01-06 18:30:16王冬梅

输出结果:


nums: 0 0 0 0
name each: a 
name: a

可以知道,整型数组定以后未赋值默认为0,字符数组就是为空.而且字符数组可以直接以字符串的形式输出。

关于布尔类型,在C语言中,没有真正意义上的布尔类型,而是用一个整数来表示。0表false,1表示true

数据类型大小

数据类型的大小在C语言上是很常见的,我们可以使用sizeof来检测一个长度,他返回的是一个long unsigned int类型的,所以要用%ld来格式化输出:


#include <stdio.h>
int main(){
  printf("The size of short: %ldn", sizeof(short));
  printf("The size of int: %ldn", sizeof(int));
  printf("The size of float: %ldn",sizeof(float));
  printf("The size of double: %ldn", sizeof(double));
  printf("The size of char: %ldn", sizeof(char));
}

运行结果如下(64位机器):


The size of short: 2
The size of int: 4
The size of float: 4
The size of double: 8
The size of char: 1

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对ASPKU的支持。


注:相关教程知识阅读请移步到C++教程频道。