Omitting the datatype (e.g. "unsigned" instead of "unsigned int")

I know that if the data type declaration is omitted in C/C++ code in such way: unsigned test=5;, the compiler automatically makes this variable an int (an unsigned int in this case). I've heard that it's a C standard and it will work in all compilers.

But I've also heard that doing this is considered a bad practice.

What do you think? Should I really type unsigned int instead of just unsigned?

Are short, long and long long also datatypes?

Answers


unsigned is a data type! And it happens to alias to unsigned int.

When you’re writing unsigned x; you are not omitting any data type.

This is completely different from “default int” which exists in C (but not in C++!) where you really omit the type on a declaration and C automatically infers that type to be int.

As for style, I personally prefer to be explicit and thus to write unsigned int. On the other hand, I’m currently involved in a library where it’s convention to just write unsigned, so I do that instead.


I would even take it one step further and use stdint's uint32_t type. It might be a matter of taste, but I prefer to know what primitive I'm using over some ancient consideration of optimising per platform.


As @Konrad Rudolph says, unsigned is a datatype. It's really just an alias for unsigned int.

As to the question of using unsigned being bad practice? I would say no, there is nothing wrong with using unsigned as a datatype specifier. Professionals won't be thrown by this, and any coding standard that says you have to use unsigned int is needlessly draconian, in my view.


Gratuitous verbosity considered harmful. I would never write unsigned int or long int or signed anything (except char or bitfields) because it increases clutter and decreases the amount of meaningful code you can fit in 80 columns. (Or more likely, encourages people to write code that does not fit in 80 columns...)


Need Your Help

How can I add a boolean value to a NSDictionary?

objective-c iphone cocoa-touch uikit

Well, for integers I would use NSNumber. But YES and NO aren't objects, I guess. A.f.a.i.k. I can only add objects to an NSDictionary, right?

Determine if an event has been attached to yet

vb.net events event-handling

I have two objects - one that contains some code with will fire an event, and one that contains the handler for that event. I can't "AddHandler" in the Load of the first object, because an instance...