What is the default value of char in c

余生长醉 提交于 2019-12-04 17:17:50

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.

The C99 standard says that:

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

On a side note:

As per 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.

Automatic variables that are not initialized have indeterminate value, we can see this by going to the draft C99 standard section 6.7.8 Initialization:

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

using an indeterminate value is undefined behavior. The definition for indeterminate values is as follows:

either an unspecified value or a trap representation

It just may be the case that the values you are ending up with for char are not printable.

There is not default value assigned. Never trust in a default value you didn't assign.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!