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 8318623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:02:23+00:00 2026-06-08T22:02:23+00:00

I was learning linux system programming, O’reilly. It says A common mistake is to

  • 0

I was learning linux system programming, O’reilly. It says “A common mistake is to declare the buffer as an automatic variable in a scope that ends before the stream is closed. Particularly, be careful not to provide a buffer local to main(),and then fail to explicitly close the stream.”

then it shows a buggy code example:

#include <stdio.h>
int main()
{
    char buf[BUFSIZ];

    /*set stdin to block-buffered with a BUFSIZ buffer*/
    setvbuf(stdout,buf,_IOFBF,BUFSIZ);
    printf("Arr!\n");
    return 0;

}

I compile and execute the code .. and don’t really understand what this kind of code will cause … please help me understand this concept, thank you all.

  • 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-06-08T22:02:25+00:00Added an answer on June 8, 2026 at 10:02 pm

    In that example, stdout will be flushed after main has returned.

    When that happens, buf is out of scope, you can’t legally use it anymore. So the program will exhibit undefined behavior.

    buf needs to live as long as stdout is open, and stdout often stays open until after main has returned. So you should use a global, static or heap-allocated buffer.

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

Sidebar

Related Questions

OS: Linux, Language: pure C I'm moving forward in learning C programming in general,
I have installed Lampp on my linux system, and I am learning symfony2, while
I am learning socket programming under Linux,so I make a sample program to list
I am learning linux programming and want to do the following. I would like
I am new to programming, and even newer to Linux. I was told that
I have just started learning Linux and Emacs. It was pleasant to have the
I'm learning the Linux kernel and reading the book The Linux Kernel . Can
I'm learning the C language on Linux now and I've came across a little
I'm learning Objective-C using GNUstep(because I use Linux). I was thinking in create a
I need to run Linux-Apache-PHP-MySQL application (Moodle e-learning platform) for a large number of

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.