C--Supplementary knowledge points about C

1. C language sprintf() function: write formatted data to a string

One of the most common applications of sprintf() is to print integers into strings, such as:
    sprintf(s, "%d", 123); //Print the integer 123 as a string and store it in s
    sprintf(s , "%8x", 4567); //lowercase hexadecimal, width occupies 8 positions, right-aligned

The role of sprintf is to output a formatted string to a destination string, while printf is to output a formatted string to the screen. The first parameter of sprintf should be the destination string. If this parameter is not specified, the prompt "The program has generated an illegal operation and is about to be closed..." will appear during the execution.

2.

%a,%A read a floating point value (only valid for C99)  

%c reads a character     

%d reads in a decimal integer     

%i reads in decimal, octal , hexadecimal integers     

%o reads in an octal integer     

%s reads a string, terminated by a space, tab, or newline.     

%f, %F, %e, %E, %g, %G are used to input real numbers, which can be input in decimal form or exponential form.     

%p reads in a pointer     

%x means hexadecimal output (output variable address in hexadecimal)

%u reads in an unsigned decimal integer     

%n Equivalent characters of the value read so far

3.


Explanation 1:
Function: Convert an integer to a string
Usage: char *itoa(int value, char *string, int radix);
Detailed explanation: itoa is an English integer to array (convert an int integer into a string, and store the value in the array string) abbreviation.
Parameters:
value: The integer to be converted.
radix: It means the base number, that is, first convert the value into a radix number in the range of 2-36, such as 10 for decimal, and 16 for hexadecimal.
* string: Save the converted string.
Return value:
char * : Point to the generated string, same as *string.
Remark: The header file for this function is " stdlib.h "


Explanation 2:
itoa() function

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

Prototype Description:

value: The data to be converted. 
string: The address of the target string.
radix: The converted base number, which can be decimal, hexadecimal, etc. The range must be 2-36.

Function: Convert the integer value into a string and store it in the memory space pointed to by string. radix is ​​the base used for conversion (the base of the data stored in the string).

Return value: The function returns a pointer to str and returns without error.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325687540&siteId=291194637
Recommended