Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Theory Behind getchar() and putchar() Functions

Tags:

c

getchar

putchar

I'm working through "The C Programming Language" by K&R and example 1.5 has stumped me:

#include <stdio.h>

/* copy input to output; 1st version */
int main(int argc, char *argv[])
{
    int c;

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

    return 0;
}

I understand that 'getchar()' takes a character for 'putchar()' to display. However, when I run the program in terminal, why is it that I can pass an entire line of characters for 'putchar()' to display?

like image 214
Raeven Avatar asked Jul 09 '13 15:07

Raeven


People also ask

What is the use of getchar () and putchar () functions?

Here the getchar() function takes a single character from the standard input and assigns them to a ch variable. Whereas the putchar() function prints the read character.

What is the purpose of Putchar function?

putc() – putchar() — Write a Character The putc() function converts c to unsigned char and then writes c to the output stream at the current position. The putchar() is equivalent to putc( c , stdout) . The putc() function can be defined as a macro so the argument can be evaluated multiple times.

How does the Getchar function work?

getchar is a function in C programming language that reads a single character from the standard input stream stdin, regardless of what it is, and returns it to the program. It is specified in ANSI-C and is the most basic input function in C. It is included in the stdio. h header file.

How does Putchar work in C?

putchar is a function in the C programming language that writes a single character to the standard output stream, stdout. Its prototype is as follows: int putchar (int character) The character to be printed is fed into the function as an argument, and if the writing is successful, the argument character is returned.


2 Answers

Because your terminal is line-buffered. getchar() and putchar() still only work on single characters but the terminal waits with submitting the characters to the program until you've entered a whole line. Then getchar() gets the character from that buffer one-by-one and putchar() displays them one-by-one.

Addition: that the terminal is line-buffered means that it submits input to the program when a newline character is encountered. It is usually more efficient to submit blocks of data instead of one character at a time. It also offers the user a chance to edit the line before pressing enter.

Note: Line buffering can be turned off by disabling canonical mode for the terminal and calling setbuf with NULL on stdin.

like image 140
Kninnug Avatar answered Nov 18 '22 18:11

Kninnug


Yeah you can actually write whatever you want as long as it's not an EOF char, the keyboard is a special I/O device, it works directly through the BIOS and the characters typed on the keyboard are directly inserted in a buffer this buffer is, in your case read by the primitive getchar(), when typing a sentence you are pushing data to the buffer, and the getchar() function is in an infinite loop, this is why this works.

You can ask me more questions if you want more details about how the IO device work.

Cheers.

like image 32
Belkacem REBBOUH Avatar answered Nov 18 '22 20:11

Belkacem REBBOUH