scanf函数的行为() [英] Behaviour of scanf()
问题描述
可能重复:结果
混乱()与&安培;运营商
块引用>我们为什么需要急症室;在scanf函数用于输入整数,为什么不为字符。
执行和放大器;在scanf函数是指merory位置同时获得输入。例如: -
的main()
{
int类型的;
焦炭℃;scanf函数(%d个,&安培; A);
scanf函数(%CC);
}
解决方案对于每个转换说明,
scanf()的
预计,相应的参数是一个指向正确的类型:%d个
预计键入为int *
,%F $ C $的参数C>预计,键入
双*
,%C
和%S $的参数C $ C>都期望类型的参数
的char *
等
%C
和%S
的区别在于,前者让scanf函数( )
来读的单的字符并将其存储在由相应的参数指定的位置,而后者则讲述scanf()的
读取多个字符,直到它看到一个0值字符并存储在缓冲区中开始参数所指定的位置的所有这些字符。您需要使用
&安培;
运营商对你的论点,如果他们是不是已经指针类型。例如:INT X;
为int * PX = some_valid_memory_location_such_as_&放大器; X;
焦炭℃;
字符* PC = some_valid_memory_location_such_as_和C;
...
scanf函数(%d个,&安培; X); // x不是一个指针类型,所以我们必须使用&放大器;操作者
scanf函数(%D,像素); //像素为指针类型,所以我们不需要和放大器;操作者
scanf函数(%C,和C); //等等。
scanf函数(%C,PC); //等等。当事情变得扑朔迷离正在读的字符串的字符(使用
%S
转换说明):字符BUF [SIZE]
scanf函数(%S,BUF); //前pression''BUF * *隐式转换为指针类型为什么不我们需要的
&安培;
运营商在这种情况下?它用C如何处理数组前pressions做。当编译器看到数组类型的前pression(如BUF
在scanf()的
调用) ,它会隐式地从类型转换除权pressionT
N个元素的数组来指针至T
,和其值设置为阵列中的第一个元素的地址。这个值不是一个左值 - 它不能被分配到(所以你不能写东西像BUF = foo的
)。唯一的例外是当数组前pression要么是的sizeof
或&放的操作;
运营商,或者如果数组前pression是一个字符串用来初始化另一个数组:的char * p =这是一个测试; //字符串隐式转换为char *,
//字符串*地址*写为p
所以char a [] =这是一个测试; //字符串不是隐式转换,
//字符串* *的内容复制到总之,前pression
BUF
是隐的从类型转换的char [SIZE]
到的char *
,所以我们并不需要使用&安培;
运营商,并在事实上,前pression类型&放大器; BUF
是字符指针的大小元素的数组
,或(*)[SIZE]
,这是不是有什么scanf()的
预计为%S
转换说明。Possible Duplicate:
confusion in scanf() with & operatorWhy we need a & in scanf for inputting integer and why not for characters. Do the & in scanf refers to merory location while getting input.
Eg:-
main() { int a; char c; scanf("%d",&a); scanf("%c"c); }
解决方案For each conversion specifier,
scanf()
expects the corresponding argument to be a pointer to the proper type:%d
expects an argument of typeint *
,%f
expects an argument of typedouble *
,%c
and%s
both expect an argument of typechar *
, etc.The difference between
%c
and%s
is that the former tellsscanf()
to read a single character and store it in the location specified by the corresponding argument, while the latter tellsscanf()
to read multiple characters until it sees a 0-valued character and store all those characters in the buffer starting at the location specified by the argument.You need to use the
&
operator on your arguments if they are not already of pointer type. For example:int x; int *px = some_valid_memory_location_such_as_&x; char c; char *pc = some_valid_memory_location_such_as_&c; ... scanf("%d", &x); // x is not a pointer type, so we must use the & operator scanf("%d", px); // px is a pointer type, so we don't need the & operator scanf("%c", &c); // etc. scanf("%c", pc); // etc.
Where things get confusing is reading strings of characters (using the
%s
conversion specifier):char buf[SIZE]; scanf("%s", buf); // expression 'buf' *implicitly* converted to pointer type
Why don't we need the
&
operator in this case? It has to do with how C treats array expressions. When the compiler sees an expression of array type (such asbuf
in thescanf()
call), it will implicitly convert the expression from typeN-element array of T
topointer to T
, and set its value to the address of the first element in the array. This value is not an lvalue -- it cannot be assigned to (so you can't write something likebuf = foo
). The only exceptions to this rule are when the array expression is an operand of either thesizeof
or&
operators, or if the array expression is a string literal being used to initialize another array:char *p = "This is a test"; // string literal implicitly converted to char *, // string *address* written to p char a[] = "This is a test"; // string literal not implicitly converted, // string *contents* copied to a
In short, the expression
buf
is implicitly converted from typechar [SIZE]
tochar *
, so we don't need to use the&
operator, and in fact the type of the expression&buf
would bepointer to SIZE-element array of char
, or(*)[SIZE]
, which is not whatscanf()
expects for the%s
conversion specifier.这篇关于scanf函数的行为()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!