atoi - C stdlib.h

C examples for stdlib.h:atoi

Type

function

From


<cstdlib>
<stdlib.h>

Description

Convert string to integer

Prototype

int atoi (const char * str);

Parameters

Parameter Description
str C-string representation of an integral number.

Return Value

On success, the function returns the converted integral number.

On error, 0 is returned.

Demo Code


#include <stdio.h>
#include <stdlib.h>

int main ()/*from  ww  w.  j  a v  a 2  s. co m*/
{
  int i;
  char buffer[256];

  printf ("Enter a number: ");

  fgets (buffer, 256, stdin);

  i = atoi (buffer);

  printf ("The value entered is %d. Its double is %d.\n",i,i*2);


  printf ("%d\n",atoi("0"));
  printf ("%d\n",atoi("-1"));
  printf ("%d\n",atoi("-2"));
  printf ("%d\n",atoi("222"));
  printf ("%d\n",atoi("123"));

  printf ("%d\n",atoi("a"));
  return 0;
}

Related Tutorials