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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:04:30+00:00 2026-05-24T04:04:30+00:00

Consider the following programs: // http://ideone.com/4I0dT #include <limits> #include <iostream> int main() { int

  • 0

Consider the following programs:

// http://ideone.com/4I0dT
#include <limits>
#include <iostream>

int main()
{
    int max = std::numeric_limits<int>::max();
    unsigned int one = 1;
    unsigned int result = max + one;
    std::cout << result;
}

and

// http://ideone.com/UBuFZ
#include <limits>
#include <iostream>

int main()
{
    unsigned int us = 42;
    int neg = -43;
    int result = us + neg;
    std::cout << result;
}

How does the + operator “know” which is the correct type to return? The general rule is to convert all of the arguments to the widest type, but here there’s no clear “winner” between int and unsigned int. In the first case, unsigned int must be being chosen as the result of operator+, because I get a result of 2147483648. In the second case, it must be choosing int, because I get a result of -1. Yet I don’t see in the general case how this is decidable. Is this undefined behavior I’m seeing or something else?

  • 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-24T04:04:30+00:00Added an answer on May 24, 2026 at 4:04 am

    This is outlined explicitly in §5/9:

    Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:

    • If either operand is of type long double, the other shall be converted to long double.
    • Otherwise, if either operand is double, the other shall be converted to double.
    • Otherwise, if either operand is float, the other shall be converted to float.
    • Otherwise, the integral promotions shall be performed on both operands.
    • Then, if either operand is unsigned long the other shall be converted to unsigned long.
    • Otherwise, if one operand is a long int and the other unsigned int, then if a long int can represent all the values of an unsigned int, the unsigned int shall be converted to a long int; otherwise both operands shall be converted to unsigned long int.
    • Otherwise, if either operand is long, the other shall be converted to long.
    • Otherwise, if either operand is unsigned, the other shall be converted to unsigned.

    [Note: otherwise, the only remaining case is that both operands are int]

    In both of your scenarios, the result of operator+ is unsigned. Consequently, the second scenario is effectively:

    int result = static_cast<int>(us + static_cast<unsigned>(neg));
    

    Because in this case the value of us + neg is not representable by int, the value of result is implementation-defined – §4.7/3:

    If the destination type is signed, the value is unchanged if it can be represented in the destination type (and bit-field width); otherwise, the value is implementation-defined.

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

Sidebar

Related Questions

Consider the following program: #include <iostream> #include boost/filesystem.hpp int main() { boost::filesystem::directory_entry d(test.txt); boost::filesystem::directory_entry
Consider the following program. #include<iostream> using namespace std; class base { public: int _bval;
Consider the following small program: #include <cstdio> int main() { printf(%d\n, 1); std::printf(%d\n, 2);
Lets us consider the following program : #include <stdlib.h> int main(int argc, char **argv){
Consider the following program #include <iostream> #include<cstdlib> using namespace std; class E { public:
Consider the following program: #include <iostream> #include <algorithm> using namespace std; template<class T> struct
Consider the following programm, #include<stdio.h> int main() { int marks[]={20,65,45,68,89}; int *x,*y; x=&marks[2]; y=&marks[4];
Consider the two following programs: program one int main() { printf( hello\n ); }
Consider following program: static void Main (string[] args) { int i; uint ui; i
Consider the following program, which is obviously buggy: #include <cstdio> double test(int n) {

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.