This blog is under construction

Monday 30 April 2012

atoi example in C

Header file:
    stdlib.h

Synopsis:
     int atoi(const char *str);

Description:
     It converts string str to an integer value.


atoi function C example:


  #include<stdio.h>
  #include<stdlib.h>
  #include<string.h>
  int main() {
        char str[100];
        int val;
        printf("Enter your input:");
        fgets(str, 90, stdin);
        str[strlen(str) - 1] = '\0';
        val = atoi(str);
        printf("Converted int value: %d\n", val);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input:12345
  Converted int value: 12345




No comments:

Post a Comment