scanf函数的行为() [英] Behaviour of scanf()

查看:156
本文介绍了scanf函数的行为()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  混乱()与&安培;运营商


我们为什么需要急症室;在scanf函数用于输入整数,为什么不为字符。
执行和放大器;在scanf函数是指merory位置同时获得输入。

例如: -

 的main()
{
int类型的;
焦炭℃;scanf函数(%d个,&安培; A);
scanf函数(%CC);
}


解决方案

对于每个转换说明, scanf()的预计,相应的参数是一个指向正确的类型:%d个预计键入为int * %F 预计,键入双* %C %S 都期望类型的参数的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()的调用) ,它会隐式地从类型转换除权pression T 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 & operator

Why 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 type int *, %f expects an argument of type double *, %c and %s both expect an argument of type char *, etc.

The difference between %c and %s is that the former tells scanf() to read a single character and store it in the location specified by the corresponding argument, while the latter tells scanf() 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 as buf in the scanf() call), it will implicitly convert the expression from type N-element array of T to pointer 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 like buf = foo). The only exceptions to this rule are when the array expression is an operand of either the sizeof 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 type char [SIZE] to char *, so we don't need to use the & operator, and in fact the type of the expression &buf would be pointer to SIZE-element array of char, or (*)[SIZE], which is not what scanf() expects for the %s conversion specifier.

这篇关于scanf函数的行为()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆