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

  • Home
  • SEARCH
  • 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 1051441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:55:57+00:00 2026-05-16T16:55:57+00:00

In a test, I’m discarding anything from stderr since it clutters the output of

  • 0

In a test, I’m discarding anything from stderr since it clutters the output of the test case. I’m using the following code:

freopen("/dev/null", "w", stderr);

When compiling with -Wall -Werror, I get the error

error: ignoring return value of ‘freopen’, declared with attribute warn_unused_result

which is expected. However, the usual solution of casting to void doesn’t seem to work. That is, changing the code to

(void) freopen("/dev/null", "w", stderr);

still produces the same warning. I don’t care if this function fails since the worst case scenario is a bit of extra output. Any other way I can fix this?

EDIT: I know I could introduce an extra unnecessary variable. I would really like to know why casting to void doesn’t work.

UPDATE:
I decided to go with this:

FILE *null = fopen("/dev/null", "w");
if (null) { fclose(stderr); stderr = null; }

After reading the freopen documentation more carefully, I see that if opening /dev/null fails, stderr will still be destroyed. This solves that problem.

  • 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-16T16:55:58+00:00Added an answer on May 16, 2026 at 4:55 pm

    Why not simply use the result, as the warning suggests you should.

    if (freopen("/dev/null", "w", stderr) == 0)
        ...oops...lost stderr...hard to report errors...
    

    Since the function is declared with the ‘warn_unused_result’ attribute, you will get the warning unless you use the return value. Since the function either returns null on failure or the file stream argument on success, you might think about assigning the result. However, you should not
    assign to stderr like that (see below), so this is a bad idea:

    stderr = freopen("/dev/null", "w", stderr);
    

    Theoretically, you should make that check; there are dire (and implausible) circumstances under which you could fail to open “/dev/null”.


    Footnote 229 in the C99 standard notes:

    229) The primary use of the freopen function is to change the file associated with a standard text stream
    (stderr, stdin, or stdout), as those identifiers need not be modifiable lvalues to which the value
    returned by the fopen function may be assigned.

    Therefore, the assignment is ill-advised. But testing the return value would deal with the compiler warning and might help prevent core dumps too. It is unlikely to improve your code coverage figures, though (the error path is not going be taken very often; it will be hard to force coverage of the error handling).

    Note that the POSIX description of freopen() has some moderately caustic comments about the design of freopen(), which was invented by the C standard committee (1989 version), presumably without input from POSIX.

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

Sidebar

Related Questions

Test the following code: #include <stdio.h> #include <stdlib.h> main() { const char *yytext=0; const
{test_url=>http://test.com/123.jpg} is my output from <% @results.each do |t| %> <%= t.image.extract!(test_url) %> <%
Test string: the%20matrix%20 How can I delete the last three chars? Using this code
#test code: #!/bin/bash #~/test/test.sh trap echo 'testmessage' DEBUG while : do echo abc sleep
I test password recovery, but there are errors. Rspec study recently. code (User Controller)
Test Site Example: http://cincinnati-website-design.net/sandbox/ I am using the Avada Theme and can't seem to
test.php $aaa = $_POST['aaa']; echo $aaa; Javascript Code $.ajax({ url: 'ajax/test.php', data: { aaa:
Test case schema and data follow as: create table tmp ( vals varchar(8), mask
Test case: <script language=JavaScript> setInterval(function (){ window.location.hash='#hash'+Math.random() },400); </script> <a href=http://google.com/>click me</a> (if you
-------test code(test.html)------------ <!DOCTYPE html> <html> <head> <style> *{padding:0px;margin:0px;} </style> </head> <body style=background-color:orange;> <b onmousemove=tip(this);>A</b>

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.