Winter Bootcamp in ML and IoT in Jaipur
 Course content (For Bootcamp & Winter Training):- Machine Learning (ML) || Internet of Things (IoT) || Register for winter bootcamp
0 like 0 dislike
in Tutorial & Interview questions by (3.3k points)
Version ≥ C99

1 Answer

0 like 0 dislike
by (3.3k points)
 
Best answer
Since C99 the C library has a set of safe conversion functions that interpret a string as a number. Their names are of the form strtoX, where X is one of l, ul, d, etc to determine the target type of the conversion

double strtod(char const* p, char** endptr);

long double strtold(char const* p, char** endptr);

They provide checking that a conversion had an over- or underflow:

double ret = strtod(argv[1], 0);                    /* attempt conversion */

/* check the conversion result. */

if ((ret == HUGE_VAL || ret == -HUGE_VAL) && errno == ERANGE)    

return;                                   /* numeric overflow in in string */

else if (ret == HUGE_VAL && errno == ERANGE)    

return;                                      /* numeric underflow in in string */

/* At this point we know that everything went fine so ret may be used */

If the string in fact contains no number at all, this usage of strtod returns 0.0.

If this is not satisfactory, the additional parameter endptr can be used. It is a pointer to pointer that will be pointed to the end of the detected number in the string. If it is set to 0, as above, or NULL, it is simply ignored.

This endptr parameter provides indicates if there has been a successful conversion and if so, where the number ended:

char *check = 0; double ret = strtod(argv[1], &check);            /* attempt conversion */

/* check the conversion result. */

if (argv[1] == check)    

return;                 /* No number was detected in string */

else if ((ret == HUGE_VAL || ret == -HUGE_VAL) && errno == ERANGE)    

return;               /* numeric overflow in in string */

else if (ret == HUGE_VAL && errno == ERANGE)    

return;                                  /* numeric underflow in in string */

                /* At this point we know that everything went fine so ret may be used */

There are analogous functions to convert to the wider integer types:

long strtol(char const* p, char** endptr, int nbase);

long long strtoll(char const* p, char** endptr, int nbase);

unsigned long strtoul(char const* p, char** endptr, int nbase);

unsigned long long strtoull(char const* p, char** endptr, int nbase);

These functions have a third parameter nbase that holds the number base in which the number is written.

long a = strtol("101",   0, 2 );            /* a = 5L */

long b = strtol("101",   0, 8 );            /* b = 65L */

long c = strtol("101",   0, 10);            /* c = 101L */

long d = strtol("101",   0, 16);           /* d = 257L */

long e = strtol("101",   0, 0 );            /* e = 101L */

long f = strtol("0101",  0, 0 );            /* f = 65L */

long g = strtol("0x101", 0, 0 );          /* g = 257L */

The special value 0 for nbase means the string is interpreted in the same way as number literals are interpreted in a C program: a prefix of 0x corresponds to a hexadecimal representation, otherwise a leading 0 is octal and all other numbers are seen as decimal.

Thus the most practical way to interpret a command-line argument as a number would be

int main(int argc, char* argv[]

{    

if (argc < 1)        

return EXIT_FAILURE;                  /* No number given. */

    /* use strtoull because size_t may be wide */    

size_t mySize = strtoull(argv[1], 0, 0);

    /* then check conversion results. */

     ...

return EXIT_SUCCESS;

}

This means that the program can be called with a parameter in octal, decimal or hexadecimal.

Winter 10 Days boot-camp classes(7 HRS Daily) will start from 5, 20 & 27 December 2019 in:
1) Internet of things(IoT) Using RASPBERRY-PI
2) Machine Learning (ML)

70% OFF| Fee-INR 3,000/-

Limited seats!! Hurry up!!

[[ CALL - 07976731765 ]]

Some Study Resources are compiled from original Stack Overflow Documentation, the content is developed by the different experts at Stack Overflow. Study resources are released under Creative Commons BY-SA. Images may be copyright of their respective owners. This website is for self-learning and not affiliated with Stack Overflow. All trademarks and registered trademarks are the property of their respective company owners. Please send feedback and corrections to chandwaglobal@gmail.com.

Goeduhub Important Lists Our Youtube Channels (For free E-learning)
About Us List of IITs Goeduhub Technologies
Contact Us List of NITs AI and Big Data-HADOOP E-Learning Series
  List of PSUs Smart Learning PLC-SCADA, IoT and Raspberry-PI
  List of Exams After Graduation  
...