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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:48:06+00:00 2026-06-01T08:48:06+00:00

I was reading the section on C portability in the book C Traps and

  • 0

I was reading the section on C portability in the book C Traps and Pitfalls by Andrew Koening..

On an integer divison

q = a/b;
r = a%b;

If a is a negative number, apparently the reminder r can be a negative or positive number, while satisfying the property

q * b + r == a

Normally I would expect r to be negative if dividend a is negative. And that is what I see in a intel machine with gcc. I am just curious have you ever seen a machine that would return a positive reminder when the dividend is a negative number ?

  • 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-01T08:48:08+00:00Added an answer on June 1, 2026 at 8:48 am

    C99 formalized the remainder as having the same sign as the dividend. Prior to C99 (C89 and K&R), it could have gone either way as both results meet the technical requirements. There are indeed compilers out there non-conforming to the C99 spec in this matter, though I don’t know of any off the top of my head.

    In particular, section 6.5.5 (Multiplicative operators) states:

    ¶5 The result of the / operator is the quotient from the division of the
    first operand by the second; the result of the % operator is the
    remainder. In both operations, if the value of the second operand is
    zero, the behavior is undefined.

    ¶6 When integers are divided, the result of the / operator is the algebraic quotient with any
    fractional part discarded.87) If the quotient a/b is representable, the expression
    (a/b)*b + a%b shall equal a.

    87) This is often called “truncation toward zero”.

    With this new definition, the remainder is basically defined as what you’d expect it to be mathematically speaking.

    EDIT

    To address a question in the comments, the C99 spec also specifies (footnote 240) that if the remainder is zero, on systems where zero is not signed the sign of r will be the same as that of divisor, x.

    ‘‘When y ≠ 0, the remainder r = x REM y is defined regardless of the
    rounding mode by the mathematical relation r = x − ny, where n is the
    integer nearest the exact value of x/y; whenever | n − x/y | = 1/2,
    then n is even. Thus, the remainder is always exact. If r = 0, its
    sign shall be that of x.
    ’’ This definition is applicable for all
    implementations.

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

Sidebar

Related Questions

I am reading ECMAScript Language Specification Function Calls section Can someone rephrase or detailed
I'm reading Stroustrup's book, the section on overloading and related ambiguities. There's an example
I was reading the Simple Database section of Peter Siebel's book Practical Common Lisp
I am reading a C book. In the Arithmetic Expression section, they say: Division
Recently i'm reading the book Operating System Concepts Chapter VI about critical section problem,
I was reading the GoF book and in the beginning of the prototype section
I'm reading through a C++ book and I'm in a section about reducing the
I'm reading Silberschatz's OS book . In section 16.8.1 it says: The fundamental problem
When reading width of DIV section, the result comes in %. But, I want
while reading up djangobook chapter ,I came across the section which mentions a csrf

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.