简体   繁体   English

如何用C语言在一行中输入2个输入?

[英]How to take 2 inputs in a single line in C language?

    for(i=0;i<t;++i)
{
    scanf("%d",&arr[i]);
    scanf("%d",&brr[i]);
    a=arr[i];
    b=brr[i];
}

This code block is taking inputs in two separate line(after pressing enter),ex 该代码块在两条单独的行中接受输入(按回车后),例如
12 12
45 45
How to modify it so that it take both the numbers in a single line(after pressing space),ex 12 45 如何修改它以便将两个数字都放在一行中(按空格后),例如12 45


How to modify it so that it take both the numbers in a single line(after pressing space) 如何修改它以便将两个数字都放在一行中(按空格键之后)

Your code already does this (it already works if you pass "12 45" - you can put any amount of whitespace between them). 您的代码已经做到了这一点(如果您传递“ 12 45”,则已经可以使用-您可以在它们之间放置任意数量的空格)。 If you want to you can use a single scanf call with something like: 如果您愿意,可以将单个scanf调用与以下内容一起使用:

scanf("%d %d", &arr[i], &brr[i]);

When using scanf it is a wise decision to check the return code, ie the number of scanned elements . 使用scanf时,明智的选择是检查返回码,即扫描的元素数

rc = scanf(...);
if (rc != 2)
    /* We scanned less than we expected! */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM