Use C atoi function to convert string to integer

Syntax

C atoi function has the following syntax.

int atoi(const char *str);

C atoi function is from header file stdlib.h.

Description

C atoi function converts the string to an int value.

Example

Use C atoi function to convert string to integer.


#include <stdlib.h>
#include <stdio.h>
/*from  w ww  . j a va  2s . co m*/
int main(void)
{
  char num1[80], num2[80];
  printf("Enter first: ");
  gets(num1);
  printf("Enter second: ");
  gets(num2);
  printf("The sum is: %d.", atoi(num1)+atoi(num2));

  return 0;
}

The code above generates the following result.





















Home »
  C Language »
    Function Reference »




assert.h
ctype.h
math.h
setjmp.h
signal.h
stdio.h
stdlib.h
string.h
time.h
wctype.h