0

I need to convert a char array into int and float using C The array is like this

char* text = "15.34";

I also need to convert a float/int back into an array again

3 Answers 3

7

You can use sscanf also. For example:

float fp = 0; sscanf( text, "%f", &fp );

To convert back use sprintf()

Sign up to request clarification or add additional context in comments.

1 Comment

One should check sscanf's return value to make sure it successfully converted the string into a float, too.
7

Use atoi()/strtol() and atof()/strtod() library functions to convert from string.

To convert back use sprintf() with %d and %f format specifiers.

Comments

0

Take a look at sscanf() and sprintf().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.