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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:26:52+00:00 2026-05-15T20:26:52+00:00

I was taking a look at the assert() reference page and I got stuck

  • 0

I was taking a look at the assert() reference page and I got stuck while I read the given example:

/* assert example */
#include <stdio.h>
#include <assert.h>

int main ()
{
  FILE * datafile;
  datafile=fopen ("file.dat","r");
  assert (datafile);

  fclose (datafile);

  return 0;
}

In this example, assert is used to abort the program execution if datafile compares equal to 0, which happens when the previous call to fopen was not successful.

I totally agree that if fopen() fails, assert() will abort execution. However I’m concerned about the rightness of this example:

In my opinion assert() is there to detect cases that can’t normally happen (like passing a NULL pointer to a function whose documentation states it is forbidden).

In this example, failing to open a file is not something that can’t normally happen. In fact, I can see dozens of reasons why this would fail. The file couldn’t exist, the program could run without required privileges, and so on.

I would rather have done something like:

/* not longer an assert example */
#include <stdio.h>
#include <assert.h>

int main ()
{
  FILE * datafile;
  datafile=fopen ("file.dat","r");

  if (datafile != NULL)
  {
    // Do something, whatever.
    fclose (datafile);
  } else
  {
    // Report the error somehow.
  }

  return 0;
}

Is my understanding of how assert() should be used incorrect ?


EDIT AND GOOD NEWS !

It seems the referred site is ruled by rigorous people. Here is the mail I got back from one of the site maintainer:

Hi Julien, I have to agree, the
example code was poorly chosen. It has
now been just rewritten to something
more appropriate.

Many thanks for
pointing this out, and sorry for any
inconveniences this may have caused to
you.

Best regards,

And the updated example:

/* assert example */
#include <stdio.h>
#include <assert.h>

void print_number(int* myInt) {
  assert (myInt!=NULL);
  printf ("%d\n",*myInt);
}

int main ()
{
  int a=10;
  int * b = NULL;
  int * c = NULL;

  b=&a;

  print_number (b);
  print_number (c);

  return 0;
}

Glad to see that some people do their work well on the Internet ! 😉

  • 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-15T20:26:52+00:00Added an answer on May 15, 2026 at 8:26 pm

    You’re perfectly right sir. This is a poor usage of assert.

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

Sidebar

Related Questions

So taking a look at the API doc page here: http://developers.facebook.com/docs/reference/javascript/FB.api I'm wondering if
While taking a look at this awesome thread I noticed that some examples use
I was taking a look at this example, http://gardengnomesoftware.com/samples/pano2vr_3/mozart/ and I was really interested
I've been taking a look to Roslyn CTP and, while it solves a similar
Taking a look at my question HERE , I now want to return the
After taking a look at this SO question and doing my own research, it
Thanks for taking a look at this. The issue has to do with the
I was taking a look at Star Schema Benchmark and then I was thinking
I'm now taking a look at the PocketC powerful tool, but there is an
I'm taking a look at some random Icecast playlists (available here: http://dir.xiph.org/index.php ) and

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.