Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

atoi - convert string to integer in C

September 8, 2013 Posted by: GPA Views: 720 0 comments

Problem

Write simple program that convert string value to integer.

Solution

In this article, you will know how to convert a string value into integer value in C. In the given example we have used the library function atoi() of header file <stdlib.h>, to convert the input string into integer value.

Declaration:

int atoi(const char *inputString); 

where inputString is the string to be converted. 

This function converts its argument a string of digits  to a int value. It returns the int value. If the converted value cannot be represented—for example, if the inputstring is array of letters then the function atoi is undefined.

Example:

   

#include <stdio.h>
#include <stdlib.h>//use this header for function atoi
//this program prompt user to input string then convert this string to int
//main function
int main(int argc, char *argv[])
{
   char* inputString;//input string
   printf("globalproganswer.com\n");
   printf("Enter string (example 2555): ");//promt the user to input string
   scanf("%s",&inputString);//read string from keyboard
   int inputValue=atoi(&inputString);//convert inputString to int using function atoi
   inputValue-=100;//decrement inputValue
   printf("The converted value after decrementing by 100 is %d.\n\n",inputValue);//show result
   system("PAUSE");  //delay
   return 0;//retun 0 exit program
}

Output

atoi - convert string to integer in C

Download this example

0 Comments...

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe