ITOA and sprintf functions Linux C string processing functions

Source: Internet
Author: User
ITOA and sprintf functions Linux C string processing functions
 ITOA functions and atoi functions,C LanguageProvides several standard library functions to convert numbers of any type (integer, long integer, floating point type, etc.) to characters

String. Here is an example of converting integers into strings 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 three parameters: the first parameter is the number to be converted, the second parameter is the target string to write the conversion result, and the third parameter is used when the number is transferred.

. In the preceding example, the conversion base is 10. 10 : Decimal; 2 : Binary...

ITOA is not a standard C function, it is unique to Windows, if you want to write cross-platformProgram, Use sprintf.
It is extended on the Windows platform. The standard library contains sprintf, which is more powerful than this one. Its usage is similar to that of printf:

Char STR [ 255 ];
Sprintf (STR, " % X " , 100 ); // Convert 100 to a hexadecimal string.

The following functions can convert integers into strings:
----------------------------------------------------------
Function Name
----------------------------------------------------------
ITOA () converts an integer value to a string
Ltoa () converts a long integer value to a string
Ultoa () converts an unsigned long integer value to a string


A atoi converts a string to an integer.

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 blank 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 into integer characters
Return Sign * N;

}

ITOA converts an integer to a string.

Example program:

# Include < Ctype. h >
# Include < Stdio. h >
Void ITOA ( Int N, Char S []);
// Atoi function: Convert s to integer
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 symbols
N =- N; // Make n a positive number
I = 0 ;
Do {
S [I ++ ] = N % 10 + ' 0 ' ; // Next Digit
} While (N /= 10 ) > 0 ); // Delete this number

If (Sign < 0 )
S [I ++ ] = ' - ' ;
S [I] = ' /0 ' ;
For (J = I; j > = 0 ; J -- ) // The generated number is in reverse order, so it must be output in reverse order.
Printf ( " % C " , S [J]);

}

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.