删除和添加白色 space 字符到底发生了什么?
What is really happening on removing and adding white space characters?
我是编程新手,正在学习 C 编程基础知识。我正在学习 scanf()
是如何工作的,但我想现在我很困惑,真的不知道该问什么以及如何问。但我会尽力把我的问题说清楚。
问题
- 我真的无法理解白色的整个概念space。我的意思是什么时候被
scanf
跳过,什么时候不跳过,最大的问题是:他们是如何被跳过的?
连同白色space的概念我也无法理解scanf
函数的工作原理?我在许多书籍和网站以及本网站上都读过它,但它让我更加困惑,因为每个人都有自己讲述任何概念的方式,而且每个人的方式都不一样。
看看这个小程序:
#include<stdio.h>
int main()
{
int num;
char ch;
printf("enter the value of num and ch:\n");
scanf("%d",&num);
scanf("%c",&ch);
printf("num = %d and ch = %c",num,ch);
return 0;
}
我知道在这个程序中,用户将只允许输入 num
的值,因为换行符留在输入缓冲区中,下次 scanf
将输入这个换行符,但如果我们在第二个 scanf 函数中的 %c
之前添加额外的 space 就可以解决。
但是当我将 char ch
变量替换为 int ch
时,scanf
会跳过新行。 为什么?
为什么 scanf 不跳过非白色 space 字符,就像白色 space 例如 - a, b, c, d, @) # etc?
scanf
中的space
和newline
字符有什么区别?我的意思是会有一些例外,对吗?
第一个问题
I mean when they are skip by the scanf and when they are not
White-space 字符被跳过,除非格式说明符是 %c
、%n
或 %[
。 C11标准的相关引用:
7.21.6.2 The fscanf function
[...]
- Input white-space characters (as specified by the
isspace
function) are skipped, unless the specification includes a [
, c
, or n
specifier. 284)
How they are skipped?
只需阅读并丢弃它们。
第二个问题
I'm not able to understand the working of scanf function also?
scanf
是一个 variadic function 意味着它可以接受任意数量的参数,最少有一个。 scanf
解析第一个参数,它是一个字符串文字,因此接受输入。
第三题
But when I replace the char ch variable with int ch, scanf skips the new line. Why?
第一个答案的第一部分对此进行了解释。 %d
将跳过白色space 个字符。
第四题
Why scanf do not skip non-white space character just like whitespace?
对于某些转换说明符,如 %c
,非白色 space 字符是有效输入。他们为什么要跳过它们是没有意义的。对于其他如 %d
,字符(不是数字)是无效输入。 scanf
在发现无效输入时停止扫描并 returns。它是这样设计的。
第五题
What is the difference between space and newline character in scanf?
在scanf
中的格式字符串中放置任何一个都没有区别。尽管它们是不同的字符,但它们都被视为 whitespace characters。当它们用于格式字符串 scanf
时,它们会跳过任意数量的白色 space 字符,包括 none,直到第一个非白色 space 字符。 C11标准的相关引用:
7.21.6.2 The fscanf function
[...]
- A directive composed of white-space character(s) is executed by reading input up to the first non-white-space character (which remains unread), or until no more characters can be read. The directive never fails.
我是编程新手,正在学习 C 编程基础知识。我正在学习 scanf()
是如何工作的,但我想现在我很困惑,真的不知道该问什么以及如何问。但我会尽力把我的问题说清楚。
问题
- 我真的无法理解白色的整个概念space。我的意思是什么时候被
scanf
跳过,什么时候不跳过,最大的问题是:他们是如何被跳过的? 连同白色space的概念我也无法理解
scanf
函数的工作原理?我在许多书籍和网站以及本网站上都读过它,但它让我更加困惑,因为每个人都有自己讲述任何概念的方式,而且每个人的方式都不一样。看看这个小程序:
#include<stdio.h> int main() { int num; char ch; printf("enter the value of num and ch:\n"); scanf("%d",&num); scanf("%c",&ch); printf("num = %d and ch = %c",num,ch); return 0; }
我知道在这个程序中,用户将只允许输入
num
的值,因为换行符留在输入缓冲区中,下次scanf
将输入这个换行符,但如果我们在第二个 scanf 函数中的%c
之前添加额外的 space 就可以解决。但是当我将
char ch
变量替换为int ch
时,scanf
会跳过新行。 为什么?为什么 scanf 不跳过非白色 space 字符,就像白色 space 例如 - a, b, c, d, @) # etc?
scanf
中的space
和newline
字符有什么区别?我的意思是会有一些例外,对吗?
第一个问题
I mean when they are skip by the scanf and when they are not
White-space 字符被跳过,除非格式说明符是 %c
、%n
或 %[
。 C11标准的相关引用:
7.21.6.2 The fscanf function
[...]
- Input white-space characters (as specified by the
isspace
function) are skipped, unless the specification includes a[
,c
, orn
specifier. 284)
How they are skipped?
只需阅读并丢弃它们。
第二个问题
I'm not able to understand the working of scanf function also?
scanf
是一个 variadic function 意味着它可以接受任意数量的参数,最少有一个。 scanf
解析第一个参数,它是一个字符串文字,因此接受输入。
第三题
But when I replace the char ch variable with int ch, scanf skips the new line. Why?
第一个答案的第一部分对此进行了解释。 %d
将跳过白色space 个字符。
第四题
Why scanf do not skip non-white space character just like whitespace?
对于某些转换说明符,如 %c
,非白色 space 字符是有效输入。他们为什么要跳过它们是没有意义的。对于其他如 %d
,字符(不是数字)是无效输入。 scanf
在发现无效输入时停止扫描并 returns。它是这样设计的。
第五题
What is the difference between space and newline character in scanf?
在scanf
中的格式字符串中放置任何一个都没有区别。尽管它们是不同的字符,但它们都被视为 whitespace characters。当它们用于格式字符串 scanf
时,它们会跳过任意数量的白色 space 字符,包括 none,直到第一个非白色 space 字符。 C11标准的相关引用:
7.21.6.2 The fscanf function
[...]
- A directive composed of white-space character(s) is executed by reading input up to the first non-white-space character (which remains unread), or until no more characters can be read. The directive never fails.