免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2135 | 回复: 7
打印 上一主题 下一主题

[C] a simple and fundamental question about scanf. [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-01-24 01:34 |只看该作者 |倒序浏览
printf("\nEnter a value: "); /* Prompt for the next value */
scanf(" %lf", &value);       /* Read the next value       */
total += value;              /* Add value to total        */
++count;                     /* Increment count of values */

question is:

why put a space before the %lf in scanf? only if put the space this part can be executed correctly.

thanks

论坛徽章:
0
2 [报告]
发表于 2009-01-24 04:47 |只看该作者
No, you do not need this extra space.

论坛徽章:
0
3 [报告]
发表于 2009-01-24 09:51 |只看该作者
不需要的, 就像你不需要用英文发贴一样.

论坛徽章:
0
4 [报告]
发表于 2009-01-24 10:40 |只看该作者
funny。。。

论坛徽章:
0
5 [报告]
发表于 2009-01-25 09:36 |只看该作者
sorry about this. I did not install Scim.

If we do not enter the space, we will not see scanf is waiting for your input.  Very strange problem. The header ctype.h is included besides stdio.h.

BTW, i use the eclipse to build.

论坛徽章:
0
6 [报告]
发表于 2009-01-25 11:56 |只看该作者

回复 #5 gaohua97115 的帖子

why, scanf is but always a blocking function

论坛徽章:
0
7 [报告]
发表于 2009-01-25 13:11 |只看该作者
This is a example from <<Beginning C From Novice to Professional>> ( Fourth Edition)

/* Program 4.6 The almost indefinite loop - computing an average */
#include <stdio.h>
#include <ctype.h>           /* For tolower() function */
int main(void)
{
  char answer = 'N';        /* Records yes or no to continue the loop */
  double total = 0.0;       /* Total of values entered                */
  double value = 0.0;       /* Value entered                          */
  int count = 0;            /* Number of values entered               */
  printf("\nThis program calculates the average of"
                                      " any number of values.");
  for( ;; )                           /* Indefinite loop */
  {
    printf("\nEnter a value: ");      /* Prompt for the next value */
    scanf(" %lf", &value);            /* Read the next value       */
    total += value;                   /* Add value to total        */
    ++count;                          /* Increment count of values */
    /* check for more input */
    printf("Do you want to enter another value? (Y or N): ");
    scanf(" %c", &answer);            /* Read response Y or N   */
    if(tolower(answer) == 'n')        /* look for any sign of no */
      break;                          /* Exit from the loop      */
  }
  /* output the average to 2 decimal places */
  printf("\nThe average is %.2lf\n", total/count );
  return 0;
}


try this program with and without that space. you will see the difference.

thanks.

论坛徽章:
0
8 [报告]
发表于 2009-01-25 15:52 |只看该作者
原来是没装输入法, 囧.


scanf(" %lf", &value);            /* Read the next value       */

注意这行, 你前面加了一个空格, 那么你的输入就要按照你的格式字符串的形式来输入.

如果是
scanf("%d,%d", &a, &b);  那么你的输入也以逗号隔开.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP