We know that we need to declare variables before using it in our code. However, we variables can be assigned with 0 or 1 without declaration. In the following example we can see this.
Example
#include #include x, y, array[3]; // implicit initialization of some variablesint main(i) { //The argument i will hold 1 int index; printf("x = %d, y = %d", x, y); for(index = 0; index
", i, array[i]); printf("The value of i : %d", i);}
登录后复制
Output
x = 0, y = 0Array[0] = 0Array[1] = 0Array[2] = 0The value of i : 1
登录后复制
有时候,如果某个数组只初始化了一部分值,那么剩下的值会被设置为0。
#include #include int main() { //The argument i will hold 1 int index; int array[10] = {1, 2, 3, 4, 5, 6}; for(index = 0; index", index, array[index]);}
登录后复制
Output
Array[0] = 1Array[1] = 2Array[2] = 3Array[3] = 4Array[4] = 5Array[5] = 6Array[6] = 0Array[7] = 0Array[8] = 0Array[9] = 0
登录后复制
以上就是在C语言中,变量的隐式初始化为0或1的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2587934.html