How to check if scanf successfully takes 3 user inputs in C

1 Answer

0 votes
#include <stdio.h>

int main()
{
    char s[16] = "";
    int i;
    float f;
    
    printf("Enter float int and string: ");

    if (scanf("%f %d %s", &f, &i, s) != 3) {
        fputs("Invalid input\n", stderr);
    }
    else {
        printf("%f %d %s", f, i, s);
    }
    
    return 0;
}



/*
run:

Enter float int and string: 3.14 7284 abcd
3.140000 7284 abcd

*/

 



answered May 22, 2023 by avibootz

Related questions

1 answer 145 views
1 answer 152 views
1 answer 129 views
1 answer 118 views
118 views asked Aug 8, 2024 by avibootz
...