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 207 views
207 views asked May 22, 2024 by avibootz
1 answer 135 views
1 answer 106 views
106 views asked Feb 11, 2023 by avibootz
1 answer 129 views
129 views asked Jan 23, 2023 by avibootz
2 answers 149 views
149 views asked Jul 30, 2022 by avibootz
1 answer 140 views
140 views asked Dec 28, 2018 by avibootz
...