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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:05:47+00:00 2026-05-22T18:05:47+00:00

I recently came across this question , where the OP was having issues printing

  • 0

I recently came across this question, where the OP was having issues printing the hexadecimal value of a variable. I believe the problem can be summed by the following code:

#include <stdio.h>

int main() {
    char signedChar = 0xf0;

    printf("Signed\n”);
    printf(“Raw: %02X\n”, signedChar);
    printf(“Masked: %02X\n”, signedChar &0xff);
    printf(“Cast: %02X\n", (unsigned char)signedChar);

    return 0;
}

This gives the following output:

Signed
Raw: FFFFFFF0
Masked: F0
Cast: F0

The format string used for each of the prints is %02X, which I’ve always interpreted as ‘print the supplied int as a hexadecimal value with at least two digits’.

The first case passes the signedCharacter as a parameter and prints out the wrong value (because the other three bytes of the int have all of their bits set).

The second case gets around this problem, by applying a bit mask (0xFF) against the value to remove all but the least significant byte, where the char is stored. Should this work? Surely: signedChar == signedChar & 0xFF?

The third case gets around the problem by casting the character to an unsigned char (which seems to clear the top three bytes?).

For each of the three cases above, can anybody tell me if the behavior defined? How/Where?

  • 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-22T18:05:48+00:00Added an answer on May 22, 2026 at 6:05 pm

    I don’t think this behavior is completely defined by c standard. After all it depends on binary representation of signed values. I will just describe how it’s likely to work.

    printf(“Raw: %02X\n”, signedChar);
    

    (char)0xf0 which can be written as (char)-16 is converted to (int)-16 its hex representation is 0xfffffff0.

    printf(“Masked: %02X\n”, signedChar &0xff);
    

    0xff is of type int so before calculating &, signedChar is converted to (int)-16.
    ((int)-16) & ((int)0xff) == (int)0x000000f0.

    printf(“Cast: %02X\n", (unsigned char)signedChar);
    

    (unsigned char)0xf0 which can be written as (unsigned char)240 is converted to (unsigned int)240 as hex it’s 0x000000f0

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

Sidebar

Related Questions

I recently came across a question about sequence points in C++ at this site,
This is my first StackOverflow question so be nice! :-) I recently came across
I came across this recently and thought it would make a great SO question.
I came across this issue recently and thought it was really helpful. My question
I recently came across this question What is the BEST-WORST case time to add
I recently came across this interview question (posted in a forum somehwere... looks like
I came across this question recently - What is function try block handler? Also,
I recently came across this in some code - basically someone trying to create
I've been developing with PHP for some years now, and recently came across this
Recently I came across this error in my WCF trace: There was an error

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.