我在練習8下面的代碼被寫入以下cLearnthehardway,我有2個問題大小和數組在C
- 同時打印和整數它用來%ld的打印他們不要%d!
- 打印區域[10] //超出範圍打印0!爲什麼不拿給我一個錯誤,而它的valgrind通過(6erros)
#include<stdio.h>
int main(int argc,char *argv[])
{
int areas[]={10,12,13,14,20};
char name[]="Zed";
char full_name[]={'Z','e','d',' ','A','.',' ','S','h','a','w','\0'};
printf("The size of an int: %ld\n", sizeof(int));//why didn't we use %d instead of %ld(*)
printf("The size of areas (int[]):%ld\n",sizeof(areas));//*
printf("The number of ints in areas : %ld\n",sizeof(areas)/sizeof(int));//*
printf("The first area is %d, the second area is %d\n",areas[0],areas[1]);//Printed areas[10]=0!!
printf("The size of a char is: %ld\n",sizeof(char));//*
printf("The size of name(char[]) is:%ld\n",sizeof(name));//*
printf("The number of chars is : %ld\n",sizeof(name)/sizeof(char));//*
printf("the size of FULL NAME (char[]) is:%ld\n",sizeof(full_name));//*
printf("The number of chars in full name is %ld\n",sizeof(full_name)/sizeof(char));//*
printf("name=\"%s\" and full name =\"%s\"\n",name,full_name);// what is \"%s\" \ is an ESCAPE
return 0;
}
問題已經被答案解決,我選擇了完美的一個:) – 2014-11-21 13:18:31