Printf(), Unsigned Char, And %hhd - Stack Overflow
-
- Home
- Questions
- Tags
- Users
- Companies
- Labs
- Jobs
- Discussions
- Collectives
-
Communities for your favorite technologies. Explore all Collectives
- Teams
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Try Teams for free Explore Teams - Teams
-
Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams
Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about CollectivesTeams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about TeamsGet early access and see previews of new features.
Learn more about Labs printf(), unsigned char, and %hhd Ask Question Asked 6 years, 5 months ago Modified 6 years, 5 months ago Viewed 7k times 1The program:
#include <stdio.h> #include <limits.h> int main( void ) { #ifdef __CHAR_UNSIGNED__ printf( "%d\n", __CHAR_UNSIGNED__ ); #endif printf( "%d\n", CHAR_MAX ); printf( "%d\n", CHAR_MIN ); printf( "%hhd\n", CHAR_MAX ); }Output (on my x86_64 desktop):
127 -128 127That is as expected. Now, I ran the same on a Raspberry Pi (ARM):
1 255 0 -1So... apparently I have misunderstood some step CHAR_MAX is taking in its way to output, as the output I was expecting in that last line -- passing CHAR_MAX to %hhd on a machine with char being unsigned -- would have been 255.
(And if you should ask, the result is the same for (char)CHAR_MAX, and (unsigned char)CHAR_MAX.)
What am I missing?
This happened to me during regression testing my own printf() implementation on the Raspberry Pi -- which, by the way, does print 255. So... only one implementation is doing it right, and I have the sinking feeling it's not mine...
Share Improve this question Follow edited Jul 2, 2018 at 17:47 DevSolar asked Jul 2, 2018 at 17:41 DevSolarDevSolar 70k21 gold badges137 silver badges215 bronze badges 5- 1 "Output (on my x86_64 desktop):..." You're missing a line. A very important line. – Mad Physicist Commented Jul 2, 2018 at 17:42
- 1 Think about what that __CHAR_UNSIGNED__ being non-zero could mean... And remember that it's implementation specific if char is signed or unsigned. – Some programmer dude Commented Jul 2, 2018 at 17:44
- What is __CHAR_UNSIGNED__ ??? – Stargateur Commented Jul 2, 2018 at 17:44
- @MadPhysicist: Actually I was missing two lines in my program, which I added after the x86_64 compiler barked at me. ;-) – DevSolar Commented Jul 2, 2018 at 17:44
- @Someprogrammerdude: I am thinking about what __CHAR_UNSIGNED__ means. That was why I was expecting the ARM output for %hhd to read 255 instead of -1 for CHAR_MAX... – DevSolar Commented Jul 2, 2018 at 17:45
1 Answer
Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 8The format "%hhd" is for a signed byte. If you pass an unsigned byte then you technically have undefined behavior. To print an unsigned byte you need to use "%hhu".
What really happens is because of how two's complement integers work, and that 0xff is equal to -1.
Share Improve this answer Follow answered Jul 2, 2018 at 17:47 Some programmer dudeSome programmer dude 408k35 gold badges412 silver badges642 bronze badges 6- head -> desk. Of course. {Cursing voice fading into the background...} – DevSolar Commented Jul 2, 2018 at 17:48
- So now I may debug my printf() to figure out what went wrong - wrong in there. :-D Geeeeeez.... – DevSolar Commented Jul 2, 2018 at 17:48
- 2 Hmmm... on second thought... if passing an unsigned value to %hhd is UB, does it really matter that my implementation gives a different result...? (Just talking to myself here, feel free to not answer. ;-) ) – DevSolar Commented Jul 2, 2018 at 17:53
- @DevSolar is your implementation give 0 for printf( "%hhu\n", 256); ? I suspect not ;) – Stargateur Commented Jul 2, 2018 at 19:36
- @Stargateur: It does, actually. ;-) – DevSolar Commented Jul 2, 2018 at 20:03
Your Answer
Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Draft saved Draft discardedSign up or log in
Sign up using Google Sign up using Email and Password SubmitPost as a guest
Name EmailRequired, but never shown
Post Your Answer DiscardBy clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Not the answer you're looking for? Browse other questions tagged
or ask your own question.- The Overflow Blog
- Even high-quality code can lead to tech debt
- Featured on Meta
- More network sites to see advertising test [updated with phase 2]
- We’re (finally!) going to the cloud!
Related
0 Weird result when using the %d specifier to print an unsigned char in C 3 Why does printing an unsigned char sometimes work and sometimes not? In C 74 How to print an unsigned char in C? 0 Magical printing with unsigned char in C 3 Unsigned and Signed int and printf 2 printf not printing unsigned int 3 Signed and unsigned characters behavior while printing in C 31 Unsigned values in C 1 unsigned characters and sprintf() C 4 Unsigned integer printHot Network Questions
- Sharing own software with a restricted group of persons
- Understanding DC solenoid inrush current (oscilloscope readings)
- tabularray : use of "@{ ... }" in tblr
- Are Terms of Service enforceable upon AI generated content?
- What were other physicists' opinions on David Bohm's book "Quantum Theory"
- Are these stars or noise around Saturn?
- Publish a paper about a contribution already briefly outlined in one of my papers?
- Subscript alignment
- Is it (always) better to build a model prior to viewing the data?
- How safe are NTA-877 e-bike helmets for real world use?
- Why a 95%CI for difference of proportions and a 2x2 Chi-square test of independence don't agree
- What is Horatio’s role in Hamlet?
- PSE Advent Calendar 2024 (Day 3): A cacophonic crossword
- Are any two recursive languages reducible to one another?
- 'such that' - how to pronounce and what is their function?
- The Desktop, Downloads and Documents folders have disappeared from the Sidebar
- Do prime numbers ever occur in nature? That is, would their occurrence be de facto proof of intelligence?
- Minimal pair /u/ and /ʊ/
- Why does the Global Positioning System include particular numbers of satellites?
- STRING_SPLIT with order not working on SQL Server 2022
- Criteria for a number being a square-pyramidal number
- What happened in this battle in Ender's Game?
- are both my drives bad? this doesnt make sense
- Tracing light through a house of mirrors
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
lang-cTừ khóa » C Hhd
-
C-data Type-format Specifiers-use Of %hhd - Sololearn
-
"%hhd" - Visual Studio Feedback
-
What Is Hybrid Hard Drive (HHD)? - Definition From
-
Sscanf Print Format Problem - %hhd - C / C++ - Bytes
-
UGREEN USB C Hard Drive Enclosure For 2.5" SATA SSD HDD ...
-
UGREEN USB C Hard Drive Enclosure USB 3.1 Gen 1 Type C To ...
-
Ugreen USB C Hard Drive Enclosure
-
Ugreen 2.5 Inch USB C Hard Drive Enclosure
-
The Difference Between C Language %d, %c And %hhd, The ...
-
External Hard Drives - Officeworks
-
HHD Software: Hex Editor, Serial/USB/Network Sniffer, Virtual Serial ...
-
The Best External Hard Drives For 2022 - PCMag
-
Best External Hard Drive And SSD For 2022: Mac, PC, PS4, PS5 And ...
-
2.5-Inch Type-C Portable Hard Drive Enclosure-奥睿科官网 - ORICO