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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:30:05+00:00 2026-06-13T00:30:05+00:00

I know that when we assign a negative value to a unsigned datatype then

  • 0

I know that when we assign a negative value to a unsigned datatype then the two’s compliment of it gets stored, that is the maximum value that the datatype can store minus the negative value we have assigned.
To test that, I have written a program which illustrates that, however I am not able to understand the behavior of char datatype.

#include <iostream>
using namespace std;


template<class T>
void compare(T a,T b)
{
    cout<<dec<<"a:"<<(int)a<<"\tb:"<<(int)b<<endl; //first line
    cout<<hex<<"a:"<<(int)a<<"\tb:"<<(int)b<<endl; //second line

    if(a>b)
        cout<<"a is greater than b"<<endl;
    else
        cout<<"b is greater than a"<<endl;
}

int main()
{
    unsigned short as=2;
    unsigned short bs=-4;
    compare(as,bs);

    unsigned int al = 2;
    unsigned int bl =-4;
    compare(al,bl);

    char ac=2;
    char bc=-4;
    compare(ac,bc);

    int ai =2;
    int bi =-4;
    compare(ai,bi);
}

Output is

a:2     b:65532
a:2     b:fffc
b is greater than a
a:2     b:-4
a:2     b:fffffffc
b is greater than a
a:2     b:-4
a:2     b:fffffffc
a is greater than b
a:2     b:-4
a:2     b:fffffffc
a is greater than b

The compare(…) function is called for times with arguments of different datatypes

  1. unsigned short- 2 bytes , therefore -4 gets stored as 65532.
  2. unsigned int – 4 bytes, however as we are trying to typecast it to int while outputting it it is shown -4 in the output, so it is tricking the compiler, however the hex output and the logical comparison result shows that the internal representation is in two’s compliment.
  3. char – 1 byte, this is where I am getting confused.
  4. int – 4 bytes, signed datatype, nothing unexpected, normal result.

The question I have to ask is why char is behaving like a signed int?
Even though we are typecasting to int before outputting the first line in the result, why is char showing values similar to int, even when char is 1 byte and int 4 byte. unsigned short showed different value, because its memory requirement was 2 byte.
unsigned int and int is showing same result in the first line of the result, because both are 4 bytes, and the compiler gets tricked successfully, and is acceptable.
But why is char also showing the same value, as if its memory layout was the same as that of int?

And the logical comparison also shows that char does not behave as a unsigned datatype, but a signed one. unsigned datatypes are showing b as greater than one. While char is showing a is greater than b , in terms with signed datatype. Why?

Isn’t char 1 byte unsigned datatype?
This is what I learnt when I did a course on C and C++ in by B.Tech degree.

Any explaination would be helpful.
The compiler used is mingw 2.19.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-06-13T00:30:07+00:00Added an answer on June 13, 2026 at 12:30 am

    Isn’t char 1 byte unsigned datatype?

    Maybe, maybe not. The signedness of char is implementation-defined.

    In your current implementation, it is obviously signed.

    And in the output from the compare method, you get four bytes shown, because you cast to int for the output, so the char value -4 gets converted to the int value -4.

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

Sidebar

Related Questions

I know that I can assign a value specifically to a float by doing
I was curious to know what would happen if I assign a negative value
I know that I can assign an onclick action to <input type=button -style buttons.
I know that you can assign a class to the generated table by using
How to merge two PNG's together? I know that you can't use PNGObject.Draw because
Possible Duplicate: What happens if I assign a negative value to an unsigned variable?
I'm getting this error while doing rake:seed. can't mass-assign protected attributes I know that
Is it possible to assign by reference? I know that ref has to be
I know that I can hijack a form by showing a login form in
I know that if I retain an IBOutlet by using property then I have

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.