logo
How to print an number in C using putchar and recursion
Mwaura

Mwaura

Feb 06, 2023

How to print an number in C using putchar and recursion

In this article, we dive deep into the low levels. This tutorial demonstrates how to print an integer in C. Here is the fun part, the printf() function will not be used. I found the approach extremely elegant. (You will too) Lets dive in.

The code

The explanation

I know, this may seem intimidating. Don't worry, we will take it step by step.

We start by importing the standard input/output library.

This allows us access to the putchar function, contained in the library. (I will soon be writing about how putchar works)

Tip: stdio actually means standard input/output

In brief, here is the function prototype of putchar from the linux man pages.

with the description:

putchar takes an integer, and prints the ACII represented by the integer to stdout.

There are two functions in the code, i.e.,

The main function

The main function is the entry point to our program. The text surrounded by /* comment */ are the comments, describing what each function does.

The main function calls the print_number function several times, with a specific number each time. The print_number function then does its magic and prints each number.

putchar is used to print a new line after every call of print number.

The output

Compiling and running our code produces the following output:

We will go through the step compilation later.

The print number function

Here is where all the magic happens. The function takes an integer n.

  • We start by declaring an integer i and giving it the value n. eg, n = -87.
  • We then check if i is negative, and print "-" to the screen. After which we convert i to a positive integer. ie.

In our case, we print "-" .

After the assignment above, i is now equal to 87.

  • We then check
  • If true, we call the function print_number recursively with the value (i / 10)

In our case we call the function print_numbers(87 / 10 = 8)

All the steps above are repeated for n = 8.

  • When i / 10 == 0 is no longer true, we print (i % 10) to the screen.

In this case we print 8 since 8 % 10 = 8.

  • We then go back to where we had stopped, with i = 87.
  • We then print 87 % 10 = 7 to the standard output.
We pass (i % 10 + '0') to putchar because we want to print the digit i / 10, 7 in our case and not the character represented by 7 in ascii. By adding '0', we are printing the 7th character after '0', i.e., 7
  • And thats it, -87 has been printed to the screen.

Compilation

To compile our code, run the command bellow, change it accordingly based on how you have named your files.

gcc print_number.c -o print_number

We are simply telling gcc to compile the file print_number.c and output the result to the file print_number.

Conclusion

Run the program as shown below. (This may be different depending on your developer environment).

./print_number

The output will be as observed earlier.

And that's it.

Feel free to air any concerns on the comment section.

Leave a Reply

Related Posts

Categories