Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 656739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:44:50+00:00 2026-05-13T22:44:50+00:00

This can be a good question for finding bugs. No? Okay for beginners at

  • 0

This can be a good question for finding bugs.
No? Okay for beginners at least.

#define SIZE 4
int main(void){
  int chars_read = 1;
  char buffer[SIZE + 1] = {0};  
  setvbuf(stdin, (char *)NULL, _IOFBF, sizeof(buffer)-1);  
  while(chars_read){
    chars_read = fread(buffer, sizeof('1'), SIZE, stdin);
    printf("%d, %s\n", chars_read, buffer);
  }
  return 0;
}

Using the above code, I am trying to read from a file using redirection ./a.out < data. Contents of input file:

1line
2line
3line
4line

But I am not getting the expected output, rather some graphical characters are mixed in.
What is wrong?


Hint: (Courtesy Alok)

  • sizeof('1') == sizeof(int)
  • sizeof("1") == sizeof(char)*2

So, use 1 instead 🙂

Take a look at this post for buffered IO example using fread.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:44:50+00:00Added an answer on May 13, 2026 at 10:44 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a pretty basic question but I can't find a good answer for
I found some good answers for this question, but I can't really get them
I can't seem to find an answer to this question or a good example
Is there a good Flex IDE/development env that works on Ubuntu? Where can this
is this good code? can it be simplified somehow? SELECT u.id,u.title,u.title,u.first,u.last FROM (((tblusers u
So I can't seem to find a good example of this anywhere. I found
Googling this does little good, as you can imagine. Does anyone have resources that
Can anyone please help me with this...I'm not very good with regular expressions and
Can someone explain this to me. I am pretty good with perl regular expressions
Asserts can't be caught. This is good because some errors I don't want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.