Hi!
I have seen a function to do "atoi for converting a string to its numeric equivalent. It copes with optional leading white space and an optional + or - sign. "
The structure of the program reflects the form of the input:

skip white space, if any

get sign, if any

get integer part and convert it

Given function is

#include <ctype.h>

/* atoi: convert s to integer; version 2 */

int atoi(char s[])

{

int i, n, sign;

for (i = 0; isspace(s[i]); i++) /* skip white space */

;

sign = (s[i] == '-') ? -1 : 1;

if (s[i] == '+' || s[i] == '-') /* skip sign */

i++;

for (n = 0; isdigit(s[i]); i++)

n = 10 * n + (s[i] - '0');

return sign * n;

}

I am unable to write a main function that calls it in order to give output.I am not sure where to give input and how to.I simply quite not understood the program.Please help me.
I have tried this..

#include<stdio.h>


main(){

atoi({abcdefghij});
}
#include <ctype.h>

/* atoi: convert s to integer; version 2 */

int atoi(char s[])

{

int i, n, sign;

for (i = 0; isspace(s[i]); i++) /* skip white space */

;

sign = (s[i] == '-') ? -1 : 1;

if (s[i] == '+' || s[i] == '-') /* skip sign */

i++;

for (n = 0; isdigit(s[i]); i++)

n = 10 * n + (s[i] - '0');

return sign * n;

}
main(){
int /* what to declare and input here*/
atoi(/*what to give here*/);
}

Dani AI

Generated

Quick answer for : the atoi function needs a NUL-terminated C string (a char *). You must pass either a string literal or a buffer that you filled (for example with fgets or argv[1]). Do not write {abcdefghij} — string literals must be in double quotes. Either place your conversion function above main or put a prototype for it before main so the compiler knows the signature.

Example — call with a literal:

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

int main(void) {
    const char *s = "  -42";
    int value = atoi(s);    // calls the conversion routine
    printf("%d\n", value);
    return 0;
}

Example — read a line from standard input and convert it:

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

int main(void) {
    char buf[100];
    if (fgets(buf, sizeof buf, stdin) != NULL) {
        int v = atoi(buf);   // or call your own converter here
        printf("%d\n", v);
    }
    return 0;
}

Notes and troubleshooting:

  • As pointed out, the standard atoi is declared in <stdlib.h>. If you write your own, consider renaming it (for example my_atoi) to avoid confusion with the library version.
  • atoi offers no error reporting and can’t detect overflow. Prefer strtol when you need to know whether conversion succeeded or to detect leftover characters. See strtol on cppreference and atoi on cppreference.
  • Compile with warnings enabled (for example gcc -Wall -Wextra) and make main return int so you spot mistakes quickly.

Recommended Answers

All 4 Replies

Are you trying to USE atoi() in your program, or are you trying to make your own atoi() function?

The C atoi() function will need a pointer to the char array, not this:
atoi({abcdefghij});

I have no idea what the abcd... stuff is all about.

Are you trying to USE atoi() in your program, or are you trying to make your own atoi() function?

The C atoi() function will need a pointer to the char array, not this:
atoi({abcdefghij});

I have no idea what the abcd... stuff is all about.

Sorry I have edited the post wrongly actually the program is here..
Now,I have given main function below the atoi function now I don't know how to take input for string and calling such atoi() what to give in () of atoi in main function.

#include<stdio.h>



#include <ctype.h>

/* atoi: convert s to integer; version 2 */

int atoi(char s[])

{

int i, n, sign;

for (i = 0; isspace(s[i]); i++) /* skip white space */

;

sign = (s[i] == '-') ? -1 : 1;

if (s[i] == '+' || s[i] == '-') /* skip sign */

i++;

for (n = 0; isdigit(s[i]); i++)

n = 10 * n + (s[i] - '0');

return sign * n;

}

main(){
int /* what to declare and input here*/
atoi(/*what to give here*/);
}

Sorry I have edited the post wrongly actually the program is here..
Now,I have given main function below the atoi function now I don't know how to take input for string and calling such atoi() what to give in () of atoi in main function.

#include<stdio.h>



#include <ctype.h>

/* atoi: convert s to integer; version 2 */

int atoi(char s[])

{

int i, n, sign;

for (i = 0; isspace(s[i]); i++) /* skip white space */

;

sign = (s[i] == '-') ? -1 : 1;

if (s[i] == '+' || s[i] == '-') /* skip sign */

i++;

for (n = 0; isdigit(s[i]); i++)

n = 10 * n + (s[i] - '0');

return sign * n;

}

main(){
int /* what to declare and input here*/
atoi(/*what to give here*/);
}

Actually,I have taken a screen shot and it is big in my system but is not here.Why I don't know so I am copying the text of lesson.
We have already encountered the while and for loops.


The structure of the program reflects the form of the input:

skip white space, if any

get sign, if any

get integer part and convert it

Each step does its part, and leaves things in a clean state for the next. The whole process terminates on the first character that could not be part of a number.

#include <ctype.h>

/* atoi: convert s to integer; version 2 */

int atoi(char s[])

{

int i, n, sign;

for (i = 0; isspace(s[i]); i++) /* skip white space */

;

sign = (s[i] == '-') ? -1 : 1;

if (s[i] == '+' || s[i] == '-') /* skip sign */

i++;

for (n = 0; isdigit(s[i]); i++)

n = 10 * n + (s[i] - '0');

return sign * n;

}

The standard library provides a more elaborate function strtol for conversion of strings to long integers;

First off ALWAYS put your code, between the code tags! It's VERY hard to study code like that is smashed together as html text.

Second, my atoi() function, is in stdlib.h, not ctype.h. Please recheck that on your compiler.

I can't tell if I'm looking at comma's or semi-colons on your code - post it up in between code tags. It will be ignored pretty much, otherwise.

Don't post about Shell sort and all the rest - I know Shell sort. Post up about your own problems with your own code. And please, limit the pic's - just copy and paste your code between code tags.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.