Embedded Systems

Print a long int in C using putchar() only

Write a C function print(n) that takes a long int number n as argument, and prints it on console. The only allowed library function is putchar( ), no other function like itoa( ) or printf( ) is allowed. Use of loops is also not allowed.

                         Since putchar( ) prints a character, we need to call putchar( ) for all digits. Recursion can always be used to replace iteration, so using recursion we can print all digits one by one. Now the question is putchar( ) prints a character, how to print digits using putchar( ). We need to convert every digit to its corresponding ASCII value, this can be done by using ASCII value of ‘0’. Following is complete C program.



If you like this Article, then don’t forget to Click on Social likes buttons.