C Programming

Convert String to Long in C

A string is one of the data types used for datasets in programming languages. It is a collection of characters that ends using the null character in the C language. A string may include some observable or unidentified series of characters, and sometimes elements can be duplicated.

By default, the compiler inserts a null character to the completion of a series of elements surrounded by inverted commas. The long data type retains integers the same way int does, but it has a larger range of possibilities when using additional memory. The strtol() method has been used to modify an integer’s string illustration to a long type. It is declared in the header file <stdlib.h>. It’s much more versatile and efficient than the atol() method.

The strtol() method allows the user to set the value of the string’s base. This method would be used to translate any string to a long type. We may immediately differentiate between effective and ineffective transformations by the strtol() method. In this article, we’ll explore how to transform a string to a long, while utilizing multiple C functions.

Use the strtol() Function to Change the String to Long

The strtol() is a C library method of transforming the first portion of a string to a long data type value based on the specified base, which needs to be between 2 and 36 encompassing or the exceptional value 0.

The strtol() method requires three arguments: a defined string, a pointer, and a numeric value base. It translates the string’s data as an integrated part of the provided base and produces a long int value.

#include <stdio.h>

#include <stdlib.h>

int main () {
   char s[40] = "304852 Information Technology";
   char *p;
   long res;
   res = strtol(s, &p, 10);
printf("The number is %ld\n", res);
printf("String portion is |%s|", p);

We are going to start the program by integrating two libraries: <stdio.h> and <stdlib.h>. In the next step, we utilize the main() function. Within the main() function, a string having a character data type is defined. Here, we set the size of the defined string. This string contains both numeric and alphabetic portions.

Further, we declare a pointer and a variable “long res” for storing the result. Now, we apply the strtol() function. This method contains three arguments. The first parameter shows the integral portion of the defined string. The second argument is the pointer to a character object that has already been created.

The function specifies the valuation of the end toward the next valid character in the string and followed by the previous suitable character. And the last parameter represents the base of the numeric part. The range of acceptable base values is 0, 2, 3… 35, 36. Hence, the printf() method first prints the numeric portion and then prints the string portion.

Use the strtol() Function for Preceding Spacing With Different Bases

Until the initial non-whitespace element is detected, the strtol() method avoids any preceding whitespace elements. This function transforms the string to a long int value and takes many of the elements as necessary that create a suitable integer illustration. Since the last definitive character, anything left on the line is eliminated and has no impact on the outcome.

#include <stdlib.h>

#include <cstdlib>

using namespace std;
int main()
    char *e;
printf("40lmno to Long Integer having base-13 = %d\n");
    strtol("40lmno", &e, 13);
printf("String = %s", e );
printf("13674di to Long Integer having base-6 = %d\n");
    strtol("13674di", &e, 6);
printf("String = %s", e );
printf("pqt589.2 to Long Integer having base-20 = %d\n");
    strtol("pqt589.2", &e, 20);
printf("String = %s", e );
    return 0;

Here, we introduce two header files <stdlib.h> and <cstdlib>. After this, we utilize the standard namespace function. Further, the body of the main() function starts. Meanwhile, we construct the pointer of the character data type. Now, we apply the printf() function for displaying the defined line.

In addition, the strtol() function is also applied. This function contains the required string, pointer, and the base to which the string has to be converted as parameters. In the next step, the printf() function is again used to display the converted string. Similarly, we employ the strtol() function and set the base to 6 and 20. Printf() method is also used to get the results of these conversions. To terminate the program, we have to include the following “return 0” statement:

Use the atol() Function to Convert the String to Long

An alphanumeric string is changed to a long value using the atol() method. The corresponding string is a series of bits that could be translated into an integer value of the data type provided. The method refuses to read the entered string once it detects the first character and doesn’t identify it as an integer. This might be the invalid character at the termination of the string.

The atol() method generates a long value by converting the specified characters into integers. If the function is unable to transform the data to a variable of that type, it gives 0L. In the situation of overload, the return type is invalid.

#include <stdlib.h>

#include <stdio.h>

int main(void)
    long l;
    char *str;
    str = "349639 dollars";
    l = atol(str);    
printf("l = %.ld\n",l);

At the start of the code, we have to include the required header files, <stdlib.h> and <stdio.h>. Now, we employ the main() function. In the next step, we declare a variable having a long data type and a pointer for the string which has a character data type. Next, we specify the value of the string in dollars.

In addition to this, the atol() method is called. Here, we passed the required string as a parameter to this function. The atol() function converts the string to long. In the end, we utilize the printf() function to display the result.


In this article, we have observed the methods of converting the string to long in C language. Here, we have gone through the use of the strtol() function and atol() function for this type of conversion. We have also utilized the strtol() method having different bases for converting string to long. We hope you found this article helpful. Check the other Linux Hint articles for more tips and tutorials.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content