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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:37:21+00:00 2026-06-08T02:37:21+00:00

Given this code : #include <stdio.h> #include <assert.h> void print_number(int* somePtr) { assert (somePtr!=NULL);

  • 0

Given this code :

#include <stdio.h>
#include <assert.h>

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

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

  b=&a;

  print_number (c);
  print_number (b);

  return 0;
}

I can do this instead :

#include <stdio.h>
#include <assert.h>

void print_number(int* somePtr) {
  if (somePtr != NULL)
       printf ("%d\n",*somePtr);
  // else do something 
}

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

  b=&a;

  print_number (c);
  print_number (b);

  return 0;
}

So , what am I gaining by using assert ?

Regards

  • 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-08T02:37:23+00:00Added an answer on June 8, 2026 at 2:37 am

    assert is to document your assumptions in the code. if statement is to handle different logical scenarios.

    Now in your specific case, think from the point of view of the developer of the print_number() function.

    For example when you write

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

    you mean to say that,

    In my print_number function I assume that always the pointer coming is not null. I would be very very surprised if this is null. I don’t care to handle this scenario at all in my code.

    But, if you write

    void print_number(int* somePtr) {
      if (somePtr != NULL)
           printf ("%d\n",*somePtr);
      // else do something 
    }
    

    You seem to say that, in my print_number function, I expect people to pass a null pointer. And I know how to handle this situation and I do handle this with an else condition.

    So, sometimes you will know how to handle certain situations and you want to do that. Then, use if.
    Sometimes, you assume that something will not happen and you don’t care to handle it. You just express your surprise and stop your program execution there with assert.

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

Sidebar

Related Questions

I came across this code: #include<stdio.h> void main() { int x; float t; scanf(%f,&t);
Given this code : #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main(void) { int
Given this C code compiled with gcc 4.3.3 #include <stdio.h> #include <stdlib.h> int main(int
I have a program that has this code : #include<stdio.h> main(){ int input; char
This code gives me warnings: $ cat test.c #include<stdio.h> #include<time.h> int main() { time_t
Given this code : #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> #include <unistd.h>
Consider the following code: #include <stdio.h> void main() { uint32_t num = 2; __asm__
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
I study this code from some book: #include <pthread.h> #include <stdio.h> /* Parameters to
In the code given below: #include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> #include

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.