Showing posts with label string to integer. Show all posts
Showing posts with label string to integer. Show all posts

Friday, 20 September 2013

Showing posts with label string to integer. Show all posts
Showing posts with label string to integer. Show all posts

Friday, 20 September 2013

Converting String to Integer in C



C program for converting string to an integer using atoi() function:

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

int main(){
    char str[50];
    int number;
 
    //Accepting a numer in string format  
    printf("Enter any number as a string : ");
    scanf("%s",str);
    
    //Converting string into the number;  
    number = atoi(str);  
         if(number==0 && str[0]!='0')
             printf("\nInvalid number");
         else
             printf("\n Equivalent number is : %d",number);
    return 0;
}

OUTPUT:

Enter any number as a string : 23anil
 Equivalent number is : 23