The function atoi () in C

The int atoi function (const char * str) in standard C library converts a string pointed to by parameter str to an integer (int type).

Declare atoi () function in C

Below is the declaration for the atoi () function in C:

 int atoi ( const char * str ) 

Parameters

str - This is a string representing an integer.

Returns the value

This function returns an integer that has been converted as an int value. If no valid conversion is performed, the function returns 0.

For example

The following program C illustrates the usage of the atoi () function in C:

 #include #include #include int main () { int val ; char str [ 20 ]; strcpy ( str , "98993489" ); val = atoi ( str ); printf ( "Gia tri duoi dang chuoi = %s, nGia tri duoi dang so nguyen = %dn" , str , val ); strcpy ( str , "QTM.com" ); val = atoi ( str ); printf ( "nGia tri duoi dang chuoi = %s, nGia tri duoi dang so nguyen = %dn" , str , val ); return ( 0 ); } 

Compile and run the above C program to see the results.

According to Tutorialspoint

Previous article: Atof () function in C

Next lesson: Atol () function in C

4 ★ | 1 Vote

May be interested

  • The atol () function in CPhoto of The atol () function in C
    the function long int atol (const char * str) in the standard c library converts a string pointed to by parameter str to some long int.
  • The function strtod () in CPhoto of The function strtod () in C
    the double strtod function (const char * str, char ** endptr) in standard c library converts a string pointed to by parameter str to a floating point number (type double).
  • Function strtol () in CPhoto of Function strtol () in C
    the function long int strtol (const char * str, char ** endptr, int base) in standard c library converts part of the original string in str to a value of long int corresponding to the base base, but must are 2, 8, ..., 36, or special value 0.
  • Function strtoul () in CPhoto of Function strtoul () in C
    the unsigned function long int strtoul (const char * str, char ** endptr, int base) in standard c library converts part of the original string in str to a value of long int corresponding to the base base, given can be 2, 8, ..., 36, or special value 0.
  • Calloc () function in CPhoto of Calloc () function in C
    the function void * calloc (so-phan-tu, kich-co-phan-tu) allocates the requested memory and returns a pointer to it. the difference between malloc and calloc is: malloc does not set memory to 0 while calloc sets the allocated memory to 0.
  • Free () function in CPhoto of Free () function in C
    the void free function (void * ptr) frees the previously allocated memory by a call to calloc, malloc, or realloc