You are on page 1of 2

Atoi

int atoi ( const char * str );

Convert string to integer Parses the C string str interpreting its content as an integral number, which is returned as an int value. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plusor minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. Example /* atoi example */ #include <stdio.h> #include <stdlib.h> int main () { int i; char szInput [256]; printf ("Enter a number: "); fgets ( szInput, 256, stdin ); i = atoi (szInput); printf ("The value entered is %d. The double is %d.\n",i,i*2); return 0; } Output: Enter a number: 73 The value entered is 73. The double is 146. Atof double atof ( const char * str ); Convert string to double Parses the C string str interpreting its content as a floating point number and returns its value as a double. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes as many characters as possible that are valid following a syntax resembling that of floating point literals, and interprets them as a numerical value. The rest of the string after the last valid character is ignored and has no effect on the behavior of this function. A valid floating point number for atof is formed by a succession of: An optional plus or minus sign A sequence of digits, optionally containing a decimal-point character An optional exponent part, which itself consists on an 'e' or 'E' character followed by an optional sign and a sequence of digits. If the first sequence of non-whitespace characters in str does not form a valid floating-point number as just defined, or if no such sequence exists because either str is empty or contains only whitespace characters, no conversion is performed. Example /* atof example: sine calculator */ #include <stdio.h> #include <stdlib.h> #include <math.h> int main () {

double n,m; double pi=3.1415926535; char szInput [256]; printf ( "Enter degrees: " ); gets ( szInput ); n = atof ( szInput ); m = sin (n*pi/180); printf ( "The sine of %f degrees is %f\n" , n, m ); return 0; } Output: Enter degrees: 45 The sine of 45.000000 degrees is 0.707101 Atol long int atol ( const char * str ); Convert string to long integer Parses the C string str interpreting its content as an integral number, which is returned as a long int value. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plusor minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. Example /* atol example */ #include <stdio.h> #include <stdlib.h> int main () { long int li; char szInput [256]; printf ("Enter a long number: "); gets ( szInput ); li = atol (szInput); printf ("The value entered is %d. The double is %d.\n",li,li*2); return 0; } Output: Enter a number: 567283 The value entered is 567283. The double is 1134566.

You might also like