atoi library c++ code example

Example 1: atoi c++

Parses the C-string str interpreting its content as an integral number

If the converted value would be out of the range of representable
values by an int, it causes undefined behavior.

/* atoi example */
#include <stdio.h>      /* printf, fgets */
#include <stdlib.h>     /* atoi */

int main ()
{
  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);
  return 0;
}

	
/* Output */

Enter a number: 73
The value entered is 73. Its double is 146.

Example 2: what is atoi in strinf

The C library function int atoi(const char *str) converts the 
string argument str to an integer (type int).

Note: It is used in case of string because in strings a number is
also stored as char  and we have to do Typecasting  form (string -> int)

Tags:

Cpp Example