3

Okay so, I'm pretty new to C.

I've been trying to figure out what exactly is the difference between putch() and putchar()? I tried googling my answers but all I got was the same copy-pasted-like message that said:

putchar(): This function is used to print one character on the screen, and this may be any character from C character set (i.e it may be printable or non printable characters).

putch(): The putch() function is used to display all alphanumeric characters through the standard output device like monitor. this function display single character at a time.

As English isn't my first language I'm kinda lost. Are there non printable characters in C? If so, what are they? And why can't putch produce the same results?

I've tried googling the C character set and all of the alphanumeric characters there are, but as much as my testing went, there wasn't really anything that one function could print and the other couldn't.

Anyways, I'm kind of lost.

Could anyone help me out? thanks!

TLDR; what can putchar() do that putch() can't? (or the opposite or something idk)

dunno, hoped there would be a visible difference between the two but can't seem to find it.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
why
  • 37
  • 4
  • What's `putch()`? Do you mean `putc()`? The C language specifications do not define a function named `putch()`. – John Bollinger Nov 20 '22 at 16:55
  • The difference is that `putchar` is a Standard function which will work everywhere, whereas `putch` is, IIRC, a nonstandard MS-DOS function which is totally obsolete today. I urge you to always use `putchar`, and forget about `putch`. – Steve Summit Nov 20 '22 at 16:57
  • See also [this analogous question about `getch` and `getchar`](https://stackoverflow.com/questions/9180001). – Steve Summit Nov 20 '22 at 16:59
  • This is a great question, and one which I wondered about, too. – Neil Nov 20 '22 at 19:08

2 Answers2

1

putchar() is a standard function, possibly implemented as a macro, defined in <stdio.h> to output a single byte to the standard output stream (stdout).

putch() is a non standard function available on some legacy systems, originally implemented on MS/DOS as a way to output characters to the screen directly, bypassing the standard streams buffering scheme. This function is mostly obsolete, don't use it.

Output via stdout to a terminal is line buffered by default on most systems, so as long as your output ends with a newline, it will appear on the screen without further delay. If you need output to be flushed in the absence of a newline, use fflush(stdout) to force the stream buffered contents to be written to the terminal.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • I don't understand, so is putchar better because it outputs a single byte to the output stream instead of just outputting characters to the screen directly? wouldn't putch be better in this case because the computer has to do less to print out the character? – why Nov 20 '22 at 17:58
  • 1
    The standard output stream, aka `stdout` is by default directed to the terminal (aka the screen). `putch` is an obsolete, non portable alternative, not worth learning about. Forget it and do not use it. – chqrlie Nov 20 '22 at 18:01
0

putch replaces the newline character (\ n)

putchar is a function in the C programming language that writes a single character to the standard output

AlCo2
  • 45
  • 8
  • 1
    This answer is misleading: `putchar` also performs newline conversion on legacy systems that still use this ancient convention borrowed from CP/M. The conversion is performed at a lower level of implementation, namely when the stream buffer contents is written to the system handle. `putch` does it differently but the result is the same and using this function makes the program needlessly non portable. Do not fall for [Microsoft EEE strategy](https://en.wikipedia.org/wiki/Embrace,_extend,_and_extinguish). – chqrlie Nov 20 '22 at 18:04