简体   繁体   English

找到正确的c类型

[英]Finding the correct c type

I keep stumbling across new types in C. Most recently, I spent a long time trying to get a size_t variable to accept the output of getline() (misled, perhaps, by this ) only to eventually notice that ssize_t (yes, two s's) is the correct type. 我一直在使用C的新类型绊脚石。最近,我花了很长时间尝试获取size_t变量以接受getline()的输出(可能被this误导了),只是最终注意到ssize_t (是,两个s )是正确的类型。 Is there a list of all the possible variable declarations somewhere? 某处是否存在所有可能的变量声明的列表?

Well, not perhaps as such, but if you know which function to call ( getline() in your case ) then the manual page for that function of course will specify the proper types. 好吧,也许不是这样,但是如果您知道要调用哪个函数(本例中为getline() ),则该函数的手册页当然会指定适当的类型。

There aren't that many in the standard library, at least not which are commonly used. 标准库中没有那么多,至少不是常用的。

In C it is possible to define your own type names using typedef . 在C语言中,可以使用typedef定义自己的类型名称。 Example: 例:

typedef int myowntype;

For this the possible type names depend on which header files you have included. 为此,可能的类型名称取决于您包括的头文件。 Thus you have to consult the documentation for each library you use. 因此,您必须查阅所使用的每个库的文档。

In the standard library there are only a few type names, time_t , size_t for example. 在标准库中,只有几个类型名称,例如time_tsize_t

As noted in the comments it is also possible to declare your own types using struct for example. 如注释中所述,也可以使用struct声明自己的类型。 One can then define a type name for a new type: 然后可以为新类型定义类型名称:

typedef struct {
    int a;
    int b;
} my_type;

defines a new type struct {int a; int b;} 定义一个新的类型struct {int a; int b;} struct {int a; int b;} and defines a new type name my_type for this struct type. struct {int a; int b;}并为此结构类型定义一个新的类型名称my_type The struct by itselves can also define a type name written with the struct in it's name: 它自己的struct也可以定义一个用该结构名称编写的类型名称:

struct my_struct {
    int a;
    int b;
}

defines a new type and a type name struct my_struct that can be used to declare variables: 定义一个新类型和类型名称struct my_struct ,可用于声明变量:

struct my_struct a;

There is no list of all possible types in C, because there are postentially infinitely many of them (or close enough to infinitely many). C中没有所有可能类型的列表,因为它们潜在地无限多(或者足够接近到无限多)。

I suggest you're approaching this from the wrong direction. 我建议您从错误的方向进行操作。 Knowing all possible types wouldn't be very helpful in programming; 知道所有可能的类型在编程中不是很有帮助。 what you need to know is which type to use for a given purpose. 您需要知道的是为给定目的使用哪种类型。

For any function you want to call (including getline ) the first thing you need to do is to read the documentation for that function. 对于要调用的任何函数(包括getline ),您需要做的第一件事是阅读该函数的文档。

There are relatively few types defined by the language. 语言定义的类型相对较少。 There are built-in types like char , int , double , and so forth, and there are a number of types defined by the standard library. 有一些内置类型,如charintdouble等,并且标准库定义了许多类型。 Consult any decent C reference, or the C standard itself. 查阅任何体面的C参考或C标准本身。 The latest draft is N1570 . 最新的草案是N1570 (This is a draft of the 2011 ISO C standard, which is not yet fully implemented.) (这是2011 ISO C标准的草案,尚未完全实施。)

More types are defined by secondary standards. 次级标准定义了更多类型。 POSIX is probably the most notable of these. POSIX可能是其中最著名的。 For example, ssize_t (which is the signed type corresponding to size_t ) is defined by POSIX, not by ISO C. 例如, ssize_t (与size_t对应的有符号类型)是由POSIX定义的,而不是由ISO C定义的。

Be sure to read the documentation to find out just what characteristics are guaranteed for a given type. 请务必阅读文档以找出给定类型可以保证的特征。 For example, size_t is guaranteed to be an unsigned integer type, and time_t is guaranteed to be an arithmetic type (it could be signed, unsigned, or even floating-point). 例如,保证size_t是无符号整数类型,并且保证time_t是算术类型(可以是有符号,无符号甚至是浮点数)。 If you want to write portable code, don't make any assumptions beyond what's guaranteed. 如果您想编写可移植的代码,请不要做任何超出保证范围的假设。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM