How to extract multiple floats from a string of floats in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    char input[] = "2.809 -36.91 21.487 -493.808 5034.7001";
    float floats[5]; 

    char *token = strtok(input, " ");
    int i = 0;
    
    while (token != NULL) {
        float f = atof(token); 
        printf("%f\n", f); 
        floats[i++] = f;
        token = strtok(NULL, " ");
    }
    
    return 0;
}


 
 
 
 
/*
run:
 
2.809000
-36.910000
21.487000
-493.808014
5034.700195
 
*/

 



answered Jul 28, 2024 by avibootz
...