8

Is there a difference between unsigned short int and a unsigned short decleration in c and if so, what is it please ? I tried looking online, but couldn't find anything worthwhile.

unsigned short int x1;
unsigned short x2;

Lastly, if there is a difference, how can I cast them to each other respectively please?

Goaler444
  • 2,591
  • 6
  • 35
  • 53

5 Answers5

11

From C11 [PDF] (irrelevant parts omitted) (emphasis mine):

6.7.2.2:

At least one type specifier shall be given in the declaration specifiers in each declaration, and in the specifier-qualifier list in each struct declaration and type name. Each list of type specifiers shall be one of the following multisets (delimited by commas, when there is more than one multiset per item); the type specifiers may occur in any order, possibly intermixed with the other declaration specifiers.

  • ...
  • short, signed short, short int, or signed short int
  • unsigned short, or unsigned short int
  • ...

6.7.2.5:

Each of the comma-separated multisets designates the same type ...

Shahbaz
  • 46,337
  • 19
  • 116
  • 182
  • Important to note that the fact that they are the same does not guarantee what their size is. They could be 2 bytes, 4 bytes, 100 bytes, whatever. As long as they are no longer than `int`. – Daniel Apr 29 '13 at 16:33
  • 2
    If you want to link to a more convenient online copy of C11, here: http://port70.net/~nsz/c/c11/n1570.html – R.. GitHub STOP HELPING ICE Apr 29 '13 at 16:33
  • I'll keep your link in the comment, but would keep the link to the PDF in the answer. The good thing about the pdf is that you can download and put it on the desktop and is easier to access. Good link though, it's definitely better for quoting. – Shahbaz Apr 29 '13 at 16:49
  • @Daniel could I run into platform troubles if I send shorts through sockets connections? – Goaler444 Apr 29 '13 at 20:52
3

Just using short is a short-hand (no pun intended) way of writing short int. Just a long is a short-hand for long int.

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

No difference in the both.

The second is considered to be an int and are simply omitted.

BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
2

They are synonyms. If you're compiler does something different with them, it's broken.

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
-2

There is no difference. Try the sizeof operator:

main()
{
  unsigned short int x1;
  unsigned short x2;

  printf("%d/%d\n", sizeof x1, sizeof x2);
}
xtof pernod
  • 862
  • 5
  • 7
  • 7
    Like I said in another comment: Even though they _are_ the same (and your answer is correct), you can't present an example to prove something. 11 is prime, but that doesn't make all odd numbers prime. By the way, `int` and `unsigned int` also have the same size, but they are not same. – Shahbaz Apr 29 '13 at 16:22
  • Furthermore, the size of a type can change between different implementations. – effeffe Apr 29 '13 at 16:52