Skip to content

How to take multiple inputs in one line in C?

For fixed number of inputs:

int x[3];
printf("Enter three integers, separated by spaces: ");
scanf("%d %d %d", &x[0], &x[1], &x[2]);
printf("You entered %d, %d, and %d.\n", x[0], x[1], x[2]);

For dynamic inputs size:

int count = 0, sum = 0;
printf("how many do you want to enter: ");
scanf("%d", &count);

int *num = malloc(sizeof(int)*count);

for(int i = 0; i < count; i++) {
    scanf("%d ", &num[i]);
    //sum += num[i];
}

See also  Python code snippet - How to iterate through a pandas dataframe?

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.