-9

Having a variable

h = get_int()

how do I print (using printf) this variable

e = " "

h times? I know it may sound stupid, but I´m a rooky.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Alex
  • 9
  • 1
  • 1
  • 3
  • 3
    do you know what a for loop is? Do you know how to print it once? What did you try and what errors do you get? – 463035818_is_not_an_ai Jun 09 '17 at 14:36
  • 5
    [Find a good beginners book or tutorial](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and read about *loops*. – Some programmer dude Jun 09 '17 at 14:36
  • Take a look at a [textbook](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – Passer By Jun 09 '17 at 14:36
  • 3
    Check out SO doc on loops here: https://stackoverflow.com/documentation/c/5151/iteration-statements-loops-for-while-do-while#t=201706091436542342575 – AntonH Jun 09 '17 at 14:37
  • The [`printf()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/printf.html) function can do all sorts of formatting, but it cannot repeatedly format a single item a (variable) number of times. There's a stunt you could pull with POSIX `printf()` for a fixed number of times, but not for a variable number of times, and it would be abuse of the function to use the stunt (which is `printf("%1$s%1$s%1$s", e)` to print it three times). – Jonathan Leffler Jun 09 '17 at 15:16
  • Welcome to StackOverflow. Please take the [tour], learn asking good questions stackoverflow.com/help/how-to-ask, make a [mcve]. For you, making a MCVE probably starts with finding a "HelloWorld" in C and playing with it. – Yunnosch Jun 09 '17 at 15:23

5 Answers5

1

Read up on C loops here

for(int i = 0; i < h; i = i + 1 ) {
      printf("%s", e);
}
Easton Bornemeier
  • 1,918
  • 8
  • 22
0

you probably mean printf...

int i;
for(i = 0; i < h; i++)
  printf("%s ",e);
Gabri T
  • 454
  • 2
  • 13
  • `%c` is for character while you OP wants to print a string. You need `%s`. Also, you are just using `i` to iterate, you can declare it within the `for` like that : `for(int i = 0; i < h; i++)` so it will be destroyed after leaving the loop. – Badda Jun 09 '17 at 14:39
  • @Badda you cannot insert int inside the loop, only when you compile with C99, atleast i always had this problem in C, yea should be %s instead of %c – Gabri T Jun 09 '17 at 14:45
  • What are you doing not using C11? C90 is a quarter century old and 17+ years out of date (it was superseded by C99)? You should be compiling using C11 by default. – Jonathan Leffler Jun 09 '17 at 15:10
0

A while-loop would also be applicable. You can use the variable h as a counter. It a while loop you would first print the number and then count the variable h in a count-down manner until it is 0. This way you will have printed it exactly h-times

while(h>0){
    printf("%s", e);
    h--;
}

Disclaimer: this will of course change the value of h! If you depend on it to use it unchanged somewhere else then you should go for the for-loop solution

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
0

I guess this is what you asks.

#include <stdio.h>
#include <stdlib.h>

char *inputString(size_t size){

    char *str;
    int ch;
    size_t len = 0;
    str = realloc(NULL, sizeof(char)*size);
    if(!str)return str;
    while(EOF!=(ch=fgetc(stdin)) && ch != '\n'){
        str[len++]=ch;
        if(len==size){
            str = realloc(str, sizeof(char)*(size+=16));
            if(!str)return str;
        }
    }
    str[len++]='\0';

    return realloc(str, sizeof(char)*len);
}

int main(void){
    char *e;
    int times,i=0;

    printf("input string : ");
    e = inputString( 10);
    printf("Number: ");
    scanf("%d",&times);
    for(i=0;i<times;i++)    
        printf("%s ", e);   

    free(e);
    return 0;
}
MIRMIX
  • 1,052
  • 2
  • 14
  • 40
  • 1
    The character-reading loop seems to insist on getting a blank or newline before it exits. That's weird. Normally, you want a 'graphic' character (something that isn't white space). You could do that with `" %c"` as the format and no loop. Also, on the first iteration, `element` is uninitialized. Don't test uninitialized variables; you don't know what's going to happen. It's also a good idea to be consistent in layout. You had braces around the `scanf()`, but it was on the same line as the `while` — that's pretty nasty formatting. The `for` didn't have braces. I made them consistent. – Jonathan Leffler Jun 09 '17 at 15:07
  • 1
    I grant you the question asks about `e = " "` which could be a partial justification for looking for a blank or newline, but then you could simply have worked with `char *e = " ";` and printed that. – Jonathan Leffler Jun 09 '17 at 15:09
  • 2
    OP explicitly asks how to print a variable which can be written/initialised as `e= " "`. It seems that goal is not covered by your answer. The implicit question "How can I learn the very basic concepts of C?" is not answered by you either. – Yunnosch Jun 09 '17 at 15:20
  • Thanks @JonathanLeffler for information . I appreciate it. – MIRMIX Jun 09 '17 at 15:28
0

A for loop is the typical way to count iterations, but the syntax of a while loop may be more clear for a beginner:

int count = 0;
while (count < h) {
    printf("%s", e);
    count = count + 1;
}
Rakurai
  • 974
  • 7
  • 15