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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:19:08+00:00 2026-05-27T12:19:08+00:00

The code snippet char c1; char c2; c1 = 0X7F; c2 = 0X80; printf(c1

  • 0

The code snippet

char c1;
char c2;
c1 = 0X7F;
c2 = 0X80;
printf("c1 = %X\nc2 = %X\n", c1, c2);

outputs

c1 = 7F
c2 = FFFFFF80

why is it that c2 does not print as 80? (i’m guessing it’s something to do with the fact that the most significant bit of 0X80 is 1 while the MSB of 0x7F is 0). how can i get c2 to print as simply 80?

  • 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-27T12:19:09+00:00Added an answer on May 27, 2026 at 12:19 pm

    Because printf is a variadic function. Arguments to variadic functions undergo the default promotions; one effect of this is that char arguments are converted to int. On your platform, char is signed, so c2 is actually negative. printf is therefore printing all the leading ones that occur in the int (these are due to two’s-complement representation).

    There are (at least) 3 methods to work around this:

    • Use %02X rather than %X as your printf format specifier.
    • Use unsigned char rather than char (this gets promoted to unsigned int, so no leading ones)
    • Explicitly convert each of your chars to int, and then mask: (int)c2 & 0xFF.

    [Note: The corollary of this is that the behaviour of char c2 = 0x80 is actually implementation-defined; if char is signed then this will overflow.]

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

Sidebar

Related Questions

The code snippet is: char c[]=gate2011; char *p=c; printf(%s,p+p[3]-p[1]); The output is: 2011 Can
Here is a C code snippet char *p=Hello World; int a; char b; printf(%d\n,sizeof(p++));
I'm struggling a bit to understand why this code snippet does not compile. #include
In the below code snippet can i replace char * to const char *
Here's the code snippet: public static void main (String[]arg) { char ca = 'a'
gcc 4.4.1 c89 I have the following code snippet: #include <stdlib.h> #include <stdio.h> char
I'm wondering whether static constant variables are thread-safe or not? Example code snippet: void
Below is my code snippet struct encode { char code[MAX]; }a[10]; int main() {
Here is the code snippet: int main() { char ch=26; ofstream fout; fout.open(key.txt); if
consider this code snippet void make(int n) { std::string user_input; std::istringstream iss(user_input); char letter;

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.