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  Java code snippet - How to check path in mac?

Leave a Reply

Your email address will not be published. Required fields are marked *

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