Akshat Tandon
Akshat Tandon

Reputation: 25

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


Upvotes: 0

Views: 638

Answers (1)

cnicutar
cnicutar

Reputation: 182734

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, i.e. the number of scanned elements.

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

Upvotes: 6

Related Questions