C language realizes the conversion of string and number

Convert number to string

Use the sprintf function, included in the stdio.h header file

Usage: sprintf (character array, number type, number to be converted)

Numbers are converted to strings and stored in character arrays

#include <cstdio>
using namespace std;

char s[10];

int main(void)
{
    
    
	int n = 100;
	sprintf(s, "%d", n);
	printf("%s\n", s);
	
	return 0;
}

Convert string to number

Use the atoi function, included in the algorithm header file

Usage: atoi (string to be converted)

Return the converted number

#include <cstdio>
#include <algorithm>
using namespace std;

char s[10] = {
    
    "100"};

int main(void)
{
    
    
	int n = atoi(s);
	printf("%d\n", n);
	
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43772166/article/details/108592054