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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:36:32+00:00 2026-05-23T20:36:32+00:00

There is a function called div in C,C++ (stdlib.h) div_t div(int numer, int denom);

  • 0

There is a function called div in C,C++ (stdlib.h)

div_t div(int numer, int denom);

typedef struct _div_t
{
  int quot;
  int rem;
} div_t;

But C,C++ have / and % operators.

My question is: “When there are / and % operators, Is div function useful?”

  • 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-23T20:36:33+00:00Added an answer on May 23, 2026 at 8:36 pm

    The div() function returns a structure which contains the quotient and remainder of the division of the first parameter (the numerator) by the second (the denominator). There are four variants:

    1. div_t div(int, int)
    2. ldiv_t ldiv(long, long)
    3. lldiv_t lldiv(long long, long long)
    4. imaxdiv_t imaxdiv(intmax_t, intmax_t (intmax_t represents the biggest integer type available on the system)

    The div_t structure looks like this:

    typedef struct
      {
        int quot;           /* Quotient.  */
        int rem;            /* Remainder.  */
      } div_t;
    

    The implementation does simply use the / and % operators, so it’s not exactly a very complicated or necessary function, but it is part of the C standard (as defined by [ISO 9899:201x][1]).

    See the implementation in GNU libc:

    /* Return the `div_t' representation of NUMER over DENOM.  */
    div_t
    div (numer, denom)
         int numer, denom;
    {
      div_t result;
    
      result.quot = numer / denom;
      result.rem = numer % denom;
    
      /* The ANSI standard says that |QUOT| <= |NUMER / DENOM|, where
         NUMER / DENOM is to be computed in infinite precision.  In
         other words, we should always truncate the quotient towards
         zero, never -infinity.  Machine division and remainer may
         work either way when one or both of NUMER or DENOM is
         negative.  If only one is negative and QUOT has been
         truncated towards -infinity, REM will have the same sign as
         DENOM and the opposite sign of NUMER; if both are negative
         and QUOT has been truncated towards -infinity, REM will be
         positive (will have the opposite sign of NUMER).  These are
         considered `wrong'.  If both are NUM and DENOM are positive,
         RESULT will always be positive.  This all boils down to: if
         NUMER >= 0, but REM < 0, we got the wrong answer.  In that
         case, to get the right answer, add 1 to QUOT and subtract
         DENOM from REM.  */
    
      if (numer >= 0 && result.rem < 0)
        {
          ++result.quot;
          result.rem -= denom;
        }
    
      return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to find what function called the current function? So for
In Python there is a really neat function called zip which can be used
In PHP 5.2 there was a nice security function added called input_filter, so instead
Is there any way to specify that a function should be called when a
I'm developing a Windows gadget. There is a function called addTextObject on the background
Sometimes this function can be called too quickly and multiple elements are created but
I have three classes that all have a static function called 'create'. I would
is there a difference in the dispose() functions being called ?
Is there any way to get the caller function with something else than debug_backtrace()?
Is there a function like document.getElementById(FirstDiv).clear() ?

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.