Print a long int in C using putchar() only

CServer Side ProgrammingProgramming

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
raja
Published on 03-May-2019 07:44:27
Advertisements