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

The Archive Base Latest Questions

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

void fun(){ signed int a=-5; unsigned int b=-5; printf(the value of b is %u\n,b);

  • 0
void fun(){
    signed int a=-5;
    unsigned int b=-5;
    printf("the value of b is %u\n",b);
    if(a==b)
         printf("same\n");
    else
         printf("diff");
}

It is printing :

4294967291

same

In the 2nd line signed value is converted to unsigned value. So b has the value UINTMAX + 1 – 5 = 4294967291.

My question is what is happening in the comparison operation .

1) Is a again converted to unsigned and compared with b ?

2) Will b(ie unsigned ) be ever casted to signed value and compared automatically?

3) Is conversion from unsigned to signed undefined due to int overflow ?

I have read other posts on the topic. I just want clarification on questions 2 and 3 .

  • 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-30T15:24:35+00:00Added an answer on May 30, 2026 at 3:24 pm

    1) Is a again converted to unsigned and compared with b ?

    Yes. In the expression (a==b), the implicit type conversion called “balancing” takes place (the formal name is “the usual arithmetic conversions”). Balancing rules specify that if a signed and a unsigned operand of the same size and type are compared, the signed operand is converted to a unsigned.

    2) Will b(ie unsigned ) be ever casted to signed value and compared automatically?

    No, it will never be converted to signed in your example.

    3) Is conversion from unsigned to signed undefined due to int overflow ?

    This is what the standard says: (C11)

    6.3.1.3 Signed and unsigned integers

    1 When a value with integer type is converted to another integer type
    other than _Bool, if the value can be represented by the new type, it
    is unchanged.

    2 Otherwise, if the new type is unsigned, the value is
    converted by repeatedly adding or subtracting one more than the
    maximum value that can be represented in the new type until the value
    is in the range of the new type.

    3 Otherwise, the new type is
    signed and the value cannot be represented in it; either the result is
    implementation-defined or an implementation-defined signal is raised.

    In other words, if the compiler can manage to do the conversion in 2) above, then the behavior is well-defined. If it cannot, then the result depends on the compiler implementation.

    It is not undefined behavior.

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

Sidebar

Related Questions

From the lectures notes of a course at university, on call-by-value: void fun(int *ip)
My code looks like this _declspec(naked) void f(unsigned int input,unsigned int *output) { __asm{
I use to write code like this: void fun(char *buff, unsigned size) { std::strstream
I have two C++ files, say file1.cpp and file2.cpp as //file1.cpp #include<cstdio> void fun(int
#include <cstdio> #include <string> void fun(const char* c) { printf(--> %s\n, c); } std::string
I wrote a timer following some examples from the web: void fun(int i) {
Which of these 2 methods is better and why? Method 1: void fun(int i)
Suppose you have a function: void fun(int *a, int *b); Which way you would
What are the benefits of using variadic functions void fun(int i, ...); instead of
#include <stdio.h> void fun(char a[]){ a[0]^=a[1]^=a[0]^=a[1]; } int main(int argc, char **argv){ char b[10];

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.