首页 > 后端开发 > C++ > 正文

在C语言中,变量的隐式初始化为0或1

王林
发布: 2023-08-25 17:13:11
转载
1657人浏览过

在c语言中,变量的隐式初始化为0或1

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 <stdio.h>
#include <stdlib.h>
x, y, array[3]; // implicit initialization of some variables
int main(i) {
   //The argument i will hold 1
   int index;
   printf("x = %d, y = %d</p><p></p>
                    <div class="aritcle_card">
                        <a class="aritcle_card_img" href="/ai/2177">
                            <img src="https://img.php.cn/upload/ai_manual/000/000/000/175680420128767.png" alt="码哩写作">
                        </a>
                        <div class="aritcle_card_info">
                            <a href="/ai/2177">码哩写作</a>
                            <p>最懂作者的AI辅助创作工具</p>
                            <div class="">
                                <img src="/static/images/card_xiazai.png" alt="码哩写作">
                                <span>91</span>
                            </div>
                        </div>
                        <a href="/ai/2177" class="aritcle_card_btn">
                            <span>查看详情</span>
                            <img src="/static/images/cardxiayige-3.png" alt="码哩写作">
                        </a>
                    </div>
                <p>", x, y);
   for(index = 0; index < 3; index++)
      printf("Array[%d] = %d</p><p>", i, array[i]);
      printf("The value of i : %d", i);
}
登录后复制

Output

x = 0, y = 0
Array[0] = 0
Array[1] = 0
Array[2] = 0
The value of i : 1
登录后复制

有时候,如果某个数组只初始化了一部分值,那么剩下的值会被设置为0。

#include <stdio.h>
#include <stdlib.h>
int main() {
   //The argument i will hold 1
   int index;
   int array[10] = {1, 2, 3, 4, 5, 6};
   for(index = 0; index < 10; index++)
      printf("Array[%d] = %d</p><p>", index, array[index]);
}
登录后复制

Output

Array[0] = 1
Array[1] = 2
Array[2] = 3
Array[3] = 4
Array[4] = 5
Array[5] = 6
Array[6] = 0
Array[7] = 0
Array[8] = 0
Array[9] = 0
登录后复制

以上就是在C语言中,变量的隐式初始化为0或1的详细内容,更多请关注php中文网其它相关文章!

相关标签:
C语言速学教程(入门到精通)
C语言速学教程(入门到精通)

C语言怎么学习?C语言怎么入门?C语言在哪学?C语言怎么学才快?不用担心,这里为大家提供了C语言速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号