Basic usage of sprintf function and sscanf function

sprintf function

The prototype of the sprintf function is  int sprintf(char *str, const char *format, ...)

The function is to format the string, and the specific functions are as follows:

  • Convert a numeric variable to a string.
  • Get hexadecimal and octal strings of integer variables.
  • Concatenate multiple strings.
 

That is, put the string name of the numeric character you want to store in the first parameter, the second is a format such as "%d", and the third parameter is the number to be converted

		

char s[10];
int num;
sprintf(s, "%d", num); //将数字转字符串 

 

sscanf function


 The prototype of the sscanf function is int sscanf(const char *str, const char *format, ...)

Convert the string of parameter str and format the data according to the parameter format string, and the converted result is stored in the corresponding parameter. The specific functions are as follows:

Extract data from a string according to the format. Such as taking out integers, floating-point numbers and strings from strings.

  • Get a string of specified length
  • Get the string up to the specified character
  • Get a string containing only the specified character set
  • Get the string up to the specified character set

 

Simply put, it is mainly possible to convert numbers into strings

The first parameter is the string to be converted, the second is the conversion format, and the third is the integer variable to store the number

Note that the third parameter uses &

int numm;
sscanf(s, "%d", &numm);   //将s数组转为numm数字 

 

Guess you like

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