zkurtz
zkurtz

Reputation: 3288

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. Is there a list of all the possible variable declarations somewhere?

Upvotes: 0

Views: 171

Answers (3)

urzeit
urzeit

Reputation: 2909

In C it is possible to define your own type names using 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.

As noted in the comments it is also possible to declare your own types using struct for example. 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;} and defines a new type name my_type for this struct type. The struct by itselves can also define a type name written with the struct in it's name:

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 a;

Upvotes: 2

Keith Thompson
Keith Thompson

Reputation: 263237

There is no list of all possible types in C, because there are postentially infinitely many of them (or close enough to infinitely many).

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.

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. Consult any decent C reference, or the C standard itself. The latest draft is N1570. (This is a draft of the 2011 ISO C standard, which is not yet fully implemented.)

More types are defined by secondary standards. POSIX is probably the most notable of these. For example, ssize_t (which is the signed type corresponding to size_t) is defined by POSIX, not by 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). If you want to write portable code, don't make any assumptions beyond what's guaranteed.

Upvotes: 1

unwind
unwind

Reputation: 399793

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.

There aren't that many in the standard library, at least not which are commonly used.

Upvotes: 3

Related Questions