Understanding Getchar() and Putchar() in C

  • Thread starter dE_logics
  • Start date
In summary: I'm trying to understand.- Warrengetchar() waits until the user types a key on their keyboard, and then returns that character to you.When you call getchar(), it waits until you press a key before it returns the character that was typed.
  • #1
dE_logics
742
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
  • #2
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:
  • #3
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
 
  • #4
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.
 
  • #5
Okay. Let us know if you still have questions.

- Warren
 
  • #6
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?
 
  • #7
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
 
  • #8
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.
 
  • #9
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!
 

Related to Understanding Getchar() and Putchar() in C

1. What is the purpose of getchar() and putchar() in C?

Getchar() and putchar() are functions in the C programming language that allow for reading and writing single characters from the standard input and output respectively. They are commonly used for user input and output in command line programs.

2. How do I use getchar() and putchar() in my C program?

To use getchar() and putchar() in your C program, you must first include the stdio.h header file. Then, use getchar() to read a single character from the user and putchar() to print a single character to the screen. Remember to assign the return value of getchar() to a variable.

3. Can I use getchar() and putchar() to read and write strings?

No, getchar() and putchar() can only read and write single characters. To read and write strings, you can use the gets() and puts() functions respectively.

4. How do I handle errors when using getchar() and putchar()?

When using getchar() and putchar(), it is important to check for errors. To check for errors, you can use the feof() function to check for end-of-file and the ferror() function to check for other errors. It is also important to handle any errors by printing an error message or terminating the program.

5. Are getchar() and putchar() portable across different operating systems?

Yes, getchar() and putchar() are portable functions and can be used on different operating systems. However, the behavior of these functions may differ slightly on different systems, so it is important to test your program on different platforms to ensure compatibility.

Similar threads

  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
4
Replies
118
Views
7K
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
19
Views
3K
  • Programming and Computer Science
Replies
3
Views
473
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
723
Back
Top