c语言中数组的声明喝初始化的区别和联系

 

 

声明是不赋值; 初始化是给数组元素赋值。

001、

复制代码
[root@PC1 test]# ls
test.c
[root@PC1 test]# cat test.c                        ## 测试c程序
#include <stdio.h>

int main(void)
{
        int ay[3];                                // 声明,不赋int by[3] = {3,8,2};                      // 初始化,赋int i;

        for(i = 0; i < 3; i++)
        {
                printf("ay[%d] = %d\t", i, ay[i]);
        }
        puts("");
        for(i = 0; i < 3; i++)
        {
                printf("by[%d] = %d\t",i, by[i]);
        }
        puts("");

        return 0;
}
[root@PC1 test]# gcc test.c -o kkk
[root@PC1 test]# ls
kkk  test.c
[root@PC1 test]# ./kkk               ## 运算, 声明后,返回元素的值是不可预期的,为什么?
ay[0] = -1620501232     ay[1] = 32764   ay[2] = 0
by[0] = 3       by[1] = 8       by[2] = 2
复制代码

 。

 

b、

复制代码
[root@PC1 test]# ls
test.c
[root@PC1 test]# cat test.c                         ## 测试c程序
#include <stdio.h>

int main(void)
{
        int ay[3];                                // 先声明

        ay[3] = {3,8,6};                          // 然后这样初始化赋值时不可以的,只能单个元素赋值,为什么会有这种限制return 0;
}
[root@PC1 test]# gcc test.c -o kkk           ##   编译报错
test.c: In function ‘main’:
test.c:7:10: error: expected expression before ‘{’ token
  ay[3] = {3,8,6};
          ^
复制代码

 。

 

posted @   小鲨鱼2018  阅读(32)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2022-10-27 python 脚本统计fasta文件每条scaffold的碱基长度
2022-10-27 linux 中 shell 统计每条scaffold的长度
2022-10-27 linux 中 wc -c命令
2022-10-27 linux 系统中如何判断字符串是否相同
2021-10-27 R语言中%*%运算符
2021-10-27 windows中如何查看端口占用情况、端口是否开启
2021-10-27 R语言中setdiff、intersect、union函数
点击右上角即可分享
微信分享提示