0

I've always seen people writing for(int ii = 0; ii < 50; ii ++)

but for numbers < 256, why don't people write char ii instead of int ii since it's sure that it will be 8bits and not more?

David 天宇 Wong
  • 3,724
  • 4
  • 35
  • 47
  • 2
    `50` is an `int` (so is `0`); if you use `char` for the controlling variable you will force the compiler to do extra conversions at every comparison (of course the compiler can optimize the conversions out). – pmg Mar 30 '14 at 14:30
  • 5
    *since it's sure that it will be 8 bits and not more* This is wrong. `char` is not necessarily 8 bits, and even if it is, it is not guaranteed to be able to hold 255, because the implementation may use plain signed chars. For example, `for (char ii = 0; ii < 255; ii++)` is dangerous and would only work as expected in platforms with unsigned chars. – Filipe Gonçalves Mar 30 '14 at 14:32
  • 1
    char may be slower than int. Read about CPU architecture and registers. A bit of Assembly knowledge is always good for C developer. – Alex F Mar 30 '14 at 14:34
  • see http://stackoverflow.com/questions/881894/is-char-guaranteed-to-be-exactly-8-bit-long-in-c , okay so char must be at least 8 bits. So it should be smaller than an int if not equal. – David 天宇 Wong Mar 30 '14 at 15:00
  • didn't think about unsigned char though, I'm just looking at optimization but I guess changing char to int is a negligible optimization. – David 天宇 Wong Mar 30 '14 at 15:01
  • if you really care about micro-optimization that much, why are you writing `ii++`, and not `++ii`? – Elias Van Ootegem Mar 30 '14 at 15:03
  • @David天宇Wong Compilers handle most of these optimizations and they handle them better than humans on average. No point in optimizing unnecessarily. – avmohan Mar 30 '14 at 15:16
  • @EliasVanOotegem care to expand? What's the difference between ii++ and ++ii? – David 天宇 Wong Mar 30 '14 at 15:29
  • @David 天宇 Wong "So it should be smaller than an int if not equal" is true. But smaller can be _slower_. Processors, _in general_, perform best at `int` size. – chux - Reinstate Monica Mar 30 '14 at 15:29
  • @chux: you probably meant compiler. ;-) – Deduplicator Mar 30 '14 at 15:34
  • Any modern compiler can be expected to generate the same code for a stand alone `ii++` and `++ii`. (thank-you @Deduplicator) – chux - Reinstate Monica Mar 30 '14 at 15:37
  • 1
    @David天宇Wong: Bottom line: `ii++` can cause a copy of `ii`'s value to be stored for later use. Of course, in this case any half-decent compiler can optimize this code, but since this question is entirely about (IMO pointless) over-optimization, just thought I'd let you know – Elias Van Ootegem Mar 30 '14 at 15:51

1 Answers1

5

C does all arithmetic of values smaller than int on the value converted to int, and the compiler will likely put ii into a register, so going for a smaller type won't win you anything. It might even be worse due to constantly converting back to (char).

Next, you have a problem: char is only guaranteed to go up to 127, so 255 would be out of bounds. You would have to go for unsigned char.

Not that that signifies in your example loop. For this one, there's most likely no difference after compiling.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118