scanf函数其执行点之前的作品 [英] scanf works before its execution point

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

问题描述

我试过一个简单的program..function整数返回和字符pointer..after我运行codeI发现了一些奇怪的心眼儿scanf..I试图打印消息(进入A,B :)阅读两个整型输入和消息(进入C,D :)阅读两个char inputs.but在运行time..i发现,对于焦炭C输入读rightafter我输入的输入,A,b ..
对于例如:
输入A,B:10
20
进入C,D:摹
这20后获取只有一个输入(D)和输入c为换行 ..
对于如2:
输入A,B:10
20A
进入C,D:摹
它获取只有一个输入(D)和C输入为 20后..
这是为什么happening..please澄清

 为int *添加为(int *一,为int * B)
{
    返回(* A> * B A:B);
}
字符* charret(字符* C,字符* D)
{
     回报(* C> * D C:D);
 }诠释的main()
{
    诠释A,B;
    焦C,D;    的printf(\\ n \\ t \\ tFUNCTION RETURNING整型指针\\ n \\ t的);
    的printf(请输入号码A和B:);
    scanf函数(%D,&安培;一,和b);
    的printf(\\拉幅字符c:);
    scanf函数(%C%C,和C,和D);
    的printf(以下简称Biggestt值为:%d个\\ n \\ t的,*添加(安培;一,和b));
    的printf(\\ n \\里边反字符c =%C喜D =%C,C,D);
 // scanf函数(%C,和D);
    的printf(\\ n \\里边反Biggestt值为:%C,* charret(和C,和D));
    残培();
    返回0;
}


解决方案

%C 将读取的任何的字符,包括换行符您previous条目。如果你想读的第一个非空白字符,您的格式字符串添加%C之间有一个空格:

  scanf函数(%C%C,和C,和D);
        / * ^增值空间* /

这将导致 scanf()的读取字符之前吃任何数量的空格中。

I tried a simple program..function returning integer and character pointer..after i run that code i found some weird acting by scanf..I tried to print message(enter a,b:) read two integer inputs and message(enter c,d:) read two char inputs.but at run time..i found that input for the char c is read rightafter i enter the inputs for a,b.. for eg: enter a,b: 10 20 enter c,d: g it gets only one input(for d) and input for c is newline after 20.. for eg 2: enter a,b: 10 20a enter c,d: g it gets only one input(for d) and input for c is a after 20.. why is this happening..please clarify it

 int* add(int *a,int *b)
{
    return (*a>*b?a:b);
}  
char* charret(char *c,char *d)
{
     return (*c>*d?c:d);
 }

int main()
{
    int a,b;
    char c,d;

    printf("\n\t\tFUNCTION RETURNING INTEGER POINTER\n\t");
    printf("Enter the Number A and B:");
    scanf("%d %d",&a,&b);
    printf("\tEnter the character c :");
    scanf("%c %c",&c,&d);
    printf("The Biggestt Value is : %d\n\t",*add(&a,&b));
    printf("\n\tThe character c= %c hi d= %c",c,d);
 //   scanf("%c",&d);
    printf("\n\tThe Biggestt Value is : %c", *charret(&c,&d));
    getch();
    return 0;
}

解决方案

%c will read any character, including the newline character from your previous entry. If you want to read the first non-whitespace character, add a space before %c in your format string:

    scanf(" %c %c",&c,&d);
        /* ^ added space */

This will cause scanf() to eat any number of whitespaces before reading the character.

这篇关于scanf函数其执行点之前的作品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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