+9  A: 

The type of '1' is int in C, not char, so you are reading SIZE*sizeof(int) bytes in each fread. If sizeof(int) is greater than 1 (on most modern computers it is), then you are reading past the storage for buffer. This is one of the places where C and C++ are different: in C, character literals are of type int, in C++, they are of type char.

So, you need chars_read = fread(buffer, 1, SIZE, stdin); because sizeof(char) is 1 by definition.

In fact, I would write your loop as:

while ((chars_read = fread(buffer, 1, sizeof buffer - 1)) > 0) {
    buffer[chars_read] = 0; /* In case chars_read != sizeof buffer - 1.
                               You may want to do other things in this case,
                               such as check for errors using ferror. */
    printf("%d, %s\n", chars_read, buffer);
}

To answer your another question, '\0' is the int 0, so {'\0'} and {0} are equivalent.

For setvbuf, my documentation says:

The size argument may be given as zero to obtain deferred optimal-size buffer allocation as usual.

Why are you commenting with \\ instead of // or /* */? :-)

Edit: Based upon your edit of the question, sizeof("1") is wrong, sizeof(char) is correct.

sizeof("1") is 2, because "1" is a char array containing two elements: '1' and 0.

Alok
Seriously, OMG! I didnt realise '1'. Blunder. thanks :)
N 1.1
\\ typo i was frustrated :P. What documentation do you use?
N 1.1
On linux, `man` - otherwise see POSIX: http://www.opengroup.org/onlinepubs/009695399/functions/fread.html. Also, you don't want `sizeof("1")`, see my edit.
Alok
sizeof("1")! saved again. i am overlooking everything!
N 1.1
I asked nvl about the `sizeof('1')` but I guess he overlooked it.
The Elite Gentleman
@Elite: You asked, but you didnt say that was the error. So, I overlooked.
N 1.1
A: 

char buffer[SIZE + 1] = {0};

This isn't doing what you expect, it is making buffer point to a one byte region in the programs constant data segment. I.e this will corrupt SIZE amount of bytes and possibly cause a memory protection fault. Always initialize C strings with strcpy() or equivalent.

gregery
Wrong! It does exactly what i intend to do with it.
N 1.1
@gregery: `char buffer[SIZE+1] = {0};` is perfectly valid. It sets each element of `buffer` to `0`.
Alok
Ok so it's valid, but I would rewrite it because it's bad style. The fact that the buffer is filled with '0' is not obvious and would require a reading the c standard to find out how partial initializations are filled. I never use compile time initialization on an array with variable content so it looks wrong to me.
gregery
A: 

Here's a byte-by-byte way to fread the lines from a file using redirection ./a.out < data.

Produces the expected output at least ... :-)

/*

Why does this code not output the expected output ?,
http://stackoverflow.com/questions/2378264/why-does-this-code-not-output-the-expected-output

compile with:
gcc -Wall -O3 fread-test.c

create data:
echo $'1line\n2line\n3line\n4line' > data

./a.out < data

*/

#include <stdio.h>

#define SIZE 5

int main(void) 
{

   int i=0, countNL=0;
   char singlechar = 0;
   char linebuf[SIZE + 1] = {0};
   setvbuf(stdin, (char *)NULL, _IOFBF, sizeof(linebuf)-1);  

   while(fread(&singlechar, 1, 1, stdin))     // fread stdin byte-by-byte
   {
      if ( (singlechar == '\n') )
      {
         countNL++;
         linebuf[i] = '\0';
         printf("%d:  %s\n", countNL, linebuf);
         i = 0;
      } else {
         linebuf[i] = singlechar; 
         i++;
      }
   }

   if ( i > 0 )    // if the last line was not terminated by '\n' ...
   {
      countNL++;
      linebuf[i] = '\0';
      printf("%d:  %s\n", countNL, linebuf);
   }

 return 0;

}
carlo