Asked  7 Months ago    Answers:  5   Viewed   58 times

This seems like a simple question, but I can't find it with the Stack Overflow search or Google. What does a type followed by a _t mean? Such as

int_t anInt;

I see it a lot in C code meant to deal closely with hardware—I can't help but think that they're related.

 Answers

31

As Douglas Mayle noted, it basically denotes a type name. Consequently, you would be ill-advised to end variable or function names with '_t' since it could cause some confusion. As well as size_t, the C89 standard defines wchar_t, off_t, ptrdiff_t, and probably some others I've forgotten. The C99 standard defines a lot of extra types, such as uintptr_t, intmax_t, int8_t, uint_least16_t, uint_fast32_t, and so on. These new types are formally defined in <stdint.h> but most often you will use <inttypes.h> which (unusually for standard C headers) includes <stdint.h>. It (<inttypes.h>) also defines macros for use with the printf() and scanf().

As Matt Curtis noted, there is no significance to the compiler in the suffix; it is a human-oriented convention.

However, you should also note that POSIX defines a lot of extra type names ending in '_t', and reserves the suffix for the implementation. That means that if you are working on POSIX-related systems, defining your own type names with the convention is ill-advised. The system I work on has done it (for more than 20 years); we regularly get tripped up by systems defining types with the same name as we define.

Tuesday, June 1, 2021
 
huhushow
answered 7 Months ago
95

The underscore is simply a convention; nothing more. As such, its use is always somewhat different to each person. Here's how I understand them for the two languages in question:

In C++, an underscore usually indicates a private member variable.

In C#, I usually see it used only when defining the underlying private member variable for a public property. Other private member variables would not have an underscore. This usage has largely gone to the wayside with the advent of automatic properties though.

Before:

private string _name;
public string Name
{
    get { return this._name; }
    set { this._name = value; }
}

After:

public string Name { get; set; }
Thursday, June 3, 2021
 
John_BSDthos
answered 7 Months ago
37

It's a typedef to a function type. The intent is to use it for function pointers, but in this case the syntax to use it would be:

int bar(void);

fc_name* foo = bar; /* Note the * */

Update: As mentioned in the comments to Jonathan Leffler's answer, the typedef can be used to declare functions. One use could be for declaring a set of callback functions:

typedef int (callback)(int, void*);

callback onFoo;
callback onBar;
callback onBaz;
callback onQux;
Wednesday, June 9, 2021
 
Pwner
answered 6 Months ago
33

Linus Torvalds - Linux Kernel coding style from Linus Torvalds :

LOCAL variable names should be short, and to the point. If you have some random integer loop counter, it should probably be called "i". Calling it "loop_counter" is non-productive, if there is no chance of it being mis-understood. Similarly, "tmp" can be just about any type of variable that is used to hold a temporary value.

If you are afraid to mix up your local variable names, you have another problem, which is called the function-growth-hormone-imbalance syndrome.

Thursday, August 26, 2021
 
Naveed S
answered 4 Months ago
87
echo $_{0..10}

The braces are expanded to:

echo $_0 $_1 $_2 $_3 $_4 $_5 $_6 $_7 $_8 $_9 $_10

It prints the values of eleven strangely-named variables named _0, _1, _2, and so on. They're not set—which is why you don't see anything—but if they were, you would:

$ _0=zero _1=one _2=two _3=three _4=four _5=five _6=six _7=seven _8=eight _9=nine _10=ten
$ echo $_{0..10}
zero one two three four five six seven eight nine ten

$ echo ''$_{0..10}'x'

Same thing, but now there's an x after each variable name. It's not part of the variable name. It's a separate, literal character x, as if you'd written:

echo ${_0}x ${_1}x ${_2}x ${_3}x ${_4}x ${_5}x ${_6}x ${_7}x ${_8}x ${_9}x ${_10}x

Now the output when the variables have values is:

$ _0=zero _1=one _2=two _3=three _4=four _5=five _6=six _7=seven _8=eight _9=nine _10=ten
$ echo ''$_{0..10}'x'
zerox onex twox threex fourx fivex sixx sevenx eightx ninex tenx

This should be enough to understand the other examples in your question.

It also shows that the linked answer is a poor way to repeat a string. It relies on these variables being unset. Not recommended.

Monday, November 29, 2021
 
ollien
answered 1 Week ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share