0

There is a possibility that this may be a duplicate. However, I am just starting out with C and googling the issue only leads to questions which deal with more complicated situations.

Our class has been given some example code on printing variables:

#include <stdio.h>
int main() {
   int i=10;
   printf("i = %d\n", i);
   return 0;
}

The lecture is over, and now it is time for the students to write code. The 1st exercise involves drawing shapes, so I have some code to draw a shape given a character and dimensions. It is not complete, because going further without pruning all of the errors I already have would be foolish.

#include <stdio.h>
void draw_rectangle(char c, int width, int length){
  int i,j;
  for(i=0; i<width; i++){
    for(j=0; j<length; j++){
      printf("%c\n",c);
    }
    printf("\n");
  }
}
int main() {
  draw_rectangle('*',4,4);
  return 0;
}

Nevermind the fact that it seems like you can't declare and initialize variables in for loops. We have bigger fish to fry. I used printf to print a character much like printf was used in the example to print an integer. When I compile and run the code, nothing happens. This is an improvement over the program just crashing, but only slightly. What is the issue? I expected the following output:

****
****
****
****

Edit: Figured out the issue. I forgot to type a.out to check the output.

Display name
  • 257
  • 2
  • 8
  • 3
    If you take out the `\n` in the first printf, then the output seems to be what you are looking for. – Emmanuel Mathi-Amorim Jan 30 '20 at 18:08
  • The output you should get here is 16 lines with one star, because you have '\n' after each one. What output are you getting? Are you sure you're actually running the program? Have you compiled and run any program that prints anything? Adn you *can* declare variables in for loops: the syntax is `for (int i = 0; ...)`. – Lee Daniel Crocker Jan 30 '20 at 18:09
  • 2
    Cannot reproduce. It does print multiple stars - one star on each line (you don't need the newline in `printf("%c\n",c);`) – ForceBru Jan 30 '20 at 18:10
  • This may help answering your other question as to why you cannot declare variables in the for loop line. https://stackoverflow.com/questions/288441/variable-declaration-placement-in-c – Emmanuel Mathi-Amorim Jan 30 '20 at 18:12
  • Learn to use [fflush(3)](http://man7.org/linux/man-pages/man3/fflush.3.html) – Basile Starynkevitch Jan 30 '20 at 18:22

1 Answers1

2

For me the program is compiling and running great. But you have an error in your print:

void draw_rectangle(char c, int width, int length){
  int i,j;
  for(i=0; i<width; i++){
    for(j=0; j<length; j++){
      printf("%c\n",c); // The error is here must be printf("%c", c);
    }
    printf("\n");
  }
}

Your print("%c\n", c); must be only "%c" because you don't want to print carriage return at each symbols, only at the end of lines.

How are you testing it ? Maybe you are not compiling or running it correctly. For instance, if your code is in a file called program.c and you are compiling it on linux:

gcc program.c 
./a.out
Lucas Gras
  • 961
  • 1
  • 7
  • 22