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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:15:36+00:00 2026-05-29T07:15:36+00:00

I wrote this small piece of code to test something: #include <stdio.h> int main()

  • 0

I wrote this small piece of code to test something:

#include <stdio.h>

int main()
{
    unsigned  x = 1;
    signed char y = -1 ;
    if (x>y)
        printf("X > Y");
    else
        printf("X <= Y");
    return 0;
}

The output I got was “X <= Y”. Isn’t 1 > -1?

  • 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-29T07:15:37+00:00Added an answer on May 29, 2026 at 7:15 am

    We have:

    unsigned  x = 1;
    signed char y = -1;
    

    this expression:

    x > y
    

    is used as the controlling expression of the if statement.

    After usual arithmetic conversions the right operand y will be converted to an unsigned int value. The result of the conversion of the negative signed char value -1 will be a huge unsigned int value (equal to UINT_MAX).

    So the expression x > y will be evaluated as:

    1U > UINT_MAX
    

    which is always false (i.e., evaluated to 0).

    This is the short version. To explain how do we come to this result with the C standard rules, I explain it below.

    Here is how it goes:

    The > relational operator is used, here it was C says on the relational operators:

    Relational operators (C99, 6.5.8p3) “If both of the operands have arithmetic type, the usual arithmetic conversions are performed.”

    Ok, in our example both operand are of integer types and integer types are of arithmetic types. So usual arithmetic conversion will be done. What are the usual arithmetic conversions?

    Usual arithmetic conversions (C99, 6.3.1.8p1) “Otherwise, the integer promotions are performed on both operands. Then the following rules are applied to the promoted operands:`

    Ok, first integer promotions is done on each operand. How are the integer promotions performed?

    Integer promotions (C99, 6.3.1.1p2) “if an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.”

    y is of signed char type so it is first promoted to int after integer promotions and x is of unsigned int type and stays an unsigned int.

    Then usual arithmetic conversions will find a common type between both operands. In our case, it means this:

    Usual arithmetic conversions (suite) (C99, 6.3.1.8p1) “Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.”

    unsigned int has the same conversion rank as the int type (remember signed char was promoted to int), so the promoted y will be converted from int (after promotion) to unsigned int. For information, integer conversion ranks are defined in (C99, 6.3.1.1p1).
    As you can notice, unsigned int wins over int in usual arithmetic conversions, another way of saying this is to say that unsigned is sticky.

    Now how is an int value of -1 (the signed char -1 after its promotion to int) converted to an unsigned int value?`. Here is what C says on integer conversion in this specific case:

    Integer conversions (C99, 6.3.1.3p2) “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.”

    This paragraph is written so its meaning remains independent of the signed number representation. For two’s complement representation, it means the int value -1 is converted to (UINT_MAX + 1) - 1, which is equal to UINT_MAX. So in our specific case,

    x > y
    

    is equivalent to

    1U > UINT_MAX
    

    is equivalent to

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

Sidebar

Related Questions

I wrote this tiny code: #include <stdio.h> int main() { size_t temp; temp =
I wrote this small C program: #include <stdio.h> #include <stdlib.h> int main() { FILE
I've been working on this small piece of code that seems trivial but still,
I am new to jquery . I wrote small piece of code <div id=tablediv><table><tr><td><h2>Select
I've wrote a small piece of code that reads the current battery charge/discharge on
I wrote this snippet of code and I assume len is tail-recursive, but a
I wrote this code I have these errors Cannot implicitly convert type x.Program.TreeNode' to
I wrote this small page and for some reason when I cilck on the
I wrote this small snippet to calculate Fibonacci numbers. It works well for numbers
I wrote a small example to test my understanding of pointers and memory, however

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.