Code for the conversion of strings and numbers in C language

Source: Internet
Author: User
Tags integer numbers printf sprintf
The following is the C language of the conversion of strings and numbers to implement the code for the analysis, the need for friends can refer to the

1. Convert numbers to Strings
sprintf is almost the same as printf in usage, except that the print destination is different, the former is printed to a string, and the latter is printed directly on the command line.
sprintf is a variable parameter function, defined as follows:
int sprintf (char *buffer, const char *format [, argument] ...);
In addition to the first two parameter types are fixed, any number of parameters can be followed.
Both printf and sprintf use a formatted string to specify the format of the string, using some format specifiers that begin with a "%" (formatted specifications) to occupy a position, and to provide the corresponding variable in the list of arguments in the back. The final function replaces the descriptor with the corresponding position variable to produce a string that the caller wants.
If you print the integer 123 into a string, save it in S.
sprintf (S, "%d", 123);//produce "123"

2. Converting strings to numbers
function Name: ATOL
Function: Converts strings to grow integer numbers
Usage: Long atol (const char *nptr);
program Example:

Copy Code code as follows:


#include <stdlib.h>
#include <stdio.h>
int main (void)
{
Long L;
Char *str = "98765432";
L = atol (str); /* Originally L = Atol (str); */
printf ("string =%s integer =%ldn", str, L);
return (0);
}

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.