Thread: C Data Types Naming Convention

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    5

    C Data Types Naming Convention

    All of the following are contemporary "standard" C data types:

    • bool
    • char
    • int32_t
    • float
    • double


    From a naming convention point of view, can you spot the odd man out?

    Yes, only the int32_t has a _t at the end. Why not bool_t, char_t, float_t, etc. Why is the intnn singled out to have the burdensome and unsightly _t at the end?

    Surely int32 is enough. Yes, I know I can place in my code:
    typedef uint32_t uint32
    typedef int32_t int32
    typedef uint8_t uint8
    typedef uint8_t uint8

    and all is well, but why doesn't the C11 standard follow a consistent naming convention?

    [I’ve left 16 bit and 64 bit variants out since I don’t use 16 bit data types and, if I do need a 64 bit data type, I’ll add it]

    What are people's views on using int32 instead of int32_t (damn I hate typing _, it's an awkward hand maneuver!)?

    If I use int32 (instead of int32_t) am I damning future maintainers of my code to frustration/confusion?

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Well, I had a brother-in-law who came to our farm in the 1990's and while my parents were away he said, "Hey, you want to see if we can shoot your mom's tomatoes from the second floor?"
    So he was older and stuff and I said sure, but mom will be ........ed.
    So we got the 22 rifle on the second story and he was shootin' away at those tomatoes. I didn't do any of it.
    She came back and then they left and later she was like, "What happened to all my tomatoes?!" And I was programming on BASIC on my Vic-20 and I was like, "Mom, can't you see I'm racing against that Bill Gates guy?"
    "I'll buy you a whole farm of tomatoes if you would just leave me alone and I can start my mega billion dollar company before anyone like Bill comes along."
    She didn't leave me alone, I never started that company, she never got her tomato farm, and she had all those tomatoes with holes in them.

    Does that help answer your question?

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    45
    char, float, and double are standard types. bool is provided by <stdbool.h> (a typedef for _Bool), and int32_t is provided by <stdint.h> if the C implementation happens to define a signed 32-bit integer type with no padding bits. The _t suffix is simply the convention used by the integer types provided by <stdint.h>.

    There are very few situations in which a fixed-width integer type, like int32_t, is actually needed. In many cases long will work just as well.

    Edit: Sorry, bool is actually a macro that expands to _Bool, rather than a typedef.
    Last edited by zyxwvuts; 12-11-2014 at 08:24 PM.

  4. #4
    Registered User
    Join Date
    Dec 2014
    Posts
    5
    zyxwvuts,

    I know the history and reasons behind the naming convention, but what we have as a consequence is the following inconsistency:

    • bool
    • char
    • int32_t
    • float
    • double


    If we use int32 as the type definintion, we get consistency.

    Cheers, Casa

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    _t tends to be used by the standard library when extending basic types. size_t, wchar_t, ptrdiff_t etc. It's also used by lots of other libraries to define typenames which isn't the greatest idea because you dont want to step on possible future standard library namespace names. Just like you shouldn't name your string functions starting with str*() so you don't step on possible future standard library functions.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    "Burdensome and unsightly" seems a bit too harsh. Nor was it "singled out". The standard defines a number of types that end in _t: size_t, ptrdiff_t, atomic_t, wchar_t to name a few.

    Why this "inconsistency"? No idea really -- maybe it is consistent, we just don't know what meaning the _t suffix has to the C89/C99/C11 committees. A quick search turned up nothing conclusive, but I really didn't try very hard.

    As far as typedef'ing, you can do it, you likely aren't damning your code, though I don't think it really helps in any way either. Especially considering that proper code most likely should be using size_t, and maybe other _t types that don't benefit from typedef'ing away the _t (imagine a type called just size...ugh). But, for the fixed-width integer types, it won't really hurt anything either, unless you're interacting with code that has different definitions of uint8, int32, etc.

    As for personal view/opinion
    Downsides:
    * I generally avoid mere "convenience" typedefs, unless it substantially improves the readability of the code. Complex function pointer definitions might be an example of useful convenience typedefs (I usually use an _f or _fp or similar suffix). Saving me an awkward keystroke or two is never justification for adding even small complexity to my code.
    * While the typedef is a small change, it does add some extra maintenance for you, making sure you typedef all the types correctly (when you add a 64-bit type, e.g.), and include the right header in every .c file.
    * You may use a third-party library that defines the same types to different things, resulting in conflicts between it and your code.
    Conformance to standards, clarity and readability are all extremely important as your project grow in scope, especially when there are multiple people working on a project, and should outweigh decisions that amount to "I'm lazy".

    Upsides:
    * Avoid having to type awkward _t suffix

    Mixed:
    * Looks like other standard types which is nice for consistency...but it's actually not a standard type, so that may actually be A Bad Thing.
    Last edited by anduril462; 12-11-2014 at 08:59 PM.

  7. #7
    Registered User
    Join Date
    Jan 2014
    Posts
    45
    So long as you are consistent when you write your source code (by using either int32 or int32_t, but not both), the type name you use doesn't really matter. (There may, however, be portability issues with using fixed-width integer types.)

  8. #8
    Registered User
    Join Date
    Dec 2014
    Posts
    5
    This is an interesting conversation. Thanks to all who have contributed so far, particularly anduril who went to the effort of Downsides and Upsides.

    I took a look at K&R, and yes, there are types with _t in there (div_t, ldiv_t, ptrdiff_t, size_t, time_t and wchar_t). I also noticed in K&R that the basic types are defined as int, float, char, short, long and double.

    So, it appears that K&R thought it appropriate to name basic types without a _t suffix and "non-basic" types with a suffix. This would sound like a good naming convention to adopt since the more obscure and less frequently used types benefit from the emphasis of the _t suffix.

    However C11 (and C99) has chosen to differentiate the short and long types and introduce the bool type (curiously not as bool_t). I also note that there are float_t and double_t types mentioned, but these are then typedefed into float and double in math.h.

    So, the more I look, the more [u]intnn_t types stand out as not being consistent.

    I'm less worried about a future programmer having problems since it would be easy to do a global find and replace (word only) of the maximum of 8 [u]intnn_t data types.

    Yes, anduril462, I used "burdensome and unsightly" for emphasis.

    Personally I like the improved legibility of "int32 variable" rather than "int32_t variable". But then I'm the sort of person who prefers sysClock to g_ui32SysClock for a variable name.

    Looking to hear more thoughts on the subject.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Casa
    However C11 (and C99) has chosen to differentiate the short and long types and introduce the bool type (curiously not as bool_t).
    The reason for bool instead of bool_t may well be because the type is named as such in C++, hence the standard committee might have C code that can also be compiled as C++ code in mind (e.g., for the benefit of some libraries).

    Quote Originally Posted by Casa
    I also note that there are float_t and double_t types mentioned, but these are then typedefed into float and double in math.h.
    The standard does not require that float_t always be a typedef for float or that double_t always be a typedef for double; what they are depends on FLT_EVAL_METHOD. You're looking at implementation detail and then jumping to conclusions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You expect 100% consistency in a standard designed by committee? Be real. The old saying about a camel being a horse designed by committee applies.

    To answer the question though .... Types with the _t prefix are not guaranteed to be supported by all C implementations and, if they are supported, they are declared in standard header files (for example, as a typedef). Types like char, int, short, etc are guaranteed to be supported by any C implementation (i.e. if the implementation claims compliance with a particular version of the C standard, it supports all the basic types specified in that standard).

    But even that is not 100% consistent.

    The reason for bool being a macro rather than a keyword (as it is in C++) had both political and technical contributors. As did the introduction of wchar_t as a keyword in C++.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Registered User
    Join Date
    Dec 2014
    Posts
    5
    grumpy,

    Just because something was done by a committee, doesn't mean it should be substandard. More importantly it does not mean that things should not be improved over time.

    When we accept mediocrity, it's what we get.

    Cheers,
    Casa

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Casa, you clearly have no understanding of how standardisation committees work, nor interest in understanding.

    My contribution to this thread is therefore over.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  13. #13
    Registered User
    Join Date
    Dec 2014
    Posts
    5
    Glad to hear that.

  14. #14
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by Casa View Post
    Glad to hear that.
    +1 that.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Casa
    Just because something was done by a committee, doesn't mean it should be substandard.
    Agreed. On the other hand, even for programming languages where there is/was a single person with the final say over the language and standard library design choices, inconsistencies crept in. In the end, the development of programming languages and their associated standard libraries is something of an organic process: if you don't have industry players and myriad of other interested parties as the decision makers (as in a committee), you have them influencing the decision makers, whether by pressure via feedback or the existence of a third party library with conventions that have become de facto standard.

    Quote Originally Posted by Casa
    More importantly it does not mean that things should not be improved over time.
    Easy to say; hard to do when you have to cater to a very wide audience. As nonpuz mentioned in post #5, the standard reserves some names. This is good because it means that a future version of the standard can introduce more names without breaking existing code. The bad part is that the very act of reserving names can mean that existing code suddenly becomes problematic, and of course the more rules there are, the harder it is to remember what is reserved and what is not.

    So, int32 might sound like a better idea than int32_t, and maybe it is. But when introducing it, there would be the concern that existing programs use int32, especially if they use them differently from what the standard would state (e.g., as the name of a variable). There might also be consideration that perhaps it was known that some existing libraries declared int32_t in the way that inspired the standard to introduce this, in which case keeping to an existing convention would be better. Furthermore, requiring the _t suffix in order for a name starting with int to be reserved would be a plus to make it less likely for a reserved name to be declared (contrast with say, the restrictions imposed by <ctypes.h> such that isfriendly is reserved).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. data types types...
    By gftmc in forum C++ Programming
    Replies: 3
    Last Post: 09-11-2006, 11:30 AM
  2. What do you think of this C++ naming convention?
    By cnsr in forum C++ Programming
    Replies: 10
    Last Post: 01-07-2004, 08:51 PM
  3. Replies: 2
    Last Post: 01-04-2003, 01:16 AM
  4. Naming variable types
    By PHP in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2002, 06:52 PM
  5. Need help naming data structures
    By AP2 in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2002, 09:36 AM