C language number and string conversion between the method

  • 2020-04-01 21:40:34
  • OfStack

Char *itoa(int value, char *string,int radix);

Convert decimal to string: sprintf(string, format control column, data);

String to decimal: double atof(const char * NPTR);

Int atoi(const char * NPTR);

Test code:


#include<stdio.h> 
 #include<stdlib.h>
 int main()
 {
     int a=2013420;
     float b=2.054f;
     double c=5.24;
     char sa[20],sb[20],sc[20];
     //Converts the integer a to a string
     itoa(a,sa,10);
     puts(sa);
     //Converts floating point data to a string
     sprintf(sb,"%g",b);
     puts(sb);
     //Converts double data to a string
     sprintf(sc,"%lg",c);
     puts(sc);
     printf("======== The following is a string conversion to a value =========n");
     char *s1="123",*s2="1.23";
     printf("%dn",atoi(s1));
     printf("%gn",atof(s2));
     getchar();
     return 0;
 }


Related articles: