9

I need to use unsigned double in C structure, but i can't compile. An error says: short, signed or unsigned invalid for `myvar'

this is my code:

#include <stdlib.h>
#include <stdio.h>

struct eReg{
   int key;
   unsigned char a;
   unsigned short int b;
   unsigned double myvar;
   }MyReg;

Does anyone know what is wrong?

EDIT I did not know "unsigned double" equals "unsigned float", then I didn't knew I had to find the answer to my question as "unsigned floats in C?" Anyway after reading the post, I've accepted my question as "duplicate". I suggest deleting this question.

  • 3
    There is no such thing as an `unsigned double` – dmeglio Sep 07 '15 at 01:41
  • 1
    Just use `double`, delete `unsigned`. It is not a valid data type in C. – user12205 Sep 07 '15 at 01:44
  • 2
    [Why doesn't C have unsigned floats?](http://stackoverflow.com/q/512022/1708801) – Shafik Yaghmour Sep 07 '15 at 01:45
  • maybe you were thinking of `unsigned long long` vs `unsigned double` ? `double` would have made more sense as a modifier vs a type. We would have `long` and `double long`, then `float` and `double float`. We could have even had `double double float` instead of `decimal` because having `float`, `double`, and `decimal` all being non-integer number types is not intuitive. `double int` and then `double double int` would have also been intuitive similarly. And then you have to ask yourself "why wasn't `byte` a native type instead of `char` which infers that it holds a character vs a byte? – Cerniuk Jan 12 '20 at 15:49

1 Answers1

11

You can not have unsigned floating point types. Therefore no unsigned double or unsigned float. unsigned can only be applied to integer types like char short int and long. The reason we have unsigned integers is that they can store numbers twice as large, and can be better suited for handling binary data. I can't really think of much of an advantage to using unsigned floating point types. You could get a little more precision but this is probably not worth the effort of implementing it in hardware and software.

chasep255
  • 11,745
  • 8
  • 58
  • 115