Pages

Sunday, May 7, 2017

Convert given string to integer



C Program to convert given string to integer using atoi function.

Arithmetic operation is not possible on a String. Before performing any arithmetic operation we need to convert given string into integer. This conversion is possible in C using atoi function.

Note: atoi function is defined inside stdlib.h header file. Function atoi converts the string parameter to an integer.

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

int main(){
    char str[20];
    int number;
    printf("Enter your String:");
    gets(str);

    number = atoi(str);
   
    printf("Integer: %d \n\n", number);
    printf("Proof => Arithmetic operation on number:\n");
   
    printf("Adding same number %d + %d = %d",number, number, number + number);
   
    return 0;
}

Output of program

Enter your String:123
Integer: 123

Proof => Arithmetic operation on number:
Adding same number 123 + 123 = 246

No comments:

Post a Comment