C-language---integer string conversions

Source: Internet
Author: User
Tags sprintf

The C language provides several standard library functions that convert numbers of arbitrary types (integer, long, float, and so on) to strings. Here 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 parameter is the number to be converted, the second is the target string to write the result of the conversion, and the third 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, use sprintf.
is extended under the Windows platform, the standard library has the sprintf, the function is stronger than this, the use method is similar to printf:

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

Function Name: ATOL

Function: Convert string to grow integer number

Usage: Long atol (const char *nptr);

program Example:

#include <stdlib.h>

#include <stdio.h>

int main (void)

{

Long L;

Char *str = "98765432";

L = atol (str); /* Originally for L = Atol (LSTR); */

printf ("string =%s integer =%ld/n", str, L);

return (0);

}

ATOL (Convert string to growth integer)

Correlation function Atof,atoi,strtod,strtol,strtoul

Table header file #include <stdlib.h>

Defines a function long atol (const char *nptr);

The function Description Atol () scans the reference nptr string, skips the preceding space character until it encounters a number or sign to start the conversion, and then encounters a non-numeric or string ending ('/0 ') before ending the conversion and returning the result.

The return value returns the number of long integers after the conversion.

Additional Instructions Atol () with the use of Strtol (nptr, (char**) null,10);

Example/* Converts string A to string B into a number and adds a */

#include <stdlib.h>

Main ()

{

Char a[]= "1000000000";

Char b[]= "234567890";

Long C;

C=atol (a) +atol (b);

printf ("c=%d/n", c);

}

Run c=1234567890

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.