定义字符串会自动在后边添加\0.
char *p = "hello";
for(i=0;i<7;i++)
{
printf("%d\n",p[i]);
}
104 101 108 108 111 0 115
注意危险函数:
1、危险函数gets()函数,应该使用fgets()
char *gets (char *__s)
//从标准输入读入字符串
使用 char *fgets(char *s, int size, FILE *stream),其中第二个参数使用 buf 的 size 就可以, fgets 函数会保证最后一个字节是’\0’且不会越界。
#include
#include
void main()
{
char buf[5] = {0};
int i = 0;
fgets(buf,5,stdin);
printf("buf = %s\n",buf);
for(i=0;i<5;i++)
{
printf("buf[0] = %d\n",buf[i]);
}
}
2、scanf很危险 禁止使用
int scanf (const char *__restrict __format, ...);
在程序输入字符串的时候,如果输入的字符串超过了指定的长度,会使程序崩溃,造成溢出错误
使用 fge