Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在scanf中一次性插入2个其他值_C_Scanf - Fatal编程技术网

如何在scanf中一次性插入2个其他值

如何在scanf中一次性插入2个其他值,c,scanf,C,Scanf,我的麻木号码不正确 如何在scanf中一次性插入2个其他值? 或者它做不到 #include <stdio.h> main() { char a,b; int numA,numB; printf("A : "); scanf("%c%d",&a,&numA); printf("B : "); scanf("%c%d",&b,&numB); printf("\n\n%d %d",numA,nu

我的麻木号码不正确

如何在scanf中一次性插入2个其他值? 或者它做不到

#include <stdio.h>
main()
{
    char a,b;
    int numA,numB;

    printf("A : ");
    scanf("%c%d",&a,&numA);

    printf("B : ");
    scanf("%c%d",&b,&numB);

    printf("\n\n%d %d",numA,numB);
 }

问题是读入
b
的值是键入
67
后按下的换行符(回车键)。然后读取
numB
失败,因为它试图将
D
解释为
numB

如果您键入了
s67d56
(即中间没有回车键),那么您将得到正确的输出


要解决此问题,一种方法是将格式字符串更改为
“%c%d”
。空格意味着在尝试读取字符之前,它将使用任何空格。

b
中的第一个数字后面会有一个换行符,然后在
numB
中会有一个未定义的值。使用
“%c%d”
避免此问题;它跳过空白

#include <stdio.h>

int main(void)
{
    char a, b;
    int numA, numB;

    printf("A : ");
    scanf(" %c%d", &a, &numA);

    printf("B : ");
    scanf(" %c%d", &b, &numB);

    printf("\n%d %d\n", numA, numB);
    return 0;
}
#包括
内部主(空)
{
字符a,b;
int numA,麻木;
printf(“A:”);
scanf(“%c%d”、&a和numA);
printf(“B:”);
scanf(“%c%d”,&b,&numB);
printf(“\n%d%d\n”,numA,numB);
返回0;
}

尝试一下,看看它是否有效:

#include <stdio.h>
void main(){
    char a,b,e;
    int c,d;
    printf("A: ");
    scanf("%c %d",&a,&c);
    e=getchar();
    printf("B: ");
    scanf("%c %d",&b,&d);
    printf("%d %d",c,d);
}
#包括
void main(){
字符a、b、e;
int c,d;
printf(“A:”);
scanf(“%c%d”、&a和&c);
e=getchar();
printf(“B:”);
scanf(“%c%d”、&b和&d);
printf(“%d%d”,c,d);
}
#include <stdio.h>
void main(){
    char a,b,e;
    int c,d;
    printf("A: ");
    scanf("%c %d",&a,&c);
    e=getchar();
    printf("B: ");
    scanf("%c %d",&b,&d);
    printf("%d %d",c,d);
}