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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:33:05+00:00 2026-06-08T00:33:05+00:00

In am working on something and came across code similar to the following: #define

  • 0

In am working on something and came across code similar to the following:

#define MODULUS(a,b)        ((a) >= 0 ? (a)%(b) : (b)-(-a)%(b))

unsigned char w;
unsigned char x;
unsigned char y;
char z;

/* Code that assigns values to w,x and y.  All values assigned 
   could have been represented by a signed char. */

z = MODULUS((x - y), w);

It is my understanding that the arithmetic (x - y) will be accomplished prior to any type conversion and the macro will always evaluate to (a)%(b) — as the result will be an unsigned char which is always greater than or equal to zero. However, the code functions as intended and I think that my understanding is flawed. So…

My questions are these:

  1. Does an implicit type conversion to signed char occur before the expression is evaluated?

  2. Is there a situation (for example, if the unsigned values were large enough that they could not be represented by a signed value) where the above code would not work?

  • 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-08T00:33:06+00:00Added an answer on June 8, 2026 at 12:33 am

    Does an implicit type conversion to signed char occur before the expression is evaluated?

    No, a conversion to int occurs before the expression x - y is evaluated¹. Thus the result can be negative.

    Is there a situation (for example, if the unsigned values were large enough that they could not be represented by a signed value) where the above code would not work?

    If sizeof int == 1, the integer promotion would promote the unsigned chars to unsigned ints, and that could produce wrong results because before the modulus by w, a modulus by UINT_MAX + 1 is performed due to unsigned arithmetic.

    ¹ The default integer promotion.

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

Sidebar

Related Questions

I was working on some data parsing code while I came across the following.
I was working on some code recently and came across a method that had
Im working my way through some C++ code and came across the following void
While working away on an old piece of code I came across something like
While working my way through the Android tutorials, I came across something I don't
I've been working with PHP lately, and I came across something I couldn't solve.
I came across this code while working through code-analysis warnings on our code base.
I am trying to get some code working from an example I came across.
today when I was working on some code of mine I came across a
I'm working with some legacy code and I came across a function which is

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.