8

Possible Duplicate:
How do I write a short literal in C++?

Is there a suffix for short int numerical constants in C? There are u, l, d, and f for unsigned, long, double and float, but I could not find any for short.

I do some very low lever bit meddling and I need numerical constants with the size of 2 bytes. The system I work on guarantees that short ints are 2 bytes, so that is not a problem. It's embedded, so please don't bother with suggestions about portability.

Community
  • 1
  • 1
vsz
  • 4,811
  • 7
  • 41
  • 78
  • Do you need to specify short? Won't the compiler just shorten an int constant for you? or do you want bounds checking, etc.? Extreme solution: if it's embedded, are you using GCC and are you in a position to rebuild your GCC to add your own suffix? – Rup Nov 16 '11 at 10:10
  • Where do you actually need these numeric constants? The compiler ought to optimize `(short)1`. – Chris Lutz Nov 16 '11 at 10:13
  • How can that be an exact duplicate? The other question was about C++, which AFAIK has slightly different promotion rules than C. – glglgl Nov 16 '11 at 10:15
  • 2
    @glglgl In fairness [the accepted answer](http://stackoverflow.com/questions/208433/how-do-i-write-a-short-literal-in-c/208446#208446), `((short)2)`, is going to be the right answer here too. And [the bottom answer](http://stackoverflow.com/questions/208433/how-do-i-write-a-short-literal-in-c/209313#209313) cites C99. – Rup Nov 16 '11 at 10:26
  • 4
    -1 for "It's embedded, so please don't bother with suggestions about portability." Embedded and portable are not in any way mutually exclusive! – Vicky Nov 16 '11 at 13:02
  • @Vicky: The only reason I wrote this is because every time I ask any question about the internal representation of variables, nearly all the answers are the type of "you should not do this, because you can never know on what system your code will run". – vsz Nov 16 '11 at 22:17
  • @vsz: OK. So maybe "I will only be needing to run this code on one particular platform, so portability is not an issue" would be a better way to phrase it. – Vicky Nov 17 '11 at 09:54

1 Answers1

4

Checking the C standard, no such prefix exist for shorts.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621