Question

I'm new to programming and I can't seem to get my head around why the following happens in my code, which is:

#include <stdio.h>

/*copy input to output; 1st version */

main()
{
    int c;

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

So after doing some reading, I've gathered the following:

  1. Nothing executes until I hit Enter as getchar() is a holding function.
  2. Before I hit Enter, all my keystrokes are stored in a buffer
  3. When getchar() is called upon, it simply goes looks at the first value in the buffer, becomes that value, and then removes that value from the buffer.

My question is that when I remove the first c = getchar() the resulting piece of code has exactly the same functionality as the original code, albeit before I type anything a smiley face symbol immediately appears on the screen. Why does this happen? Is it because putchar(c) doesn't hold up the code, and tries to display c, which isn't yet defined, hence it outputs some random symbol? I'm using Code::Blocks if that helps.

Was it helpful?

Solution

The function you listed will simply echo back to you every character you type at it. It is true that the I/O is "buffered". It is the keyboard input driver of the operating system that is doing this buffering. While it's buffering keys you press, it echoes each key back at you. When you press a newline the driver passes the buffered characters along to your program and getchar then sees them.

As written, the function should work fine:

c = getchar();   // get (buffer) the first char

while (c != EOF) {  // while the user has not typed ^D (EOF)
    putchar(c);     // put the character retrieved
    c = getchar();  // get the next character
}

Because of the keyboard driver buffering, it will only echo back every time you press a newline or you exit with ^D (EOF).

The smiley face is coming from what @YuHao described: you might be missing the first getchar in what you ran, so putchar is echoing junk. Probably a 0, which looks like a smiley on your screen.

OTHER TIPS

If you ommit the first getchar(), the code will look like this:

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

Here, c is uninitialized, so calling putchar(c) the first time will output a garbage value, that's where you get the smiley face.

"I'm new to programming"

You're not advised to learn programming using C (and the difficulties you're going through are because of the C language). For example, my first computer science classes were in pascal. Other univesities may use scheme or lisp, or even structured natural languages to teach programming. MIT's online classes are given in python.

C is not a language you would want to use in the first months of programming. The specific reason in your case is due to the fact that the language allowed you to use the value of an uninitialized value.

When you declare the integer variable "c", it gets an implicitly reserved space on the program stack, but without have any meaningful value: it's "trash", the value is whatever value was already on memory at that time. The C language requires that the programmer implicitly knows that he needs to assign some value before using a variable. Removing the first getchar results in uses before assignment in the while condition (c != EOF) and putchar(c), both before c has any meaningful value.

Consider the same code rewritten in python:

import sys

c = sys.stdin.read(1)
while c != '':
    c = sys.stdin.read(1)
    sys.stdout.write(c)

Remove the initial read and you get the following error:

hdante@aielwaste:/tmp$ python3 1.py 
Traceback (most recent call last):
  File "1.py", line 3, in <module>
    while c != '':
NameError: name 'c' is not defined

That's a NameError: you used the value without assigned to it resulted in a language error.

For more information, try an online course, for example:

http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00-introduction-to-computer-science-and-programming-fall-2008/video-lectures/

About uninitialized values:

http://en.wikipedia.org/wiki/Uninitialized_variable

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top