Write a short C program that prints all the numbers from 1 to 1000 on separate lines without using any explicit if-statements, gotos, loops, or conditional expressions.
I'm sure there are several creative ways to skin a cat.
I sure can't think of one. Maybe recursion but even there you'd have to use an if statement to terminate it, and it would overflow the stack on most platforms.
I hate questions like this. They're more parlor tricks than learning anything deep.
Right.
All I can come up with for this is either copy-pasting, some fancy editor trick, or preprocessor abuse.
or maybe even recursively calling main
Ugh, I hope it's not preprocessor abuse. That would be the opposite of teaching. And yeah, I was thinking about recursively calling main, or a binary tree of calls, but all of those require termination conditions.
who gave you this problem?
editor tricks would violate the "short c program"
#define PRINTONE(x) fprintf(stdout, "%d\n", x++); #define TEN(x) x x x x x x x x x x #define PRINT1000(x) TEN(TEN(TEN(PRINTONE(x))))
wow, yeah, that works but is exactly the opposite what a class should be teaching
right. at least after this question I'm done with the C preprocessor chapter.
yeah, i just ran it through the preprocessor and sure enough gave me 1000 fprintf lines
#include <stdio.h> int i = 0; p() { printf("%d\n", ++i); } a() { p();p();p();p();p(); } b() { a();a();a();a();a(); } c() { b();b();b();b();b(); } main() { c();c();c();c();c();c();c();c(); return 0; } :D
lol
#include <stdio.h> void printNumber( int number ) { number ? printNumber( --number ) : printf( "%d\n", number ); } void main() { printNumber( 1000 ); return 0; } Off the top of my head, not tested at all.
@rsmith but that uses a conditional expression :(
I read the no "if", not the no conditionals. I have GOT to stop skipping!
Join our real-time social learning platform and learn together with your friends!