write a program to convert asci to integers using command line in c- ATOI

13 Dec 2022 Balmiki Mandal 0 C Programming

Using ATOI in C: Convert ASCII to Integers via Command Line

To write a C program to convert ASCII to integers using the command line in C - ATOI, we can use the following steps:

Include the necessary header files:

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

Declare a function to convert ASCII to integers:

C Programming
int atoi(const char *str) {
  int result = 0;
  while (*str != '\0') {
    result = result * 10 + (*str - '0');
    str++;
  }
  return result;
}

Write the main function to accept the ASCII string from the command line and convert it to an integer:

C Programming
int main(int argc, char *argv[]) {
  if (argc != 2) {
    printf("Usage: %s <ASCII string>\n", argv[0]);
    return 1;
  }

  int integer = atoi(argv[1]);
  printf("The integer value of the ASCII string is: %d\n", integer);

  return 0;
}

Compile and run the program:

$ gcc -o atoi atoi.c
$ ./atoi 123
The integer value of the ASCII string is: 123

 

This program can be used to convert any ASCII string that represents a valid integer to an integer value.

Further Reading:


What is command line arguments? What is the use of that?

By default Command line Arguments are treated as ?

command line argument in c- load time input in c

write a program for finding length of given string using command line argument in c

write a program to convert asci to integers using command line in c- ATOI

write a program for basic calculator operation using command line in c

assignment of command line argument in c

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.