简体   繁体   中英

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
45
How to modify it so that it take both the numbers in a single line(after pressing space),ex 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). If you want to you can use a single scanf call with something like:

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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