![]() |
VOOZH | about |
Here, we will see how to build a C Program For String to Long Conversion using strtol() function.
Syntax:
long int strtol(char *string, char **ptr, int base)
Note: We don't have to use long int in the case of strtoul() because the range of unsigned long is greater than long on the positive front.
[long : -2147483648 to 2147483647 and unsigned long : 0 to 4294967295]Syntax:
strtoul(char *string, char **ptr, int base) // no long int need in strtoul()
Number is 1234567890
Integer part is 100 String part is GeeksforGeeks
l = 349639
Output
string is 1234
Number is 123456789
Steps:
123456789
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(1)
Here's another approach to convert a string to a long integer in C:
123456789
Time complexity: The time complexity of the "sscanf()" function is O(n), where n is the length of the input string.
Auxiliary space: The space complexity of the program is O(1), as we are not using any additional data structures to perform the conversion.