Print a long int in C using putchar() only


Here we will see how to print long int value using the putchar() function in C. We can easily print the value of some variables using printf() in C, but here the restriction is, we cannot use any other function except putchar().

As we know that the putchar() is used to print only characters. We can use this function to print each digit of the number. When one numeric value is passed, we have to add character ‘0’ with it to get the ASCII form. Let us see the code to get the better idea.

Example

#include <stdio.h>
void print_long(long value) {
   if(value != 0) {
      print_long(value/10);
      putchar((value%10) + '0');
   }
}
main(void) {
   long a = 84571;
   print_long(a);
}

Output

84571

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements