0

Here is my code

void display(char ch, int lines, int width);
int main()
{
    char c;
    int rows, cols;
    while (1)
    {
        scanf("%c %d %d", &c, &rows, &cols);
        display(c, rows, cols);
        if (c == '\n')
            break;
    }
}
void display(char ch, int lines, int width) 
{

    for (int i = 0; i < lines; i++)
    {
        for (int j = 0; j < width; j++)
            printf("%c", ch);
        printf('\n');
    }

}

I wana like this. if I input a 2 3 It returns

aaa
aaa

But It didn't work. SO I change like this

void display(char ch, int lines, int width) 
{

    for (int i = 0; i < lines; i++)
    {
        for (int j = 0; j < width; j++)
           putchar(ch);
        putchar('\n');
    }

}

It works well. why that code works well?? what is difference between printf and putchar?

tohardtme
  • 101
  • 4
  • 1
    Does this answer your question? [putchar() vs printf() - Is there a difference?](https://stackoverflow.com/questions/23749797/putchar-vs-printf-is-there-a-difference) – louk1397 Mar 05 '20 at 13:24
  • 3
    @louk1397: It appears you missed the error that the first set of source code passed `'\n'` to `printf` instead of `"\n"`, so the question is not really about what is different between `printf` and `putchar` but what the correct arguments for `printf` are. – Eric Postpischil Mar 05 '20 at 13:32
  • You need to define what "didn't work" means. Did you get an error when you tried to compile the code (you should have based on the `printf('\n')` call)? Did you get an error when you tried to run the program? What did you actually see on your screen? In this case, it's pretty likely that the compiler choked on the `printf('\n')` call because `'\n'` is the wrong type of argument, but it would help everyone a lot if you posted the compiler error or the unexpected output. – John Bode Mar 05 '20 at 15:20
  • `#include ` would help too. – chux - Reinstate Monica Mar 05 '20 at 15:31

2 Answers2

2

printf('\n'); is a mistake, and your compiler should warn you about it. The first argument to printf should be a string, such as "\n", not a character constant, such as '\n'.

The source code "\n" represents a string of two characters, the first being a new-line character and the second being a null character that indicates the end of the string. When used in an expression this way, it is automatically converted to a pointer to its first element, and this pointer is passed to printf.

The source code '\n' represents a character. Its value is the code for that character. When it is passed to printf, that value is passed. That is the wrong thing to pass to printf, which is why your first program did not work.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
-1

When you have %c inside the printf, then it will print the character only. But your point is you gave 2 but it prints a, why? see the ASCII table and find the number to char equivalent value from here ASCII Table. on the other hand putchar is just char printer.

Hemjal
  • 130
  • 2
  • 7