C language Itoa () function and atoi () function (integer to character)

Source: Internet
Author: User
Tags sprintf

Http://c.biancheng.net/cpp/html/792.html

The C language provides several standard library functions that convert numbers of any type (integer, long, float, and so on) to a string.

The following is an example of converting an integer to a string using the Itoa () function:
# include <stdio.h>
# include <stdlib.h>
void Main (void)
{
int num = 100;
Char str[25];
ITOA (num, str, 10);
printf ("The number ' num ' is%d and the string ' str ' is%s. \ n",
num, str);
}

The itoa () function has 3 parameters: The first argument is the number to be converted, the second argument is the target string to write the result of the conversion, and the third argument is the cardinality used to transfer the number. In the example above, the conversion cardinality is 10. 10: decimal; 2: Binary ...

Itoa is not a standard C function, it is unique to Windows, if you want to write a cross-platform program, please use sprintf. is expanded under the Windows platform, the standard library has sprintf, the function is stronger than this, the usage is similar to printf:

Char str[255];
sprintf (str, "%x", 100); A string that converts 100 to a 16 binary representation.

The following functions can convert integers to strings:
----------------------------------------------------------
Function name with
----------------------------------------------------------
Itoa () converts an integer value to a string
Itoa () Converts a long integer value to a string
Ultoa () converts an unsigned long value to a string

one, Atoi ()--Converts a string to an integer number


Example program:
#include <ctype.h>
#include <stdio.h>
int atoi (char s[]);
int main (void)
{
Char s[100];
Gets (s);
printf ("integer=%d\n", Atoi (s));
return 0;
}
int atoi (char s[])
{
int i,n,sign;
For (I=0;isspace (S[i]); i++)//skip whitespace characters;
sign= (s[i]== '-')? -1:1;
if (s[i]== ' + ' | | s[i]== '-')//Skip Symbol
i++;
For (N=0;isdigit (S[i]); i++)
n=10*n+ (s[i]-' 0 ');//convert numeric characters to shaping numbers
return sign *n;
}

Second, itoa ()--Converts an integer to a string


Example program:
#include <ctype.h>
#include <stdio.h>
void itoa (int n,char s[]);
Atoi function: Converting s to shaping number
int main (void)
{
int n;
Char s[100];
printf ("Input n:\n");
scanf ("%d", &n);
printf ("The string: \ n");
Itoa (n,s);
return 0;
}
void itoa (int n,char s[])
{
int i,j,sign;
if ((sign=n) <0)//record symbol
n=-n;//make n a positive number
i=0;
do{
s[i++]=n%10+ ' 0 ';//Remove a number
}
while ((n/=10) >0);//Delete the number
if (sign<0)
s[i++]= '-';
s[i]= ' + ';
for (j=i;j>=0;j--)//generated number is reverse order, so to reverse the output
printf ("%c", S[j]);
}
is a function of int to string type

C language Itoa () function and atoi () function (integer to character)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.