0% found this document useful (0 votes)
135 views

Converting String To Long in C - Geeksforgeeks

This document provides information on converting strings to long integers in C/C++. It discusses using the strtol() function to convert string representations of numbers to long integers. It also discusses other methods like atol(), ltoa(), and performing the conversion without built-in functions by iterating through the string and building the integer value digit-by-digit. Code examples are provided for each method.

Uploaded by

Mike Thomson
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
135 views

Converting String To Long in C - Geeksforgeeks

This document provides information on converting strings to long integers in C/C++. It discusses using the strtol() function to convert string representations of numbers to long integers. It also discusses other methods like atol(), ltoa(), and performing the conversion without built-in functions by iterating through the string and building the integer value digit-by-digit. Code examples are provided for each method.

Uploaded by

Mike Thomson
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Courses Tutorials Jobs Practice Contests

DSA Data Structures


Algorithms
Interview Preparation
Topic-wise Practice
C++ Java PythonCompetitive Programming
JavaScript
Machine Learning
Write & Earn
SDE Sheet
PuzzlesGFG School
Projects

Arrays in C/C++

Complete Interview
std::sort() in C++ STL
Preparation- Self

Dynamic Memory Allocation


Converting String to Long in C Paced Course

in C using malloc(), calloc(), EXTRA 20% OFF


free() and realloc() Last Updated : 14 Sep, 2022

Bitwise Operators in C/C++ Read Discuss

Core Dump (Segmentation


fault) in C/C++
Here, we will see how to build a C Program For String to Long
Multidimensional Arrays in C Conversion using strtol() function.
/ C++
Syntax:

Converting Strings to
Numbers in C/C++ long int strtol(char *string, char **ptr, int base)

Left Shift and Right Shift


1. The first argument is given as a string
Operators in C/C++
2. The second argument is a reference to an object of type char*
What is Memory Leak? How 3. The third argument denotes the base in which the number is
can we avoid? represented. To know more about visit strtol() function.

Substring in C++

Note: We don’t have to use long int in the case of strtoul()


rand() and srand() in C++
because the range of unsigned long is greater than long on
Different Methods to the positive front.
Reverse a String in C++ [long : -2147483648 to 2147483647 and unsigned long : 0
to 4294967295]
Command line arguments in
C/C++ Syntax:
strtoul(char *string, char **ptr, int base) // no long int need
Enumeration (or enum) in C
in strtoul()
C Language Introduction

std::string class in C++

Function Pointer in C

fork() in C

Strings in C

Data Types in C

Structures in C

Power Function in C/C++

INT_MAX and INT_MIN in


C/C++ and Applications C

Taking String input with


// C program to demonstrate working of strol()
space in C (4 Different
#include <stdio.h>
Methods) #include <stdlib.h>

Pointer to an Array | Array int main()


Pointer {
// To store Number in String form.
char string[10] = "1234567890";
Memory Layout of C long integer;
Programs
// Base is 10 because we are converting to integer.
Modulo Operator (%) in integer = strtol(string, NULL, 10);
C/C++ with Examples printf("Number is %lu", integer);
}

Static Variables in C

Functions in C++
Output
TCP Server-Client
implementation in C
Number is 1234567890

// C program to demonstrate working of strol()


#include <stdio.h>
#include <stdlib.h>

int main()
{
char string[40] = "100 GeeksforGeeks";
long integer;
char* ptr;

// strtol function to convert number in string form to


// long integer with base 10
integer = strtol(string, &ptr, 10);
printf("Integer part is %lu\n", integer);
printf("String part is %s\n", ptr);

return 0;
}

Output

Integer part is 100


String part is GeeksforGeeks

Method: Using atol() function

#include <stdlib.h>
#include <stdio.h>
int main()
{
long l;
char *str;
str = "349639 geeksforgeeks";
l = atol(str);
printf("l = %.ld\n",l);
}

Output

l = 349639

Method: Using ltoa()


C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
long i = 1234;
char buffer[100];

ltoa(i, buffer, 10);


printf("string is %s\n", buffer);
return 0;
}

Output

string is 1234

Method: Without Inbuilt Function

// C program to demonstrate working of strol()


#include <stdio.h>
#include <stdlib.h>

// Driver Code
int main()
{
// To store Number in String form.
char string[10] = "123456789";
long integer = 0;

int i = 0;

// Until null character is encountered


while (string[i] != '\0') {
integer = (integer * 10) + (string[i] ‐ '0');
i++;
}

// Printing the number


printf("Number is %lu", integer);
}

Output

Number is 123456789
Like 1

Previous Next

C Program to Print C Program to Find


Armstrong Numbers Common Array
Between 1 to 1000 Elements Between
Two Arrays

RECOMMENDED ARTICLES Page : 1 2 3

01 05
Converting Number to String in C++ How to write long strings in Multi-
09, Jul 16 lines C/C++?
09, May 15

02
Write your own strlen() for a long 06
string padded with '\0's Is there any need of "long" data
24, May 15 type in C and C++?
21, Sep 17

03
C Program For Long to String 07
Conversion Working of Keyword long in C
25, Jul 22 programming
13, Oct 18

04
Converting Strings to Numbers in 08
C/C++ Print a long int in C using putchar()
02, May 16 only
24, Feb 14

Article Contributed By :

pushpamkjha14
@pushpamkjha14

Vote for difficulty

Easy Normal Medium Hard Expert

Improved By : uomkar369, ksrikanth0498, aadityapburujwale

Article Tags : C Conversion Programs, Picked, C Language, C Programs

Improve Article Report Issue


We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and
Got It !
understood our Cookie Policy & Privacy Policy

You might also like