1

Just started on K&R and hit:

#include <stdio.h>
/* copy input to output; 1st version */
main()
{
    int c;
    c = getchar();
    while (c != EOF) {
        putchar(c);
        c = getchar();
    }
}

And it just displays what I input, even if it's a string of text.

cacti
cacti
stop repeating what i'm saying
stop repeating what i'm saying

Just like that.

What I don't get is why I can't just instantiate variable c with a string of text and print it the same way. (Ignoring the while loop for the sake the example) Like with:

main()
{
    int c;

    c = "cacti";
    putchar(c);
}

Wherein the output is apparently just 'd'

Jawi Mmm
  • 243
  • 2
  • 8
  • 4
    Each `getchar` reads one character and each `putchar` displays one character. `c` `a` `c` `t` `i`. The entire word is never stored at once. You don’t see each character echoed immediately because your input is buffered by line. `d` is what the last byte of the `"cacti"` pointer ended up being when interpreted as text. – Ry- Sep 16 '15 at 05:00
  • On which operating system? C does not know about the keyboard! See [this](http://stackoverflow.com/a/32547696/841108) & [that](http://stackoverflow.com/a/32392409/841108) – Basile Starynkevitch Sep 16 '15 at 05:23
  • @minitech Oh. that makes a lot of sense actually. Kay thanks! ..Ugh. I murdered this question :c – Jawi Mmm Sep 16 '15 at 06:12

2 Answers2

5

Here c is only integer which take a character for your case(Although its limits are larger than that).

For first code segment:

  • getchar() only return a character and you use while loop which take a character from stream and through putchar() it print to console until end of file. You seems to get a line of string and print it but it don't do that way.

In second code segment:

  • You put a const char* to integer which is not valid(My gcc 4.9.2 does not compile). So it show undefined character (Why print 'd' I don't know, may be compiler issue).

You can do this by:

main()
{
    char c[] = "cacti";
    puts(c);
}

EDIT According to getchar() definition:

On success, the character read is returned (promoted to an int value). The return type is int to accommodate for the special value EOF, which indicates failure.

When you type a single line and press enter then first character read outside of while loop. Than check if it is EOF(End Of File- No more data on the file . You can give EOF by pressing Ctrl+Z in windows and Ctrl+D in linux).

As if it is not encountered EOF, then it entered in while loop and print the singe character(by putchar(c)). After the next line c=getchar() take another character you entered in previous line and check while loop condition while(c!=EOF) being true it entered into loop and print a character. It continues until there found any character to print.

So it makes confuse with puts() which Writes a string in standard output. Where putchar() Writes a character to the standard output.

According to http://www.cplusplus.com/

Writes the C string pointed by str to the standard output (stdout) and appends a newline character ('\n').

ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42
  • Note that you usually use the yellow-ish 'quote' notation to quote someone else's work. It appears that you're using it for your own wording. While not absolutely wrong, it is a little unorthodox. Roughly, if you don't have an external reference to cite (preferably with a URL), you probably shouldn't be using the `>`-at-start-of-line quote mechanism. – Jonathan Leffler Sep 16 '15 at 05:15
  • Thanks @JonathanLeffler . Next time I'd careful about it. – ashiquzzaman33 Sep 16 '15 at 05:19
1

You could use a line-oriented input function to read whole lines. For example:

#include <stdio.h>

int main(void)
{
    char line[4096];
    while (fgets(line, sizeof(line), stdin) != 0)
        fputs(line, stdout);
    return 0;
}

For most plausible files, this will read one line at a time into the array of characters, and then write that line out. (If the line length is longer than 4095 characters, then lines will be processed in multiple segments.) If the file contains null bytes, then the material from the null byte to the end of line will be ignored. There are fixes for that — see standard C functions fread() and fwrite(), and POSIX function getline().


Note that your second example:

int main(void)
{
    int c;

    c = "cacti";
    putchar(c);
}

shouldn't compile without warnings about assigning a character pointer to an integer. When you use putchar() on the result, it probably prints the least significant byte of the address where the string "cacti" is stored. It is pure coincidence that the character printed is d.

The putchar() function only ever prints a single byte (which means a single character in many code sets, but it might only be part of a character in UTF-8).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278