2

I learned that numbers can be converted into strings with sprintf:

int main()
{
    int n; 
    char s[32];

    printf("n=");
    scanf("%d",&n);

    sprintf(s,"%d",n); //HERE
    puts(s);

    return 0;
}

Is it possible to convert a string into a number with a similar command, without checking if each character is a number?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
webpersistence
  • 894
  • 3
  • 12
  • 29

4 Answers4

3

Yes. You can use strtol function.

long int strtol(const char * restrict nptr, char ** restrict endptr, int base);  

The strtol convert the initial portion of the string pointed to by nptr to long int representation.

Better to not use atoi. It tells noting when unable to convert a string to integer unlike strtol which specify by using endptr that whether the conversion is successful or not. If no conversion could be performed, zero is returned.

Suggested reading: correct usage of strtol

Example:

char *end;
char *str = "test";
long int result = strtol(str, &end, 10);  

if (end == str || *temp != '\0')
    printf("Could not convert '%s' to long and leftover string is: '%s'\n", str, end);  
else 
    printf("Converted string is: %ld\n", result);
Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
3

The strtol family of functions provide this capability. They allow you to convert a string to a numeric type (depending on which member of the family you choose) and, unlike the atoi family, also allow you to detect if scanning failed before reaching the end.

It does this by populating the pointer you pass with the address of the first character not included in the conversion. If this is anything other than the string terminator, it had to stop early.

There's also the special case where it may point at a terminator even when the string is invalid (specifically, the case of an empty string "").

As an example, the following program shows one way to use it:

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

int main (int argc, char *argv[]) {
    int i;
    long val;
    char *next;

    // Process each argument.

    for (i = 1; i < argc; i++) {
        // Get value with failure detection.

        errno = 0;
        val = strtol (argv[i], &next, 10);

        // Check for empty string and characters left after conversion.

        if (errno == EINVAL) {
            printf ("'%s' invalid\n", argv[i]);
        } else if (errno == ERANGE) {
            printf ("'%s' out of range\n", argv[i]);
        } else if (next == argv[i]) {
            printf ("'%s' is not valid at first character\n", argv[i]);
        } else if (*next != '\0') {
            printf ("'%s' is not valid at subsequent character\n", argv[i]);
        } else {
            printf ("'%s' gives %ld\n", argv[i], val);
        }
    }

    return 0;
}

Running that code with the arguments hi "" 42 3.14159 9999999999999999999999999 7 9q gives:

'hi' is not valid at first character
'' is not valid at first character
'42' gives 42
'3.14159' is not valid at subsequent character
'9999999999999999999999999' out of range
'7' gives 7
'9q' is not valid at subsequent character
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2

"Is it possible to convert a string into a number with a similar command, without checking if each character is a number?"

Yes, that's possible. atoi() was made for this purpose (see also the strtol function family in that reference).

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
2

You can use the atoi function.

 int atoi(const char *nptr);
Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31