Linux C library functions Encyclopedia: string Processing functions

Source: Internet
Author: User
Tags header include integer printf linux

1. Atof (convert string to floating-point number)

Related functions

Atoi,atol,strtod,strtol,strtoul

Table header File

#include <stdlib.h>

Defining functions

Double atof (const char *nptr);

Function description

Atof () scans the parameter nptr string and skips the preceding space character until it encounters a number or sign to begin the conversion, and then ends the conversion without a number or at the end of the string, returning the result. The parameter nptr string can contain a positive sign, a decimal point, or E (e) to represent the exponent portion, such as 123.456 or 123e-2.

return value

Returns the number of floating-point types after conversion.

Additional Instructions

Atof () is the same as the result of using Strtod (nptr, (char**) NULL).

Example

/* Convert string A and string B to number after adding/*

#include <stdio.h> 
#include <stdlib.h> 
main () 
{ 
    char *a= " -100.23"; 
    Char *b= "200e-2"; 
    float C; 
    C=atof (a) +atof (b); 
    printf ("c=%.2f\n", c); 
}

2.atoi (convert string to Integer)

Related functions:

Atof,atol,atrtod,strtol,strtoul

Table header file:

#include <stdlib.h>

To define a function:

int atoi (const char *nptr);

Function Description:

Atoi () scans the parameter nptr string and skips the preceding space character until it encounters a number or sign to begin the conversion, and then ends the conversion without a number or at the end of the string, returning the result.

return value:

Returns the converted integer number.

Additional Instructions:

Atoi () with the use of Strtol (nptr, (char**) null,10);

Example:

/* Convert string A and string B to number after adding/*

#include <stdio.h> 
#include <stdlib.h> 
main () 
{ 
    char a[]= "; 
    " Char b[]= "256"; 
    int C; 
    C=atoi (a) +atoi (b); 
    printf ("c=%d\n", c); 
}

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.