How would you write a C program to print 1 to 100 without loop, recursion and goto?

I came across this Quora question today – “How would you write a C program to print 1 to 100 without loop, recursion and goto?”  As always, there is a plethora of answers, of which I liked the following two.

Jonathan Day goes into how the specification can be misinterpreted in a variety of ways, and how garbage in (specification) means garbage out (implementation).  I like it so much, I’m going to quote the whole thing here, so that I can easily find it in the future:

  printf(“1 to 100\n”);

You actually want the numbers, not just that particular string? That’s the problem with poor specifications. Managers have no consideration for us poor programmers…

No loops of any kind, no recursion of any kind, no goto (or comefrom) of any kind… This is the opposite extreme, taking the question absolutely literally as it is given rather than as it might be intended. This would have to include loops, recursion and gotos in all libraries, external programs and even the kernel itself, if taken to this degree.

Which creates a problem. You have to treat all such things as black boxes. You do not know if the kernel parses system calls by looping through the operands. That would be a loop that any printing function must call. The question says nothing about YOU not using loops, it says no loops.

Again, this is why specifications must be exact in programming. Poor specifications will get you what you asked for, but not what you want, just as that code snippet above did. No, I don’t care what was in someone’s mind, nor does the computer. GIGO is the law to which all in IT must obey whether they like it or not. That is why you MUST say what you mean and mean what you say, in positives and negatives.

It’s also why a lot of highly autistic people are highly successful. Literalism is, very literally, in their genes.

Ok, let’s use a third possible interpretation, that you want the numbers 1 through to 100, without the code using loops, recursion or gotos, but where calls may.

Most of the other answers are very acceptable, although some were implicit loops rather than explicit loops. Again, are implicit loops allowed or not allowed, under the specification? The specification simply doesn’t provide enough information to say.

Nor does the specification say if I can use loops, recursion or gotos in generating the data, versus printing it. I could generate the string very easily, just malloc enough space, convert the number into text, then place the characters into the string, with a space or control-linefeed as separator. The print itself is then a single command. Nothing else is needed.

Nothing says if this is a permissible option or not, you have to interpret. And interpretation is not how you should ever program. GIGO.

By loops, I am interpreting this as meaning any sort of iterative process, regardless of formulation. That is the standard meaning. But it occurs to me that repeat/until (while) and until/repeat (do) loops aren’t always seen as loops as they’re conditional on an event. Well, in C, so is a for loop. In many languages, for is fixed-length and so is regarded as a different sort of beast.

Also, the “goto” should be regarded with suspicion. Some languages have deliberately replaced “goto” with “comefrom” to ensure that the language complies with the requirement of not having gotos. I don’t think many SE’s regard that as anything other than slightly snarky humour, but there is no indication in the question as to whether a comefrom command would be disallowed if someone could code it into a header file. This is why sane programmers would interpret the question as prohibiting any form of condition/jump operation, regardless of the nature of the condition or the jump.

This leaves grammars. If the sequence from 1 to 100 can be specified in a grammar, then an external library performs the loop and you can print the result. There’s still iteration, but it’s both implicit and indirect. (When abstraction and black-box design is used to hide things from a boss, it’s time to ask if the boss is suitable for you. These should only ever be used to clarify.)

Ok, nothing in the question specifies a sequence, only the presence of the numbers. Nor does anything specify that numbers can’t appear more than once. So if I use grammars to generate absolutely every permutation of the digits 0 to 9 and a space, then I am guaranteed every number from 0 to 100. There are other regexp functions that let us remove specific patterns, so if I remove all patterns in which one, two or three 0s have a space either side, I can eliminate 0. This just leaves 1 to 100, albeit with a great many single digit and double digit repeats. Repeats aren’t talked about and we’ve already screwed with the interpretation badly enough to make the question pointless, so we assume they’re allowed.

Questions of this kind are usually asked to analyze the methodology of thinking rather than the conclusion itself. They have to be, because the question means so many possible things that it is meaningless. The result you get is, thus, equally meaningless.

And then, this obvious Vim solution by Gilles Castel:

i#include <stdio.h><CR>int main(){<CR>
printf("%d ",1);<ESC>qq0yyp<C-A>q98@qoreturn 0;

Not only he provided the explanation of what’s going on, but also  an animated GIF of how it looks:


And, yes, I’m not really interested in the actual C solutions to this problem.

The Archive of Interesting Code

The Archive of Interesting Code

The Archive of Interesting Code is an (ambitious) effort on my part to research, intuit, and code up every interesting algorithm and data structure ever invented. In doing so, I hope both to learn the mathematical techniques that power these technologies and to improve my skills as a programmer.

Thou shalt not ignore warnings

Here is a quote from a wise comment in the discussion of the “Linux system programming” book review:

Build your code with -Wall -Werror (or your compiler’s equivalent). Once you clean up all the crud, that pops up, crank it up with -W -Wno-unused-parameter -Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith. Once there — add -Wreturn-type -Wcast-qual -Wswitch -Wshadow -Wcast-align and tighten up by removing the no in -Wno-unused-parameter. The -Wwrite-strings is essential, if you wish your code to be compiled with a C++ compiler some day (hint: the correct type for static strings is ” const char *”).
For truly clean code, add -Wchar-subscripts -Winline -Wnested-externs -Wredundant-decls.
The people, who wrote and maintain the compiler, are, most likely, several levels above you in understanding programming in general and C-programming in particular. Ignoring the advice their code generates is foolish on your part…
As a minimum, solved warnings will make your code more readable by reducing/eliminating the “Why is he doing this?” questions. More often than not, they point out bugs you would otherwise spend hours chasing with a debugger later.
And they make your code more portable. But if you don’t understand, why a warning is generated — ask around. Don’t just “shut it up”. For example, initializing a variable at declaration is usually a no-no. If the compiler thinks, the variable may be used before being initialized, scrutinize your program’s flow. If you can’t figure out, it may some times be better to disable this one warning temporarily with -Wno-uninitialized to move on, instead of shutting it up for ever by a bogus “= 0” or some such…