Understanding Getchar() and Putchar() in C

  • Thread starter Thread starter dE_logics
  • Start date Start date
AI Thread Summary
getchar() in C reads one character at a time from the input buffer, which is typically filled when the user presses the Enter key. This means that multiple characters typed by the user are not processed individually until the line is complete, causing confusion for those expecting immediate feedback. putchar() prints a single character passed to it, and if called repeatedly without new input, it will print the same character until a new one is assigned. The discussion clarifies that the behavior observed is due to input/output buffering, where the program waits for a complete line before processing characters. Understanding this buffering is crucial for effectively using getchar() and putchar() in C programming.
dE_logics
Messages
742
Reaction score
0
Every time I start off with programming, I get stuck with these get and putchar functions.

First off, as appose to what's in the books, the getchar takes in more than a character...it can take in complete pages, forget strings.

Then the putchar only prints one character...after printing that, if it's called again, it's not that it'll print the next character that the variable has in store, but the same character again and again until we call getchar with the same variable assigned to it (for e.g. c = getchar()); NOW if putchar is called again, it will print the next character.

Surprisingly getchar, when called above, did not take in new inputs, but when it was called the first time, it did take new ones.

It appears that getchar() has a pointer applied on the variable which assigns to it. Assuming this variable to be c, if the function getchar() does not have anything stored in behalf of c, then it will take in fresh inputs.

Then, each time c = getchar() is called, instead of taking new inputs, this pointer which has been pointing to a character stored in c will now will point to the next character and putchar prints this pointed character.

Also I think the real information is stored in the function getchar(), since c = getchar() might destroy information in c if getchar() returns something.


I know I'm wrong...so can someone correct me?
 
Technology news on Phys.org
dE_logics said:
First off, as appose to what's in the books, the getchar takes in more than a character...it can take in complete pages, forget strings.

getchar reads one character. To read more than one character with getchar, you need a loop to read whatever you like one character at a time.

Then the putchar only prints one character...after printing that, if it's called again, it's not that it'll print the next character that the variable has in store, but the same character again and again until we call getchar with the same variable assigned to it (for e.g. c = getchar()); NOW if putchar is called again, it will print the next character.

putchar takes an argument, and prints out whatever you have in that argument.
It appears that getchar() has a pointer applied on the variable which assigns to it. Assuming this variable to be c, if the function getchar() does not have anything stored in behalf of c, then it will take in fresh inputs.

getchar is a macro, and it refers to the stdin stream. It gets the next character from that stream every time you call it.

Then, each time c = getchar() is called, instead of taking new inputs, this pointer which has been pointing to a character stored in c will now will point to the next character and putchar prints this pointed character.

Nope.

Also I think the real information is stored in the function getchar(), since c = getchar() might destroy information in c if getchar() returns something.

Any assignment statement replaces what was in the variable with a new value.

If you write c = 'Z', then this will replace whatever was previously in c with a new value.

Cheers -- sylas
 
Last edited:
Is this school work? If you're just trying to teach yourself programming, please don't start with ancient, painful C standard I/O functions!

- Warren
 
chroot said:
Is this school work? If you're just trying to teach yourself programming, please don't start with ancient, painful C standard I/O functions!

- Warren

It's in the course, I really don't have an option.
 
Okay. Let us know if you still have questions.

- Warren
 
sylas said:
getchar reads one character. To read more than one character with getchar, you need a loop to read whatever you like one character at a time.

getchar reads a char stored in the variable?

putchar takes an argument, and prints out whatever you have in that argument.

Suppose, I do putchar(c), and I made inputs of sheets of information in c...then putchar(c) should print the same sheets...but it's only printing a character!

I'm not getting anything here!

Any assignment statement replaces what was in the variable with a new value.

If you write c = 'Z', then this will replace whatever was previously in c with a new value.

Yes, I know, so if getchar() returns something, all values in c will be replaced right?
 
getchar() waits until the user types a key on their keyboard, and then returns that character to you.

putchar() prints only a single character(). That's why it's called put char.

What exactly are you trying to do?

- Warren
 
and then returns that character to you.

Returns?

Now, when I getchar, I can type in whole words, but actually I expect it to enter the next sequence of commands even if 1 character has been inputted (like with microsoft's getch()).

This character should get stored in a variable...that's expected.
 
So, you're saying that you expect execution of your program to stop executing on getchar() and wait for you to type a single character, but it continues to wait indefinitely, no matter how many characters you type?

Can you show us your code?

- Warren
 
  • #10
dE_logics said:
Suppose, I do putchar(c), and I made inputs of sheets of information in c...then putchar(c) should print the same sheets...but it's only printing a character!

A variable is a bit of memory where you can store a single value.

Your variable c can only store one character at a time. It cannot store a "sheet".

If you can show some code, I can tell you what it is doing.

Cheers -- sylas
 
  • #11
What you're observing, dE_logics, is I/O buffering. When you type characters, they don't go directly to your program. Their journey includes:

(1) The hardware sends them to the operating system (OS)
(2) The OS routes them to your console window
(3) Your console window returns them back to the OS
(4) The OS hands them to the C libraries
(5) The C library hands them to your program

There is a lot of buffering going on in all of those parts -- and for the most part it is completely transparent. However, there is one particular part of this journey that can trip people up:

Somewhere in step (3) or step (4), it is very common for characters not to be passed one at a time: instead, it is "line buffered" -- characters are saved up until you hit the enter key (or you type a very large number of characters), and then they are handed to the C libraries all at the same time.


Your program doesn't notice anything weird -- when you called getchar() all it knows is that it asked the C library to give it a character, and getchar() returned a character to it.

However, you notice something weird because you see both ends of this pipe -- you know you've typed a bunch of characters, but you don't see your program respond until you hit the enter key.


Note that if your program asks the C library for a character and there aren't any to be had, your program gets put to sleep until a character is available. Your program has no idea it was put to sleep -- all it knows is that it asked for a character, and it got one.
 
  • #12
I don't know what compiler you're using, but note that _cgets() has been broke in Visual Studio since 2002 (perhaps before). I'm not sure if getchar() suffers from a similar issue.
 
  • #13
chroot said:
So, you're saying that you expect execution of your program to stop executing on getchar() and wait for you to type a single character, but it continues to wait indefinitely, no matter how many characters you type?

That's what I expect from getchar()...but that doesn't happen, so I'm trying to understand what happens, or what's the behavior of getchar()?

I don't have a program...the topic is getchar().

sylas said:
Your variable c can only store one character at a time. It cannot store a "sheet".

If I make it an integer, it does store and print sheets.

putchar() can reproduce sheets with this loop -
Code:
 #include <stdio.h>

   /* copy input to output; 1st version  */
   main()
   {
       int c;

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

As printed in a book.

Hurkyl said:
(or you type a very large number of characters)

Apparently that limit is unknown to me :D...despite trying.

So actually this issue is more of a library problem.
Your program doesn't notice anything weird -- when you called getchar() all it knows is that it asked the C library to give it a character, and getchar() returned a character to it.

That function getchar() has stored the complete line and whenever it gets called, it assigns the value returned by it to a variable (as in the main program).

Each time getchar() gets called, it returns a new character if the thing entered by the user is not a character but a line...each call will return a new character from within the inputed line.

Very awkward behavior.

Jeff Reid said:
I don't know what compiler you're using, but note that _cgets() has been broke in Visual Studio since 2002 (perhaps before). I'm not sure if getchar() suffers from a similar issue.

gcc
 
  • #14
dE_logics said:
If I make it an integer, it does store and print sheets.

putchar() can reproduce sheets with this loop -
Code:
 #include <stdio.h>

   /* copy input to output; 1st version  */
   main()
   {
       int c;

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

Your variable is storing a single character only. But as the program runs, it stores every successive character in turn, prints that one characters, and then gets the next one, and so on, until the program is finished.

That function getchar() has stored the complete line and whenever it gets called, it assigns the value returned by it to a variable (as in the main program).

Each time getchar() gets called, it returns a new character if the thing entered by the user is not a character but a line...each call will return a new character from within the inputed line.

The function only works on one character at a time. What you are seeing is input output buffering. The operating system doesn't make input available to your program until you have a complete line. This is important, so that you can delete characters you have typed. When you hit return, all the line is then made available to the program, which processes it one character at a time using getchar.

Cheers -- sylas
 
  • #15
The function only works on one character at a time. What you are seeing is input output buffering. The operating system doesn't make input available to your program until you have a complete line. This is important, so that you can delete characters you have typed. When you hit return, all the line is then made available to the program, which processes it one character at a time using getchar.

So to getchar, an input buffer is made available instead of a character, each time it's called, it fetches a new character from the input buffer...as a result until the input buffer turns empty, to getchar it will always appear that something has been inputted.

The capacity to print 'sheets' is actually the capacity of the buffer and not the integer variable.

putchar simply prints this read character.
 
  • #16
dE_logics said:
So to getchar, an input buffer is made available instead of a character, each time it's called, it fetches a new character from the input buffer...as a result until the input buffer turns empty, to getchar it will always appear that something has been inputted.

The capacity to print 'sheets' is actually the capacity of the buffer and not the integer variable.

putchar simply prints this read character.

Yes, your first paragraph has got it.

As you dig deeper into the entrails of your operating system, there's a lot of stuff going on in the background, but in a nutshell, that's a good account. If the buffer is empty, then your program will wait until there's something in the buffer again.

When reading from a terminal or command window, the input buffer accessed by getchar is usually filled up a line a time. So when you hit return, your program will loop through as many times as it needs to get the characters from the line one at a time, until they are all processed, and then it will pause, waiting until you've filled up the next line of input and pressed return again. When reading from a disk, the buffer is filled a block at a time. Getchar doesn't care. This let's the programmer think of it as simply getting input one character at a time.

However, the capacity to print sheets is simply because you have a loop that prints every character you read; this doesn't depend on buffers to work. The program would still print sheets even if all the buffering was removed, because of the loop.

Cheers -- sylas
 
  • #17
Ok...thanks a lot, finally I got it.
 
  • #18
But I think there is no use of this end of file indicator that getchar() puts...since getchar() will store only one variable...

So what's the point of doing so?
 
  • #19
dE_logics said:
But I think there is no use of this end of file indicator that getchar() puts...since getchar() will store only one variable...

So what's the point of doing so?

getchar needs to have some way to indicate that it failed to get a character. That's actually why it returns a value of type integer, rather than a value of type char.

getchar will return the value EOF, which is an integer constant, when there is no more input available. Otherwise, it gets the next available character, and returns that. Hence the special EOF value must be different from every possible character value.

The loop keeps reading characters, until it getchar returns EOF. When getchar returns EOF, the loop finishes and the program terminates. Otherwise, it prints out whatever character was read, and then reads the next character, and loops around again back to the EOF test.

Cheers -- sylas
 
  • #20
Ok...so it was put to ensure it got a character.

Thanks a lot!
 

Similar threads

Replies
2
Views
11K
Replies
34
Views
4K
Replies
19
Views
5K
Replies
19
Views
2K
Back
Top