为什么反序输出后有个a,怎么去掉啊,新手求问
因为你输出了一个不存在数组的数,a[5],把那个i改为i<5
因为数组是从0开始计数的,所以你的数组长度是5, 但是元素是a[0], a[1], a[2], a[3], a[4]
反序输出:
for (int i = 0; i < 5; i++)
printf("%c", a[5-i-1]);
char a[5]只能最多放4个字符,因为作为结束符,\0也要一个。
输出的时候多循环了一次
char a[5];最多存放5个字符,如果用scanf %c一个个字符读进来的话,及时最后一个字符不放\0也没关系
PS:gets并不安全,使用fgets替代
cplusplus.com有这样一段话:
Notice that gets is quite different from fgets: not only gets uses stdin as source, but it does not include the ending newline character in the resulting string and does not allow to specify a maximum size for str (which can lead to buffer overflows).
man里有这样一段话:
Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.
他们说的很好,我来凑个热闹
i=0的时候你如何输出a[5-0]?你定义的数组最大索引是4
楼上说的都对,我没有补充的了
char a[5];最多存放5个字符,如果用scanf %c一个个字符读进来的话,及时最后一个字符不放\0也没关系
PS:gets并不安全,使用fgets替代