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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:17:42+00:00 2026-06-11T01:17:42+00:00

I was working with integers in C, trying to explore more on when and

  • 0

I was working with integers in C, trying to explore more on when and how overflow happens.

I noticed that when I added two positive numbers, the sum of which overflows, I always got a negative number.

On the other hand, if I added two negative numbers, the sum of which overflows, I always got a positive number (including 0).

I made few experiments, but I would like to know if this is true for every case.

  • 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-11T01:17:43+00:00Added an answer on June 11, 2026 at 1:17 am

    Integer overflows are undefined behavior in C.

    C says an expression involving integers overflows, if its result after the usual arithmetic conversions is of a signed typed and cannot be represented in the type of the result. Assignment and cast expressions are an exception as they are ruled by the integer conversions.

    Expressions of unsigned type cannot overflow; they wrap, e.g., 0U - 1 is UINT_MAX.

    Examples:

    INT_MAX + 1    // integer overflow
    UINT_MAX + 1   // no overflow, the resulting type is unsigned
    (unsigned char) INT_MAX // no overflow, integer conversion occurs 
    

    Never let any integer expression overflow; modern compilers (like gcc) take advantage of integer overflows being undefined behavior to perform various types of optimizations.

    For example:

    a - 10 < 20
    

    when a is of type int after promotion, the expression is reduced in gcc (when optimization is enabled) to:

    a < 30
    

    It takes advantage of the expression being undefined behavior when a is in the range INT_MIN + 10 - 1 to INT_MIN.

    This optimization could not be done when a is unsigned int because if a is 0, then a - 10 has to be evaluated as UINT_MAX - 9 (no undefined behavior). Optimizing a - 10 < 20 to a < 30 would then lead to a different result than the required one when a is 0 to 9.

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

Sidebar

Related Questions

I'm working with integers and trying to do calculations that involve decimals at parts.
I am working with two-dimensional arrays of 16-bit integers defined as int16_t e[MAX_SIZE*MAX_NODE][MAX_SIZE]; int16_t
I am working on a program that requires reading in integers from a file
I'm very new to python. two days. trying to get a plot working with
I'm working on a program that will allow me to multiply/divide/add/subtract binary numbers together.
I am trying to perform logic based on the values of two integers. Here
I am working only with integers and want to create a new instance of
I am working on GPU device which has very high division integer latency, several
This is the code for my array (which is working) Public numUsers As Integer
I had this, which was working fine: public static Integer[] photos = new Integer[]

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.