C Programming

strtol and strtoul Functions in C

In C language, a standard header file is presented as a “string”, named string.h header file. This header file operates different types of strings. There are different standard predefined string functions available in this header file to manipulate those strings.

There is another standard header file available in C language called stdlib.h header file. There are two standard predefined function available: strtol() and strtoul() functions. Through these functions, we convert the different types of string to the long integer number and unsigned long integer number, respectively.

First, we will see the characteristics and implementation of the strtol() function.

Characteristics of strtol() Function

Header file: The definition of the strtol() function exists in a standard header file in the C library named in stdlib.h header file. It is a standard predefined function.

Syntax:

long strtol(const char * size_str, char ** ptr_str, int bse_n) ;

Description: The strtol() function turns a string into a long integer value. This function does not execute when the function has not accessed a valid input. This function determines the termination point at which a value is assigned to a variable. The ptr_str refers to this variable if the value of ptr_str is not NULL. The parameter bse_n reflects the base value. The base value is used as either 0 or 2 through 36. If the number starts with OX, then it’s base is 16. If it starts with a zero, then its base is 8; others are decimals in base.

Parameters:

size_str: It is a pointer to a string that converts the string.

Ptr_str: It refers to a pointer. This pointer keeps the address of a string pointer.

bse_n: Number conversion depends on the base of a number. The bse_n stores this value.

Returns: strtol() function gives an output as a value, which is a long integer type.

Programming Example 1

Here, we will see how the strtol() function converts a string to a long integer value and the implementation of the strtol() function:

#include<stdio.h>

#include<stdlib.h>

int main()
{
    char strr[60] ;  // declaring a string
    char *s ;
    long v ;// hold the converting long integer value
printf( " please enter a long value: " ) ;
    gets(strr) ;
    v = strtol(strr,&s,0) ;// calling strtol() function for conversion
printf( " The resulting value is: %ld,  doubling the value: %ld \n " , v , v*2 ) ;
    return 0 ;
}

Output

Explanation

Here, we declare a string named strr and assign some values to the user with the help of gets() function. We declare a pointer string named *s to point the string and a long integer value.

We call the strtol() function and pass the string, through the pointer inside the parenthesis of strtol() function for converting the string to long integer value. Now, we print the value of v to print the desired output.

Programming Example 2

Here, we will see another example of the strtol() function.

#include<stdio.h>

#include<stdlib.h>

int main()
{
    char strr[50] = " 430 I am here " ;// declaring as well as initializing a string
    char *s ;
    long v ;// hold the converting long integer value
    v = strtol(strr,&s,10) ;// calling strtol() function for conversion
printf( " The probable value is %ld \n " , v ) ;
    return 0 ;
}

Output

Explanation

In this programming example, we declare a string named strr[] and assign some value. Then we call the strtol() function and, inside its parenthesis, pass the string and string to the pointer as arguments. Long int value is the return from this function as output.

strtoul() Function

Now, we will discuss the characteristics and application of the strtoul() function.

Characteristics of strtoul() Function

Header file: strtoul() function presents in the stdlib.h header file.

Syntax: unsigned long int strtoul(const char * size_str, char ** ptr_str, int bse_n)

Description: This function helps to give the output as a calculation of converting any string to a value that is an unsigned long integer type.

parameters:

size_str: It is a pointer to the string.

ptr_str: It refers to a pointer. This pointer keeps the address of a string pointer.

bse_n: base of the number to be converted.

Returns: it returns the long integer value.

Programming Example 3

Here, we will see an example of the stroul() function.

#include<stdio.h>

#include<stdlib.h>

int main()
{
    unsigned long lv ;  // declaring a long data type variable.
    char szstring[50] = { " 430 I am here " } ;// declaring as well as initializing a string
    char *pEnd ;
    long v ;   // hold the converting long integer value
    lv = strtoul(szstring,&pEnd,0) ;// calling strtoul() function for conversion
printf( " The probable value is %ld \n " , lv ) ;
    return 0 ;
}

Output

Explanation

Here, we declare a string named szstring[] and assign some values inside the string. Now, we call the strtoul() function and pass some arguments like the string, the character pointer inside its parenthesis for conversion. This function returns the long integer value as its output and showing in the monitor

Conclusion

Here, we discussed the characteristics of strtol() and strtoul() functions. These two functions help us convert the string to long integer value, respectively. These functions are extremely helpful for converting the operation of strings in C language. These functions provide different options to the C language in numerical operations.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com