c中char的默认值是什么 [英] What is the default value of char in c

查看:1231
本文介绍了c中char的默认值是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我声明一个 int 变量时,不要给它赋值,然后在我打印它时,将打印一个随机数.

When I declare an int variable, and do not not assign a value to it, and then when I print it, a random number gets printed.

但是当我分配一个 char 变量并使用%c格式说明符打印时,什么也没打印.那么C中的 char 变量是否具有像null这样的默认值? C中的局部变量是否以随机值开头?那为什么 char 不能这样表现呢?

but when I allocate a char variable, and print it with %c format specifier, nothing is printed. So does a char variable in C have a default value like null? Do local variables in C start with a random value? Then why doesn't a char behave in this way?

推荐答案

没有默认值分配给它.有些值是不可打印的,您可以假定随机值是其中之一,因此这就是您看不到结果的原因.

There is no default value which is assigned to it. Some values are not printable and you can assume that random value is one of them, so that is the reason why you are not able to see the result.

C99标准指出:

如果具有自动存储持续时间的对象未初始化 明确地,其值为不确定.

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.

附带说明:

根据C99

如果具有静态存储持续时间的对象未初始化 明确地,然后:

If an object that has static storage duration is not initialized explicitly, then:

  • 如果具有指针类型,则将其初始化为空指针;
  • 如果具有算术类型,则将其初始化为(正数或无符号)零;
  • 如果是聚合,则根据这些规则(递归)初始化每个成员;
  • 如果是联合,则将根据这些规则(递归)初始化第一个命名成员.
  • if it has pointer type, it is initialized to a null pointer;
  • if it has arithmetic type, it is initialized to (positive or unsigned) zero;
  • if it is an aggregate, every member is initialized (recursively) according to these rules;
  • if it is a union, the first named member is initialized (recursively) according to these rules.

这篇关于c中char的默认值是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆