4

I was wondering if there are any side-effects defining uint.

typedef unsigned int uint;
Soft Waffle
  • 356
  • 1
  • 12
  • read question: https://stackoverflow.com/questions/5678049/difference-between-uint-and-unsigned-int and answers to that question, and the comments. They are pretty useful. – safakeskin May 14 '20 at 10:55
  • 1) namespace pollution 2) redundant repetition – pmg May 14 '20 at 10:55
  • If it is for lazy typing there are two things to do, a) use `unsigned` or b) search and replace when most of the work has been done. – Weather Vane May 14 '20 at 11:10
  • Related though, before stdint.h standardization, it was very common to name the type `u8`, `s8`, `u16` etc. But that didn't become standard, so we don't use that any longer. – Lundin May 14 '20 at 11:17
  • 1
    Whilst I'm having a rant, don't ever write `typedef char* string;`. For starters hiding pointers is a memory accident waiting to happen. And all tokens starting with `str` followed by a lower case letter are reserved in C. – Bathsheba May 14 '20 at 11:18
  • 4
    @Bathsheba Then you might find it slightly provoking that Harvard uni are currently teaching the whole world to always use `typedef char* string;` in their fiasco class called CS-50. – Lundin May 14 '20 at 11:43
  • Guess that explains the quality of some of the `cs50`-tagged questions then ^^ – Felix G May 14 '20 at 14:14

3 Answers3

4

Reading code like that is pain in the neck.

If you want to save some typing then use unsigned. That's shorthand for an unsigned int and is standard C. Standard C is readable C.

If you are attempting to standardise the sizes and ranges of integral types, then use the ones in <stdint.h> instead.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
4

You might be tempted to define uint as typedef unsigned int uint; to have the type unsigned int defined with a single word, for various reasons:

  • for stylistic reasons, because of local tradition.
  • out of consistency with types defined in <stdint.h> that all use int and uint for the signed and unsigned types (ex: int8_t and uint8_t).
  • to have all basic types defined with a single word, as might be required for token pasting macros. Of course unsigned would fit this need, but ulong would still be useful.

I can see these potential problems:

  • reading your code, some people might not know what type uint is supposed to be, or might wonder if uint is defined as something unexpected.
  • if you compile on some systems where this type is already defined in standard header files or some library's header files, you would get a compile error, thereby making your code less portable.

Also if you are a time traveller, stop by Dennis Ritchie's office in Murray Hill sometime late 1974 when C was described by this C Reference Manual, before unsigned was even invented and suggest uint as an alternative. I'm sure Ken Thomson would back you. And also tell Stu Feldman to avoid TABs :)

chqrlie
  • 131,814
  • 10
  • 121
  • 189
3

The negative effect is stylistic, namely that making your own home-brewed types turns your code unreadable. Nobody knows what "uint" is, so using such a home-made type is far worse practice than using unsigned int.

Instead of this, you should use the standardized integer types from stdint.h.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 2
    The standardized integer types in `` are not an alternative to using a typedef for `unsigned int`. As a matter of fact, `uint` would be consistent with the naming convention used in `` – chqrlie May 14 '20 at 10:59
  • I found a solution for my lazy typing too! I added this in my vimrc: `autocmd FileType c :iabbrev uint unsigned int` – Soft Waffle May 14 '20 at 11:03
  • @chqrlie Yes they are, because you shouldn't be making such typedefs to begin with. – Lundin May 14 '20 at 11:04
  • which one of these types are you refering to? the specified width types are optional, and the other ones are very cumbersome to use in `printf` and `scanf` format strings. – chqrlie May 14 '20 at 11:09
  • @PaulFilipenco If "having to type a lot" is a problem for you, then programmer isn't exactly the best trade to pick. If "don't have to type" is a more important argument to you than code readability, then you need to fix your priorities. – Lundin May 14 '20 at 11:09
  • @Lundin The negative effect is that there will appear Lundin and will say that he does not know what is uint.:) – Vlad from Moscow May 14 '20 at 11:09
  • @chqrlie: Cumbersome it may be, but correct. This is a good answer. – Bathsheba May 14 '20 at 11:10
  • @VladfromMoscow And then there is `unit meter;` vs `uint meter;` and other such typo goodies... – Lundin May 14 '20 at 11:11
  • @Lundin: don't forget `uint metre` and `uint metre` too! – Bathsheba May 14 '20 at 11:13
  • @Lundin "Having to type a lot" is easily fixed with a decent editor. The second point is valid. – chepner May 14 '20 at 11:15
  • @Lundin I'm a beginner... I thought that it may be a quality of life improvement ¯\\_(ツ)_/¯ – Soft Waffle May 14 '20 at 11:15
  • @PaulFilipenco: Your life will be borderline unlivable if you start using `uint`, `ll`, &c. – Bathsheba May 14 '20 at 11:16
  • I think every would-be programmer should be forced to write C on a [Swedish keyboard](https://external-preview.redd.it/ZITY688ZYbK33kFnw2CWOidAhPi0n9j2ZLBZ6ktDQfo.jpg?auto=webp&s=ab1e5ea01f1701e835e7ff985867bf8973a93df3) for a week. And yes, you have to press Alt Gr + 7 to get a `{`, Alt Gr + 8 to get a `[`, Alt Gr + < to get a `|` and so on. Typing `unsigned int` is a breeze. (From what I heard, Danish standard C90 committee delegates were the reason C got trigraphs) – Lundin May 14 '20 at 11:23
  • @Lundin: It's been known for a while that *Something is rotten in the state of Denmark* – chqrlie May 14 '20 at 11:50
  • @Lundin that's true for german keyboards as well, but it honestly doesn't bother me at all. Using an english keyboard on the other hand feels just weird to me ;-) – Felix G May 14 '20 at 14:11
  • @chqrlie Aye, but _A symbol, by any other trigraph alias, will smell as sweet_. – Lundin May 14 '20 at 14:15
  • @Lundin A week is good. I am writing my whole text and code with these key commands the whole time (spanish keyboard), even now. It´s a bit nasty, but what shall one do. I guess I need to find a program which maps the keystrokes to single keys or write one self :-[) – RobertS supports Monica Cellio May 14 '20 at 15:43
  • 1
    `unsigned int` can also be abbreviated by `unsigned`. And this is only little different to `uint`. – RobertS supports Monica Cellio May 14 '20 at 15:52
  • @Lundin: back in the early 80', confronted with the same issue on the French braindead AZERTY layout, before the advent of the AltGr key, I had to use Alt-123 for `{` and corresponding codes for `}`, `|`, `[`, `]`, `^`, `#`, `~`, and the backquote too. C and shell programming were such a pain, it took me less than a week to settle once and for all for the US layout that I have used ever since. Of course trigraphs were not an option then, but would not have been a solution either. Totally useless in the Unix world. – chqrlie May 14 '20 at 20:46