String to numeric type for C language string operations in Linux

Source: Internet
Author: User

1. Convert string to INTEGER (1)
# include
int atoi (const char * nptr );
string to integer
long atol (const char * nptr );
convert a string to a long integer
long Atoll (const char * nptr );
long atoq (const char * nptr );
converting string to long
the English manual is simple, directly
Note: The atoi () function converts the initial portion of the string pointed to by nptr to int. the behavior is the same as strtol (nptr, (char **) null, 10); records t that atoi () does not detect errors. the atol () and Atoll () functions behave the same as atoi (), doesn't that they convert the initial portion of the string to their return type of long or long. atoq () is an obsolete name for atoll ().

2. Convert string to integer type (2)
# Include <stdlib. h>
Long int strtol (const char * nptr, char ** endptr, int base );
String to a long integer. The base parameter is in hexadecimal format. If the base parameter is in hexadecimal format, the base value is 10.
Long long int strtoll (const char * nptr, char ** endptr, int base );
Convert string to long int
Note: For more information, see the man manual.

3. Convert string to floating point
# Include <stdlib. h>
Double strtodd (const char * nptr, char ** endptr );
String to double-precision floating-point Double Type
Float strtof (const char * nptr, char ** endptr );
Convert string to single-precision floating point numberFloat Type
Long Double strtold (const char * nptr, char ** endptr );
Convert string to longDouble Type

With the above database functions, you can easily convert strings into numeric types. It's really cool and convenient. Is there any wood?

ExampleCode:

# Include <stdio. h>
# Include <stdlib. h>
Int main ()

{

Char * str_int = "892 ";
Int int_val = atoi (str_int );
Printf ("Convert string to integer: % d \ n", int_val );
Long long_val = atol (str_int );
Printf ("string to long integer: % LD \ n", long_val );
Char x str_float = "238.23 ";
Char * endptr;
Float float_val = strtof (str_float, & endptr );
Printf ("Convert string to single-precision floating point type: % F \ n", float_val );
Double double_val = strtodd (str_float, & endptr );
Printf ("Convert string to Double Precision Floating Point: % F \ n", double_val );
Char * str_long = "9839282 ";
Int base = 10;
Long long_v = strtol (str_long, & endptr, base );
Printf ("strtol: % LD \ n", long_v );
Return 0;
}

Code output:

    1. Convert string to integer: 892
    2. String to a long integer: 892
    3. Character string to single-precision floating point: 238.229996
    4. String to double-precision floating point: 238.230000
    5. Strtol: 9839282


Note: The man manual of each function has a detailed explanation. For more information, see the man manual.

Related Article

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.