String Conversion Functions in C

Question: What are Different String Conversion Functions in Standard C Library?

Answer: We have following functions which take character string with valid integers as first argument and return integer and long integer respectively as function value. These perform base 10 conversions.

    int atoi(char const *string);
    long int atol(char const *string);

Function given below

    long int strtol(char const *string, char **p2unused, int base);

converts character string to long integer for a given base value, while following function

    unsigned long int strtoul(char const *string, char **p2unused,
                                int base);

converts character string to unsigned long integer for the given base. Note here that if the base value is given as ZERO, both functions accept any form of writing ‘integer literals’ including the forms that specify base of numbers. For ex.

advertisement
advertisement
    Ox34ffd    /* specify hexadecimal form */
    O3457      /* specify octal form */

Remember that base value may be in range 2 through 36, conversion is then performed with the given base. For bases 11 through 36, ‘upper case’ characters (A through Z) as well as ‘lower case’ characters (a through z) are interpreted as digits 10 through 35 respectively. Note, also, here that second argument, if not NULL, points to location where pointer to first invalid character in the string after conversion is stored. Therefore, this pointer can be used to access remainder of string without having to guess where conversion stopped. For ex.

/* strtoul.c -- converts string to unsigned long integer value */
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
 
int main(void)
{
    char **p2unused;
    char input[SIZE];
    unsigned long ulong_val;
 
    printf("User,type in character string with valid integer value...\n");
    gets(input);
 
    ulong_val = strtoul(input, p2unused, 12);
    printf("String \"%s\" converted to value: %ld\n", input, ulong_val);
 
    return 0;
}

Notice the output below

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
User, type in character string with valid integer value...
    232fg
String "    232fg" is converted to value:326
 
User, type in character string with valid integer value...
dfldkfkd   1232   fdfd
String "dfldkfkd   1232   fdfd" is converted to value:0
 
User, type in character string with valid integer value...
         123456 hello
String "         123456 hello" is converted to value:296130

Observe that any arbitrary amount of preceding ‘white space’ characters is skipped and illegal characters after the valid integer digits are ignored. Further, conversion stops to the first invalid character for a given base value. For example,

    strtoul("    123abcde", **p2unused, 13);

Notice that conversion stops with character ‘e’ as this isn’t valid for base ’13’. Also, notice that if the string argument to any of these functions doesn’t contain a legal numeric value, then 0 is returned. For example,

advertisement
    atoi("   hello");
    atol("hello123");
    strtol("hello mello", **p2unused, 0);
    strtoul("   ", **p2unused, 0);

If the converted value can’t be represented, the value ERANGE is stored in errno and returns LONG_MIN if the value is too large and negative or LONG_MAX if value is too large and positive for ‘strtol()’ and returns ULONG_MAX if the value is too large for ‘strtoul()’ function.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

advertisement
If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.