5

While I was learning datatypes in c. I came across int_fast8_t and int_least8_t data types. I did not know these, So I have googled it. I have found some answers here:

The difference of int8_t, int_least8_t and int_fast8_t?

The answers are int_fast8_t is fastest but Iam surprised what things makes it fast. What techniques does compiler use to make it fast ? and also, When there exists int datatype and short,long modifiers to modify int size. What is the need of this (int_fast8_t) datatypes ?

If int_fast8_t is faster then we can simply ignore int and go for int_fast8_t always, since everyone needs speed.

Is there any limitations for int_fast8_t? What are the advantages or disadvantages between int_fast8_t and int.

halfer
  • 19,824
  • 17
  • 99
  • 186
niko
  • 9,285
  • 27
  • 84
  • 131
  • 2
    Well, to start with the bit-widths of `int` versus `int_fast8_t` might be different, meaning you can't really use `int_fast8_t` safely except as an 8-bit integer. – Some programmer dude Aug 10 '13 at 14:46
  • Ultimately, use it as if you were limited to that exact number of bits. The only problem to cope with is overflow/underflow, in which case you should be using a least-width type. If you know what you're doing or know something won't overflow, then a fast type may be beneficial. Safety first. :-) –  Aug 10 '13 at 16:37
  • Even a least width type won't make overflow/underflow defined. There is no guarantee there will be a type of the exact size requested on a given architecture, like, there are architectures where int_least32_t might be 36 bits. – John Meacham Jul 04 '14 at 00:21

3 Answers3

4

The short answer is compatibility if you ever move your code from one architecture to another.

int_fast8_t will probably be the same as int since in most (many? some?) architectures the int type is defined to be the size of the native word (32 bits for a 32 bit architecture, 64 bits in 64 bit architecture). Basically, int_fast8_t is saying that, if I'm interpreting correctly, that the underlying type will be the fastest that can hold at least 8 bits of information.

If you move your code to different architecture you can be sure you'll still have at least 8 bits to work with if you use int_fast8_t. You can not be sure of that if you simply use int.

GSP
  • 3,763
  • 3
  • 32
  • 54
  • 2
    @Leushenko Agree about minimum size of `int`. Thus, in conforming C, `int` on an 8-bit architecture is _not_ the "size of the native word" as @GSP suggest. (embedded processor are still often 8-bit.) – chux - Reinstate Monica Aug 10 '13 at 17:17
3

int_fast8_t is the fastest integer type that's at least 8 bits wide. There is no reason to think that it'll be faster than int (which is typically the fastest integer type there is.)

Besides, the two types could have different width, meaning they often can't be used interchangeably.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • so kindly let me know which is better to use int_fast8_t or int ? if iam aware my machine is specific of 8 bit size . should i go for int_fast8_t ? – niko Aug 10 '13 at 14:51
  • @niko: In a nutshell: use `int` until there is a clear, specific and measurable need to use something else. – NPE Aug 10 '13 at 14:52
  • Iam done with your answer and kindly one last thing what is the need of int_least8_t? – niko Aug 10 '13 at 14:53
3

For example, on a 32-bit platform, int is 32-bit, The CPU might have instructions to operate on 32-bit chunks of data. It might not have such instructions for 8-bit data. In this case, the CPU probably has to use some bitwise operations to process 8-bit types data.

On such platform, int_fast8_t may be implemented as 32-bit int. But you can't use it as a 32-bit int, you can only use it like a 8-bit type without undefined behavior.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 1
    Actually, only the `intptr_t` and `intNN_t` exact width types (and their corresponding unsigned types prefixed with 'u') are not required. `int_leastNN_t`, `int_fastNN_t` and `intmax_t` are all required. This is true for the values of NN where NN = 8, 16, 32, and 64. However, the fast types are not guaranteed to be the fastest on that platform. All that is guaranteed is the signedness and width requirements being satisfied. –  Aug 10 '13 at 15:54
  • @ChronoKitsune You are right, it's in C11 7.20.1.3, fixed my answer. – Yu Hao Aug 10 '13 at 15:58