Complex recursive function 007

Elo, I got this code snippet from an old exam. It's complicated, and I need help figuring out how it prints “007”.

#include <stdio.h> /* Desmond Llewelyns */ int M(int Q); int main(void) { M(9); return 0; } int M(int Q) { if(Q>1) if(M(Q-1)==0) printf("%03d\n", Q); return Q-6; } 
+4
source share
2 answers

It is pretty simple.

The number will be printed only if M(Q-1) returns 0 , and this happens when the value of Q is 7 .

And about zeros, this is because you are asking printf to print the number at the three positions of the print zeros at the beginning. printf("%03d\n", Q); More details here .

+8
source

M (Q-1) returns 0 when Q is 7

printf ("% 03d \ n", Q) will display 7 - 3 positions, resulting in 007

+3
source

Source: https://habr.com/ru/post/1443864/


All Articles