gpt4 book ai didi

c - 为什么 scanf 函数不接受转义序列作为输入?

转载 作者:行者123 更新时间:2023-11-30 18:33:03 25 4
gpt4 key购买 nike

为什么 scanf 函数不接受转义序列作为输入?

代码

#include <stdio.h>

int main(){
char str[100];

printf("Enter value: ");
scanf("%s", str);

printf("\nYou Entered: %s", str);

}

输出

Enter value: hello \n world
You Entered: hello

最佳答案

%s 仅读取输入中的第一个空白字符 - 在本例中为空格。因此,它只识别“hello”,因为输入的其余部分尚未被读取。

话虽如此,scanf can't interpret escaped characters 。您可以在输入中放置实际换行符,但不能使用转义字符并期望它正确解析它们。

In C/C++ ,如果字符串文字中有 \n ,它将被打印为换行符(编译器会这样做),但如果您的输入中有 \n ,它会被打印为换行符将按字面解释。

关于c - 为什么 scanf 函数不接受转义序列作为输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59631479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com