-5

How can I get an output that repeats itself only using a printf statement. No loops involved (for, while etc) In C?

The output could look something like this:

  1. stackoverflow
  2. stackoverflow
  3. stackoverflow
  4. stackoverflow
  5. stackoverflow.
  6. etc
tshepang
  • 12,111
  • 21
  • 91
  • 136
  • You could write a recursive function. However, what have you tried so far? Or take a look [here](http://stackoverflow.com/questions/5004248/print-without-loops). – pzaenger Jan 12 '14 at 20:12

3 Answers3

1

Try this if you are afraid of recursion( very easy :) ):

#include <stdio.h>

void print(void)
{
    printf("Stackoverflow");
}

int main(void)
{
    print(); 
    print(); 
    print();
    ...
    ...
}  

If you like recursion then try this:

#include <stdio.h>

void print(int n)
{

    printf("Stackoverflow\n");
    n--;
    if (n > 0)
        print(n);
}

int main(void)
{
    int n;
    Printf)"How many times do you want to print: ");
    scanf ("%d", &n);
    print(n);  
}
haccks
  • 104,019
  • 25
  • 176
  • 264
1

Depending on what you regard as a loop, you can use goto.

#include <stdio.h>
int main(void)
{
infinite_loop:
    printf("stackoverflow\n");
    goto infinite_loop;
}
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1
#include <stdio.h>
#include <boost/preprocessor/repetition/repeat.hpp>

#define PROC(z, n, text)  text

#define REP(str, n) BOOST_PP_REPEAT(n, PROC, str)

int main(){
    REP( printf("Stackoverflow\n"); , 6);
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70