首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

关于scanf和printf的字符串分割规则,为什么要用&等等有关问题。求大神回答

2013-03-16 
关于scanf和printf的字符串分割规则,为什么要用&等等问题。求大神回答1.scanf函数的字符串分割规则是怎么样

关于scanf和printf的字符串分割规则,为什么要用&等等问题。求大神回答
1.scanf函数的字符串分割规则是怎么样的,希望可以系统点的回答。比如说scanf("%s%d%c",&name,&age,&sex)我输入了wu 20 m<回车>,那么是怎么分配的。

2.scanf函数为什么要使用&,但是printf就不用

3.
代码1:


#include<stdio.h>

int main(void)
{
char a[5];

scanf("%s",a);
printf("%s\n",a);

return 0;
}

代码2:

#include<stdio.h>

int main(void)
{
char a[5];

scanf("%s",&a);
printf("%s\n",a);

return 0;
}

这两个代码都可以通过编译,并且可以输出正确,那么scanf("%s",&a);和scanf("%s",a);没有区别吗?

百度了找不到想要的答案,希望大神可以帮帮忙
[解决办法]
1.%s在遇到空格后会自动结束输入,按照你的代码实例,你这样的输入,sex得不到预期的那个字符'm'而是20后边那个空格。
2.取地址后才可以在该地址写入数据,而printf是输出用的,只要可以得到要输出的内容就可以。
3.直接写数组名可以退化为指针(数组的首地址),所以a和&a效果一样
[解决办法]
关于第二个问题,实质上也是传值和传址的差别。。。
[解决办法]
&=地址。后面指针也会说,每个值都有存放在内存的地址 
[解决办法]
1.1 当格式串f o r m a t结束时函数返回
1.2 输入字段是一个由非空白符组成的字符串,当遇到空白符或到达最大字段宽(如果有的话)时,对输入字段的读入结束

2. 规定
[解决办法]
第一个问题是“流”的概念,从流里面提取相应的数据(根据类型说明符)
第二个问题,scanf需要往某一地址存东西,所以要用&对简单类型变量取地址;但是printf却是只要取到变量的值即可,不需要地址
第三个问题,a和&a的内容(即存的地址)是一样的,故程序能运行,但是它们的意义是完全两个东西

[解决办法]
Format Specification Fields: scanf and wscanf Functions
A format specification has the following form:

%[*] [width] [{h 
[解决办法]
 l 
[解决办法]
 I64 
[解决办法]
 L}]type

The format argument specifies the interpretation of the input and can contain one or more of the following: 

White-space characters: blank (' '); tab ('\t'); or newline ('\n'). A white-space character causes scanf to read, but not store, all consecutive white-space characters in the input up to the next non–white-space character. One white-space character in the format matches any number (including 0) and combination of white-space characters in the input.


Non–white-space characters, except for the percent sign (%). A non–white-space character causes scanf to read, but not store, a matching non–white-space character. If the next character in stdin does not match, scanf terminates.


Format specifications, introduced by the percent sign (%). A format specification causes scanf to read and convert characters in the input into values of a specified type. The value is assigned to an argument in the argument list. 


The format is read from left to right. Characters outside format specifications are expected to match the sequence of characters in stdin; the matching characters in stdin are scanned but not stored. If a character in stdin conflicts with the format specification, scanf terminates, and the character is left in stdin as if it had not been read.

When the first format specification is encountered, the value of the first input field is converted according to this specification and stored in the location that is specified by the first argument. The second format specification causes the second input field to be converted and stored in the second argument, and so on through the end of the format string.

An input field is defined as all characters up to the first white-space character (space, tab, or newline), or up to the first character that cannot be converted according to the format specification, or until the field width (if specified) is reached. If there are too many arguments for the given specifications, the extra arguments are evaluated but ignored. The results are unpredictable if there are not enough arguments for the format specification.

Each field of the format specification is a single character or a number signifying a particular format option. The type character, which appears after the last optional format field, determines whether the input field is interpreted as a character, a string, or a number. 

The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign (%) is followed by a character that has no meaning as a format-control character, that character and the following characters (up to the next percent sign) are treated as an ordinary sequence of characters, that is, a sequence of characters that must match the input. For example, to specify that a percent-sign character is to be input, use %%.

An asterisk (*) following the percent sign suppresses assignment of the next input field, which is interpreted as a field of the specified type. The field is scanned but not stored.

在每个最后不带\n的printf后面加fflush(stdout);
在每个不想受接收缓冲区旧内容影响的scanf前面加rewind(stdin);
另外请检查scanf的返回值。

------解决方案--------------------


printf里面的%和变量的一一对应关系
scanf里面的%和变量以及变量前加不加&的一一对应关系
是C代码中非常容易出错的地方,而且通常编译还不出错。
所以在编译源代码之前值得专门仔细检查一遍甚至多遍。

热点排行
Bad Request.