Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to convert [character (%c) into [floating point (%f)] in C language?
How to convert [character (%c) into [floating point (%f)] in C language?
The numerical data in 1.C language can be divided into two types: integer type and floating-point type.

Integer: character

Internationalorganizations (same as international organizations)

short

long

Floating point type: float (single precision)

Double (double precision)

Second, floating-point data is transferred to a string.

tea

str[30];

//Define a character array to store data.

double;twofold

d = 123.456;

//define a floating-point variable d

Sprintf (string, "%f",

d

);

//The format string is the same as that of printf ().

sprintf(str," %.2f ",

d

);

//Keep two decimal places, and round the third place.

Third, integer data is transferred to a string.

tea

str[30];

Internationalorganizations (same as international organizations)

I = 123;

sprintf(str,

" %d ",

I

);

Data between 4.0 and 9 are converted into characters.

In C language, when storing character data, what is actually stored is the ascii value of the character. The ascii corresponding to the characters "0" to "9" is continuous, and its value is 48-57. Therefore, when the value of 0-9 is converted into characters, just add' 0', such as:

tea

ch;

Internationalorganizations (same as international organizations)

Me;

I = 1;

ch=i+'0 '

printf("ch=%c

ascii=%d ",

ch,

Lord of honor

);

//Press %c to output, that is, the character 1, and press %d to output the ascii value of 49.