C program To show examples of the strtol function.

C program To show examples of the strtol function.
strtol() function converts the string to the long integer, and strtol() can accept the number in various bases, and converts it into the decimal number or base. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdlib.h>

main()
{
char num[10];

/* Test a valid number */
strcpy(num,"13");

printf("%s(Oct) is %i(Dec)n", num, strtol(num, NULL, 8));
printf("%s(Dec) is %i(Dec)n", num, strtol(num, NULL, 10));
printf("%s(hex) is %i(Dec)n", num, strtol(num, NULL, 16));

puts("----------------------------------");

/* Test a slightly valid number
* Returns the same results as
* above. */
strcpy(num, "13hzcd");

printf("%s(Oct) is %i(Dec)n", num, strtol(num, NULL, 8));
printf("%s(Dec) is %i(Dec)n", num, strtol(num, NULL, 10));
printf("%s(hex) is %i(Dec)n", num, strtol(num, NULL, 16));

puts("----------------------------------");

/* Test an invalid number
* Returns ZERO */
strcpy(num, "hzcd");

printf("%s(Oct) is %i(Dec)n", num, strtol(num, NULL, 8));
printf("%s(Dec) is %i(Dec)n", num, strtol(num, NULL, 10));
printf("%s(hex) is %i(Dec)n", num, strtol(num, NULL, 16));


puts("----------------------------------");

/* Test 0 base.
* This will look at the number
* and decide the base for its self!
*/
strcpy(num, "13");
printf("%s is %i(Dec)n", num, strtol(num, NULL, 0));

strcpy(num, "013");
printf("%s is %i(Dec)n", num, strtol(num, NULL, 0));

strcpy(num, "0x13");
printf("%s is %i(Dec)n", num, strtol(num, NULL, 0));

}
Read more c programs
C Basic
C Strings

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

Leave a Reply