4

I'm new to C programming, and I'm trying to figure out how I can add a newline ("\n") after printing an int value using the putchar() function. In "The C Programming" book by K&R, they provide the script below, but all characters are printed on the same line. How can I modify 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 could use printf() with something like this:

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

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

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

Kinda the same way you would do it in, say, python. Thanks!

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
amucunguzi
  • 1,203
  • 1
  • 12
  • 16
  • 5
    Add another `putchar` call. `putchar('\n')` – Eugene Sh. May 15 '17 at 17:14
  • 5
    1) C is not a scripting language. 2) put**char** is not called put**string**. 3) No offence, but from your text, I'd recommend to learn the language from a good book and not to skip chapter. 4) `void main()` is an invalid signature. The minimum signature is `int main(void)`. 5) K&R does not cover modern C; get a more recent book which teaches **at least** C99, better standard C which is C11. – too honest for this site May 15 '17 at 17:17
  • 3
    In addition to the wisdom given by @Olaf, if you are wanting to learn C, then always enable **compiler warnings** by including, at minimum, `-Wall -Wextra` in your compile string, read and learn from what the compiler is telling you (they are quite good these days), and most of all, **never** accept code until it compiles cleanly, without warnings. – David C. Rankin May 15 '17 at 17:28
  • 1
    @Olaf & @David, thanks for the suggestions! I thought that `void main()` would mean that the main() function does not expect a `return` value, but it sounds like I had it wrong and will definitely get a recent book. (I'm taking the UC Berkeley CS61C - computer architecture - and they recommended reading K&R for learning C.) – amucunguzi May 15 '17 at 17:40
  • 1
    @Mucuansel: `main` has to return an `int` on a hosted environment (which POSIX, Windows, OSX, … are) platforms. There is a special exception for **this** function only, however: You don#t need an explicit `return 0;`. `main` is the only function which has an implicit `return 0;` if the code reaches end of the function without a explicit `return`. Using an explicit `return 0;` is good coding style, though. – too honest for this site May 15 '17 at 17:42
  • @DavidC.Rankin You say at minimum `-Wall -Wextra`. What other warnings do you recommend? – Erik W May 15 '17 at 18:37
  • If you want several extra, as well as type comparison warnings, add `-pedantic`. That will include almost all possible warnings, any additional warnings you want will have to be individually specified, (see the help for your compiler, e.g. [**man 1 gcc**](https://linux.die.net/man/1/gcc)) – David C. Rankin May 15 '17 at 19:34
  • Good advice given by @DavidC.Rankin and others about enabling warnings; I typically use `-Wall -Wextra -pedantic` as a minimum. [Here is a recent SO question](http://stackoverflow.com/questions/43963271/which-compilation-flags-should-i-use-to-avoid-run-time-errors) with many suggestions for warnings you might want to enable. – ad absurdum May 15 '17 at 20:51

1 Answers1

5

The putchar function only writes a single character at a time. If you want to print a newline after each character, add an extra call to putchar, giving it the newline character:

putchar(c);
putchar('\n');
dbush
  • 205,898
  • 23
  • 218
  • 273