Add a newline at the end of an integer using putchar () (C programming)?

I am new to C programming and I am trying to figure out how to add a new line ("\ n") after printing an int value using the putchar () function. In the book "C Programming" by K & R they provide the script below, but all characters are printed on one line. How do I change the code to print one character per line?

#include <stdio.h>

void main() {
int c;

c = getchar();
while (c != EOF) {
    putchar(c);
    c = getchar();
 }
}

      

I know I can use printf () with something like this:

printf("%d\n", c);

      

But I'm wondering if C programming and the putchar () function have something like:

putchar(str(c)+"\n");

      

Right off the bat in, say, python. Thank!

+3


source to share


1 answer


The function putchar

only writes one character at a time. If you want to print a newline after each character, add an extra call putchar

by specifying a newline character:



putchar(c);
putchar('\n');

      

+5


source







All Articles