How to convert float value to string in C

2 Answers

0 votes
#include <stdio.h> 
#include <stdlib.h> 
 
int main(int argc, char **argv)
{ 
    float f = 123.4567; 
    char s[8]; 
  
    gcvt(f, 7, s); 
  
    printf("%s\n", s); 
     
    return 0; 
}   
 
 
/*
run:
 
123.4567
 
*/

 



answered Jan 13, 2019 by avibootz
0 votes
#include <stdio.h> 

int main(int argc, char **argv)
{ 
	char s[10]; 
    float f = 123.4567; 
	
    sprintf(s, "%f", f); 
    printf("%s\n", s); 
     
    return 0; 
}   
 
 
/*
run:
 
123.456703
 
*/

 



answered Jan 14, 2019 by avibootz

Related questions

2 answers 317 views
317 views asked May 22, 2024 by avibootz
1 answer 222 views
1 answer 183 views
183 views asked Feb 11, 2023 by avibootz
1 answer 212 views
212 views asked Jan 23, 2023 by avibootz
2 answers 246 views
246 views asked Jul 30, 2022 by avibootz
1 answer 227 views
227 views asked Dec 28, 2018 by avibootz
...