Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,872 questions

51,795 answers

573 users

How to parse string to multiple long double numbers in C

2 Answers

0 votes
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    char s[] = "3.14 8721.809 52345.81";
    char *end;
    long double n1, n2, n3;
     
    n1 = strtold(s, &end);
    n2 = strtold(end, &end);
    n3 = strtold(end, NULL);
     
    printf("%g %g %g\n", (double)n1, (double)n2, (double)n3);
}
  
/*
 
run:
 
3.14 8721.81 52345.8
 
*/

 



answered Jul 20, 2018 by avibootz
edited Jul 20, 2018 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main()
{
    char *p = "3.14 8721.809 52345.81";
    char *end;
    
    for (long double n = strtold(p, &end); p != end; n = strtold(p, &end)) {
        p = end;
        if (errno == ERANGE) {
            printf("range error");
            errno = 0;
        }
        printf("n = %g\n", (double)n);
    }
}
 
/*

run:

n = 3.14
n = 8721.81
n = 52345.8

*/

 



answered Jul 20, 2018 by avibootz

Related questions

1 answer 186 views
1 answer 175 views
1 answer 116 views
1 answer 149 views
1 answer 285 views
285 views asked Jun 20, 2021 by avibootz
1 answer 57 views
1 answer 91 views
...