Open In App

C Program For Long to String Conversion

Last Updated : 03 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

To convert the long to string in C language we will use the following 2 approaches:

  1. Using Macros with sprintf
  2. Using sprintf

Input: 

long = 1234 

Output: 

string 1234

1. Using Macros and sprintf

C




// C Program for Long to 
// String Conversion
#include <stdio.h>
#include <string.h>
  
#define Max_Digits 10
int main()
{
    long N = 1243;
    char str[Max_Digits + sizeof(char)];
    sprintf(str, "%ld", N);
    printf("string is: %s \n", str);
}


Output

string is: 1243 

2. Using Sprintf 

C




// C Program for Long to String Conversion
#include <stdio.h>
  
int main()
{
  
    long x = 1234;
    char str[256];
    sprintf(str, "%ld", x);
    printf("The string is : %s", str);
    return 0;
}


Output

The string is : 1234

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads