#include <stdio.h>
int main(){
char *s="hello world";
printf("%c\n",s);
}
Run Code Online (Sandbox Code Playgroud)
我用 C 编写了一个小代码。在这段代码的最后一条语句中,我%c在printf()函数中使用了格式说明符并分配了其中命名的指针s。它给我D作为输出。它是返回垃圾值还是我的代码自动在其中分配 ASCII 值或其他什么?
当我添加s+1它时,返回E并s+2返回F等等。任何人都可以澄清我吗?
%c打印 a char,而不是字符串。由于您的变量s不是 a char,而是指向 a 的指针char,printf因此将您的字符串的地址解释为一个字符。您可能想使用以下代码段之一:
printf("%c\n", s[0]); // print the first character in your string
printf("%s\n", s); // print the whole string
Run Code Online (Sandbox Code Playgroud)