18

Recurrent function

  1. #include <stdio.h>
  2.  
  3. void printd( int );
  4.  
  5. void printi( int i )
  6. {
  7.     if ( i < 0 )
  8.     {
  9.         putchar( '-' );
  10.         i = -i;
  11.     }
  12.     printd( i );
  13. }
  14.  
  15. void printd( int i )
  16. {
  17.     int d = i/10;
  18.     if ( d )
  19.         printd( d );
  20.     putchar( (char)(i%10 + '0') );
  21. }
  22.  
  23. main() {
  24.     int x;
  25.  
  26.     x = 12345;
  27.     putchar('x');
  28.     putchar('=');
  29.     printi( x );
  30.     putchar('\n');
  31.  
  32.     x = -x;
  33.     putchar('x');
  34.     putchar('=');
  35.     printi( x );
  36.     putchar('\n');
  37.  
  38.     x = 0;
  39.     putchar('x');
  40.     putchar('=');
  41.     printi( x );
  42.     putchar('\n');
  43. }
$ gcc -o recur recur.c
$ ./recur
x=12345
x=-12345
x=0

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].