声明是不赋值; 初始化是给数组元素赋值。
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 ‘{’ tokenay[3] = {3,8,6};^
。