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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:09:24+00:00 2026-05-17T19:09:24+00:00

stdint.h in C99 provides many options for integer sizes, types and ranges – so

  • 0

stdint.h in C99 provides many options for integer sizes, types and ranges – so many I don’t know what ones to choose!

I know how to use size_t and ptrdiff_t when appropriate, and I use fixed size types for storage and transmission. My question concerns values that will only be stored in memory of the host machine.

For example, a structure for an image might contain these members:

struct image {
    integer width, height; /* pixel dimensions of the image */
    integer bits_per_pixel;
    ...
};

If width and height will never exceed SHRT_MAX, should a short be used, or stick with int? An image can’t have negative width or height, so use an unsigned type? Perhaps (u)int_least16_t is the correct choice? Something else?

If bits_per_pixel will never exceed a value of 64 use char, unsigned char, uint8_t, int or something else?

What would you use in this example and why?

How does the CPU architecture the code will run on affect the choice? i.e. PPC or x86, 32 or 64bit.
How does the device the code will run on affect the choice? i.e. Desktop, phone, console.
How does the choice relate to performance and optimization?

My question in simple terms is: How do you choose which integer to use?

  • 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-17T19:09:24+00:00Added an answer on May 17, 2026 at 7:09 pm

    I would say: Don’t worry to much about this, it often is a form of premature optimisation. But my rules of thumb are:

    • Use plain int when possible. It should be the natural word size of the machine.
    • Use unsigned types when you need well-defined integer overflow.
    • Use an (u)intX_t type when you need two’s-complement representation.
    • Use unsigned char for large arrays with values <= UCHAR_MAX.

    Beware that a lot of the types in <stdint.h> are optional, so you can’t depend on their existence. POSIX makes this slightly better.

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

Sidebar

Related Questions

Background Unfortunately the current C++ standard lacks C99's exact-width types defined in the stdint
To my amazement I just discovered that the C99 stdint.h is missing from MS
Boost's C99 stdint implementation is awfully handy. One thing bugs me, though. They dump
In C99, I include stdint.h and that gives me UINT32_MAX as well as uint32_t
I'm writing a bignum library, and I want to use efficient data types to
The stdint.h header lacks an int_fastest_t and uint_fastest_t to correspond with the {,u}int_fastX_t types.
I'm looking at stdint.h and given that it has uint16_t and uint_fast16_t, what is
I have this piece of code #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h>
If I execute the following code in C: #include <stdint.h> uint16_t a = 4000;
I need access to the uint64_t typedef from stdint.h in some wrapper code that

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.