2

I've asked a question regarding the theoretical aspect of this code a couple of hours ago. Now I understand everything, however, that code simply gives no output. Here's the link to the code on http://ideone.com/LWMC5

Code:

#include <stdio.h>
#include <unistd.h> /* changed from "syscalls.h" it was not working */
/* changed the name intentionally */
int main(void)
{

    static char buf[BUFSIZ];
    static char *bufp = buf; 
    static int n = 0;
    if (n == 0) {            /* buffer is empty */
        n = read(0, buf, sizeof buf);
        bufp = buf;          
    }
    return (--n >= 0) ? (unsigned char) *bufp++ : EOF; 
}

What should I do/add to produce output?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Peter Cerba
  • 806
  • 4
  • 14
  • 26
  • 2
    You need to effectively output something. Hint: use a function that outputs (putchar(), write(), printf(), ...) :-) – pmg Aug 18 '12 at 18:33
  • 1
    What do you want to output? and what are you trying to accomplish with this code? The `if` statement is always going to be true since you declare `n = 0` immediately before. – rudolph9 Aug 18 '12 at 18:33
  • @rudolph9 I think You're not right about the if statement. It is true only in the first run, then n is equal to the value returned by read() – Peter Cerba Aug 18 '12 at 20:39
  • @PeterKowalski Consider calling the main function recursively (not that you should be calling main recursively but since this is where the code in question is, for the sake of argument suppose you did), each time `static int n = 0;` `n = 0`, hence each time `n == 0` is called the statement will return true and therefore code after the `if` statement will _always_ execute. – rudolph9 Aug 18 '12 at 20:58
  • @rudolph9 So I should delete static before int n ? Everything would be fine then? Or is the whole algorithm to be changed. Because I doubt that K&R would make such a big mistake ;) – Peter Cerba Aug 18 '12 at 21:04
  • @PeterKowalski you need to think about what you want you code to do and the logic of what you have currently implemented. You need to set the value of `n` somewhere if it is going to be the basis for your conditional statements. But, in the current implementation, because you declare `n = 0` immediately before your conditional statements (i.e. nothing is able to change `n` before your conditionals to make `n == 0` false), your program will _always_ execute the code after the `if` statement. (Note by _after the `if` statement_ I am referring the code which gets executed if `n == 0` is true) – rudolph9 Aug 18 '12 at 21:46

1 Answers1

1

I don't understand what did you expect as output, to print something on the screen, you need to use something like printf.

TOC
  • 4,326
  • 18
  • 21
  • I thought that return would display it on the screen.... How stupid I am. Sorry. Anyway thanks for pointing that out! – Peter Cerba Aug 18 '12 at 20:06
  • @PeterKowalski : You are not stupid! Like all humans when we start learning something new, we made some mistakes, they are essential for understanding – TOC Aug 18 '12 at 23:05