字符串數組例子
#include ”stdio.h“
int main()
{
char a[ ]= "hello world";
printf("%s\n",a);
}
輸出結果:
hello world
整型數組例子:
#include ”stdio.h“
int main()
{
int a[ ]= {1,2,3,4,5,6}
printf("%d\n",a);
}
理想結果:123456
實際輸出結果:6356728
實際輸出的是整型數組a的地址,改爲printf("%d\n",a[0]); 就可以輸出:1
字符串數組例子(%c)
#include ”stdio.h“
int main()
{
char a[ ]= "hello world";
printf("%c\n",a);
}
輸出結果:
是一個方框,這是因爲其實是輸出的一個很大的數值,他的ASCLL值對應的是這個方框。
總結:
在用%c,%d時都能作爲首地址輸出第一個字母或數組元素,
但是如果換用,%s,輸出字符串,他就會根據給出的字符指針位置往後尋找字符,直到遇到‘\0’結束。
printf 遇到 “%c” 就會在後面參數列表找 char 類型的參數,輸出一個字符
而遇到 “%s” 就會在後面參數列表找 char* 類型的參數,輸出一個字符串
綜合例子:
#include "stdio.h" int main() { char *a="i'm zhaoyu"; printf("%s\n",a); printf("%c\n",*(a+1)); char b[]="love jingjing"; printf("%s\n",b); printf("%c\n",b[1]); }