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

  • Home
  • SEARCH
  • 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 658813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:58:30+00:00 2026-05-13T22:58:30+00:00

Say we have an 8-bit unsigned integer n ( UINT8_MAX=255 ); what is the

  • 0

Say we have an 8-bit unsigned integer n (UINT8_MAX=255); what is the behavior of the compiler for n=256? Where can I find a table of default behavior when the value of a data type is out of range for different data types? Is there a pattern to how they behave when set out of range?

#include <stdio.h>
#include <inttypes.h>

uint8_t n = UINT8_MAX;
int main() {
  printf("%hhu ",n++);
  printf("%hhu",n);
  return 0;
}

Compiling with gcc -std=c99 -Wall *.c, this prints: 255 0

Also, is it acceptable to use C99’s PRI* macros? How are they named?

  • 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-13T22:58:30+00:00Added an answer on May 13, 2026 at 10:58 pm

    n=256; converts the signed integer value 256 to uint8_t, then assigns it to n. This conversion is defined by the standard to take the value modulo-256, so the result is that n is set to 0.

    Not sure where you can find a table, but the rules for integer conversions are at 6.3.1.3:

    1 When a value with integer type is
    converted to another integer type
    other than _Bool, if the value can be
    represented by the new type, it is
    unchanged.

    2 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.49)

    3 Otherwise, the new type is signed
    and the value cannot be represented in
    it; either the result is
    implementation-defined or an
    implementation-defined signal is
    raised

    As AndreyT points out, this doesn’t cover what happens when an out-of-range value occurs during a calculation, as opposed to during a conversion. For unsigned types that’s covered by 6.2.5/9:

    A computation involving unsigned
    operands can never overflow, because a
    result that cannot be represented by
    the resulting unsigned integer type is
    reduced modulo the number that is one
    greater than the largest value that
    can be represented by the resulting
    type.

    For signed types, 3.4.3/3 says:

    EXAMPLE An example of undefined behavior is the behavior on integer overflow.

    (indirect, I know, but I’m too lazy to keep searching for the explicit description when this is the canonical example of undefined behavior).

    Also note that in C, the integer promotion rules are quite tricky. Arithmetic operations are always performed on operands of the same type, so if your expression involves different types, there are a list of rules to decide how to choose what type to do the operation in. Both operands are promoted to this common type. It’s always at least an int, though, so for a small type like uint8_t, arithmetic will be done in an int and converted back to uint8_t on assignment to the result. Hence for example:

    uint8_t x = 100;
    uint8_t y = 100;
    unsigned int z = x * y;
    

    results in 10000, not 16 which would be the result if z were a uint8_t too.

    Also, is it acceptable to use C99’s PRI* macros? How are they named?

    Acceptable to whom? I don’t mind, but you might want to check whether your compiler supports them or not. GCC does in the earliest version I have lying around, 3.4.4. They are defined in 7.8.1.

    If you don’t have a copy of the C standard, use this: http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf. It’s a “draft” of the standard, released some time after the standard was published and including some corrections.

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

Sidebar

Ask A Question

Stats

  • Questions 349k
  • Answers 349k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The key to understanding what is going on in the… May 14, 2026 at 6:49 am
  • Editorial Team
    Editorial Team added an answer Latitude and longitude in MapKit are stored as CLLocationDegrees types,… May 14, 2026 at 6:49 am
  • Editorial Team
    Editorial Team added an answer getElementsByTagName always operates in the context of element it is… May 14, 2026 at 6:48 am

Related Questions

In ARM assembly immediates are encoded by an 8-bit rotated value which means we
Say we have the following code: int main(){ int a[3]={1,2,3}; printf( E: 0x%x\n, a);
I have a program where I generate bitstreams, of about 80 to 150 bits
We are processing IBMEnterprise Japanese COBOL source code. The rules that describe exactly what
I'm doing research into a web API for my company, and it's starting to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.