How do I determine the underlying type?

Discussion in 'C Programming' started by Bob Williamson, Jun 27, 2013.

  1. Hi. How can I determine the underlying type of typedefs? For example the
    stat structure has various fields in it. Suppose I want to know what dev_t
    is, where can I find it? If I look in stat.h it's all gobbledygook to
    me. Ideally I would like a tool to be able to tell me what the type is for
    any typedef'd name. Is there anything like that?

    Thanks,
    Bob
    Bob Williamson, Jun 27, 2013
    #1
    1. Advertising

  2. In article <kqhpbo$lha$>,
    Bob Williamson <> wrote:
    >Hi. How can I determine the underlying type of typedefs? For example the
    >stat structure has various fields in it. Suppose I want to know what dev_t
    >is, where can I find it? If I look in stat.h it's all gobbledygook to
    >me. Ideally I would like a tool to be able to tell me what the type is for
    >any typedef'd name. Is there anything like that?
    >
    >Thanks,
    >Bob


    There's obviously no "standard C" way to do this, so this is all obviously
    off-topic. Or, in the words of the members of this group:

    >Off topic. Not portable. Cant discuss it here. Blah, blah, blah.
    >(see sig for useful links)


    But, if what you're really asking is "Can I dump out a struct's layout with
    typedefs expanded?", and you are running a Debian-like Linux system (a
    reasonable assumption, though not 100%, of course), then I think you might
    like to look for a file called something like: dwarves_1.10-2_*.deb

    Somewhere in there (in the "dwarves" utils) is a tool, whose name escapes
    me at the moment, since it's been a while since I've played with it, that
    does a very nice job on this task. I have faith you will be able to fill
    in the missing details in this description.

    --
    Useful clc-related links:

    http://en.wikipedia.org/wiki/Aspergers
    http://en.wikipedia.org/wiki/Clique
    http://en.wikipedia.org/wiki/C_programming_language
    Kenny McCormack, Jun 27, 2013
    #2
    1. Advertising

  3. On Thursday, June 27, 2013 5:30:48 PM UTC+1, Bob Williamson wrote:
    > Hi. How can I determine the underlying type of typedefs? For example the
    > stat structure has various fields in it. Suppose I want to know what dev_t
    > is, where can I find it? If I look in stat.h it's all gobbledygook to
    > me. Ideally I would like a tool to be able to tell me what the type is for
    > any typedef'd name. Is there anything like that?
    >

    Standard headers tend to be like that. They define things in strange ways,
    often with compiler-specific preprocessing instructions or tags.
    For a standard function, look up the documentation.

    For a non-standard function, the answer is a bit different. Normally a decent
    IDE will list fields of a structure for you, often by right-clicking or
    hovering.
    Malcolm McLean, Jun 27, 2013
    #3
  4. Bob Williamson

    James Kuyper Guest

    On 06/27/2013 12:30 PM, Bob Williamson wrote:
    > Hi. How can I determine the underlying type of typedefs? For example the
    > stat structure has various fields in it. Suppose I want to know what dev_t
    > is, where can I find it? If I look in stat.h it's all gobbledygook to
    > me. Ideally I would like a tool to be able to tell me what the type is for
    > any typedef'd name. Is there anything like that?


    The C standard provides no such tool. However, I've used two different
    debuggers which support the 'whatis' command, which will explain to you
    the type of a specified expression.
    James Kuyper, Jun 27, 2013
    #4
  5. Bob Williamson

    Ike Naar Guest

    On 2013-06-27, Bob Williamson <> wrote:
    > Hi. How can I determine the underlying type of typedefs? For example the
    > stat structure has various fields in it. Suppose I want to know what dev_t
    > is, where can I find it? If I look in stat.h it's all gobbledygook to
    > me. Ideally I would like a tool to be able to tell me what the type is for
    > any typedef'd name. Is there anything like that?


    One can use the C preprocessor to do some of the dirty work.
    Here's what I did to find the underlying type of dev_t on
    the machine I'm currently using:

    1) Created a one-line C file that includes the header for 'stat':

    /* begin a.c */
    #include <sys/stat.h>
    /* end a.c */

    2) ran this file through the C preprocessor, and searched for
    the word 'dev_t':

    CC -E a.c | grep -w dev_t

    which produced the following output:

    typedef __uint64_t dev_t;
    dev_t st_dev;
    dev_t st_rdev;
    int mknod(const char *, __mode_t, dev_t) __asm("__mknod50");

    The answer is on the first line of output.
    Ike Naar, Jun 27, 2013
    #5
  6. Bob Williamson

    Ike Naar Guest

    On 2013-06-27, Ike Naar <> wrote:
    > 2) ran this file through the C preprocessor, and searched for
    > the word 'dev_t':
    >
    > CC -E a.c | grep -w dev_t


    Sorry, an editing error appears on the line above, 'CC' should be 'cc'.
    Ike Naar, Jun 27, 2013
    #6
  7. On 2013-06-27, Ike Naar <> wrote:
    > On 2013-06-27, Bob Williamson <> wrote:
    >> Hi. How can I determine the underlying type of typedefs? For example the
    >> stat structure has various fields in it. Suppose I want to know what dev_t
    >> is, where can I find it? If I look in stat.h it's all gobbledygook to
    >> me. Ideally I would like a tool to be able to tell me what the type is for
    >> any typedef'd name. Is there anything like that?

    >
    > One can use the C preprocessor to do some of the dirty work.
    > Here's what I did to find the underlying type of dev_t on
    > the machine I'm currently using:
    >
    > 1) Created a one-line C file that includes the header for 'stat':
    >
    > /* begin a.c */
    > #include <sys/stat.h>
    > /* end a.c */
    >
    > 2) ran this file through the C preprocessor, and searched for
    > the word 'dev_t':
    >
    > CC -E a.c | grep -w dev_t
    >
    > which produced the following output:
    >
    > typedef __uint64_t dev_t;
    > dev_t st_dev;
    > dev_t st_rdev;
    > int mknod(const char *, __mode_t, dev_t) __asm("__mknod50");
    >
    > The answer is on the first line of output.


    Ike thank you very much for taking the time to answer my question. On my
    Linux using gcc it took a bit longer but I am getting closer with your help.

    bobw@home/tmp$ cat a.c
    #include <sys/stat.h>

    bobw@home/tmp$ gcc -E a.c | grep -w dev_t
    typedef __dev_t dev_t;

    bobw@home/tmp$ gcc -E a.c | grep -w __dev_t
    __extension__ typedef __u_quad_t __dev_t;
    typedef __dev_t dev_t;
    __dev_t st_dev;
    __dev_t st_rdev;
    extern int mknod (__const char *__path, __mode_t __mode, __dev_t __dev)
    __dev_t __dev) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (2)));
    __dev_t *__dev) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (2, 4)));
    __mode_t __mode, __dev_t *__dev)

    bobw@home/tmp$ gcc -E a.c | grep -w __u_quad_t
    __extension__ typedef unsigned long long int __u_quad_t;
    __extension__ typedef __u_quad_t __dev_t;
    __extension__ typedef __u_quad_t __ino64_t;
    __extension__ typedef __u_quad_t __rlim64_t;
    __extension__ typedef __u_quad_t __fsblkcnt64_t;
    __extension__ typedef __u_quad_t __fsfilcnt64_t;
    bobw@home/tmp$

    I am not sure how to interpret this output. Is dev_t unsigned long or long int?

    Thanks,
    Bob
    Bob Williamson, Jun 27, 2013
    #7
  8. Bob Williamson

    James Kuyper Guest

    On 06/27/2013 02:39 PM, Bob Williamson wrote:
    ....
    > bobw@home/tmp$ cat a.c
    > #include <sys/stat.h>
    >
    > bobw@home/tmp$ gcc -E a.c | grep -w dev_t
    > typedef __dev_t dev_t;
    >
    > bobw@home/tmp$ gcc -E a.c | grep -w __dev_t
    > __extension__ typedef __u_quad_t __dev_t;

    ....
    >
    > bobw@home/tmp$ gcc -E a.c | grep -w __u_quad_t
    > __extension__ typedef unsigned long long int __u_quad_t;

    ....
    >
    > I am not sure how to interpret this output. Is dev_t unsigned long or long int?


    dev_t is a typedef for __dev_t. __dev_t is a typedef for __u_quad_t.
    __u_quad_t is a typedef for unsigned long long int. "long long" is a
    type specifier that was introduced in C99, 14 years ago. It's guaranteed
    to be at least 64 bits in size.
    James Kuyper, Jun 27, 2013
    #8
  9. On 2013-06-27, James Kuyper <> wrote:
    > On 06/27/2013 02:39 PM, Bob Williamson wrote:
    > ...
    >> bobw@home/tmp$ cat a.c
    >> #include <sys/stat.h>
    >>
    >> bobw@home/tmp$ gcc -E a.c | grep -w dev_t
    >> typedef __dev_t dev_t;
    >>
    >> bobw@home/tmp$ gcc -E a.c | grep -w __dev_t
    >> __extension__ typedef __u_quad_t __dev_t;

    > ...
    >>
    >> bobw@home/tmp$ gcc -E a.c | grep -w __u_quad_t
    >> __extension__ typedef unsigned long long int __u_quad_t;

    > ...
    >>
    >> I am not sure how to interpret this output. Is dev_t unsigned long or long int?

    >
    > dev_t is a typedef for __dev_t. __dev_t is a typedef for __u_quad_t.
    > __u_quad_t is a typedef for unsigned long long int. "long long" is a
    > type specifier that was introduced in C99, 14 years ago. It's guaranteed
    > to be at least 64 bits in size.
    >


    James thank you for clarifying this for me. I am not a C programmer so I did
    not know that. I want to understand what gcc is doing on Linux. Ike's idea
    and your explanation here helped quite a bit. Thanks for mentioning the
    debugger angle earlier but something static like this helps me a lot more.

    Thanks,
    Bob
    Bob Williamson, Jun 27, 2013
    #9
  10. On 27-Jun-13 13:39, Bob Williamson wrote:
    > Ike thank you very much for taking the time to answer my question.
    > On my Linux using gcc it took a bit longer but I am getting closer
    > with your help.
    >
    > ...
    > typedef __dev_t dev_t;
    > ...
    > __extension__ typedef __u_quad_t __dev_t;
    > ...
    > __extension__ typedef unsigned long long int __u_quad_t;
    > ...
    >
    > I am not sure how to interpret this output. Is dev_t unsigned long
    > or long int?


    Neither; it is "unsigned long long int".

    You may not be familiar with "long long int", which is an integer type
    added in C99 that is (at least) 64-bit; in contrast, a "long int" is (at
    least) 32-bit, and "int" and "short int" are (at least) 16-bit.

    Most modern implementations make "int" larger than required, and some
    also make "long int" larger than required, but you cannot rely on that
    in portable code. While _your_ system may have 64-bit long ints (and
    even 64-bit ints!), another may have only 32-bit long ints (and maybe
    even 16-bit ints!), so if you need a type that is guaranteed to be (at
    least) 64-bit, you should use long long int.

    S

    --
    Stephen Sprunk "God does not play dice." --Albert Einstein
    CCIE #3723 "God is an inveterate gambler, and He throws the
    K5SSS dice at every possible opportunity." --Stephen Hawking
    Stephen Sprunk, Jun 27, 2013
    #10
  11. Bob Williamson

    Ike Naar Guest

    On 2013-06-27, Bob Williamson <> wrote:
    > I am not sure how to interpret this output. Is dev_t unsigned long or long int?


    Just out of curiosity: why do you need to know the underlying type of dev_t?
    Ike Naar, Jun 27, 2013
    #11
  12. Bob Williamson

    Eric Sosman Guest

    On 6/27/2013 3:07 PM, Bob Williamson wrote:
    > On 2013-06-27, James Kuyper <> wrote:
    >> On 06/27/2013 02:39 PM, Bob Williamson wrote:
    >> ...
    >>> bobw@home/tmp$ cat a.c
    >>> #include <sys/stat.h>
    >>>
    >>> bobw@home/tmp$ gcc -E a.c | grep -w dev_t
    >>> typedef __dev_t dev_t;
    >>>
    >>> bobw@home/tmp$ gcc -E a.c | grep -w __dev_t
    >>> __extension__ typedef __u_quad_t __dev_t;

    >> ...
    >>>
    >>> bobw@home/tmp$ gcc -E a.c | grep -w __u_quad_t
    >>> __extension__ typedef unsigned long long int __u_quad_t;

    >> ...
    >>>
    >>> I am not sure how to interpret this output. Is dev_t unsigned long or long int?

    >>
    >> dev_t is a typedef for __dev_t. __dev_t is a typedef for __u_quad_t.
    >> __u_quad_t is a typedef for unsigned long long int. "long long" is a
    >> type specifier that was introduced in C99, 14 years ago. It's guaranteed
    >> to be at least 64 bits in size.
    >>

    >
    > James thank you for clarifying this for me. I am not a C programmer so I did
    > not know that. I want to understand what gcc is doing on Linux. Ike's idea
    > and your explanation here helped quite a bit. Thanks for mentioning the
    > debugger angle earlier but something static like this helps me a lot more.


    Keep in mind that this definition of dev_t is specific to
    the particular implementation you're using. That implementation
    defines dev_t (eventually) as unsigned long long int, but other
    implementations might use something else: unsigned long int, or
    unsigned int, perhaps plain unadorned int, or possibly something
    quite strange indeed. That's the reason for using names like
    `dev_t' in the first place: You don't need to learn the details
    of how every (POSIX-conforming) system on the planet encodes its
    device identifiers. In a sense, dev_t is like a contract or an
    interface: You just utter the magic word `dev_t', and the local
    system's header files declare it as the Right Thing for the local
    customs, matching up with the encodings the library traffics in.

    Some other examples of this sort of thing are size_t (an integer
    of some kind or other that can count any object's bytes), time_t
    (some sort of arithmetic type that can represent times and dates),
    and FILE (some unspecified sort of thing that holds the information
    necessary to read and write data). All of these are likely to resolve
    to different fundamental types on different systems, but as long as
    you use the "abstract" names you'll get the right result on any system.

    So in a sense, the type underlying dev_t is ... dev_t!

    --
    Eric Sosman
    d
    Eric Sosman, Jun 27, 2013
    #12
  13. Ike Naar <> writes:
    > On 2013-06-27, Bob Williamson <> wrote:
    >> Hi. How can I determine the underlying type of typedefs? For example the
    >> stat structure has various fields in it. Suppose I want to know what dev_t
    >> is, where can I find it? If I look in stat.h it's all gobbledygook to
    >> me. Ideally I would like a tool to be able to tell me what the type is for
    >> any typedef'd name. Is there anything like that?

    >
    > One can use the C preprocessor to do some of the dirty work.
    > Here's what I did to find the underlying type of dev_t on
    > the machine I'm currently using:
    >
    > 1) Created a one-line C file that includes the header for 'stat':
    >
    > /* begin a.c */
    > #include <sys/stat.h>
    > /* end a.c */
    >
    > 2) ran this file through the C preprocessor, and searched for
    > the word 'dev_t':
    >
    > CC -E a.c | grep -w dev_t


    Should be cc, as you pointed out in a followup.

    > which produced the following output:
    >
    > typedef __uint64_t dev_t;
    > dev_t st_dev;
    > dev_t st_rdev;
    > int mknod(const char *, __mode_t, dev_t) __asm("__mknod50");
    >
    > The answer is on the first line of output.


    At least for gcc, and assuming a Unix-like sysetm, this is a bit simpler:

    echo '#include <sys/stat.h>' | gcc -E - | grep -w dev_t

    Though the output on my system is less illuminating:

    typedef __dev_t dev_t;

    You might need to iterate to find the actual definition.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 27, 2013
    #13
  14. On 2013-06-27, Ike Naar <> wrote:
    > On 2013-06-27, Bob Williamson <> wrote:
    >> I am not sure how to interpret this output. Is dev_t unsigned long or long int?

    >
    > Just out of curiosity: why do you need to know the underlying type of dev_t?


    To use the stat interface from a different language. Thanks to you and
    everyone who answered.

    Bob
    Bob Williamson, Jun 28, 2013
    #14
  15. Bob Williamson

    BartC Guest

    "Bob Williamson" <> wrote in message
    news:kqjged$pqa$...
    > On 2013-06-27, Ike Naar <> wrote:
    >> On 2013-06-27, Bob Williamson <> wrote:
    >>> I am not sure how to interpret this output. Is dev_t unsigned long or
    >>> long int?

    >>
    >> Just out of curiosity: why do you need to know the underlying type of
    >> dev_t?

    >
    > To use the stat interface from a different language. Thanks to you and
    > everyone who answered.


    sizeof(dev_t) might be sufficient in that case.

    --
    Bartc
    BartC, Jun 28, 2013
    #15
  16. On 2013-06-28, BartC <> wrote:
    >
    >
    > "Bob Williamson" <> wrote in message
    > news:kqjged$pqa$...
    >> On 2013-06-27, Ike Naar <> wrote:
    >>> On 2013-06-27, Bob Williamson <> wrote:
    >>>> I am not sure how to interpret this output. Is dev_t unsigned long or
    >>>> long int?
    >>>
    >>> Just out of curiosity: why do you need to know the underlying type of
    >>> dev_t?

    >>
    >> To use the stat interface from a different language. Thanks to you and
    >> everyone who answered.

    >
    > sizeof(dev_t) might be sufficient in that case.


    Bart thanks for your posting. It wouldn't be sufficient but it helps. We
    would need to know what kind of data the field contains, signed/unsigned
    etc. I'll add this to our list.

    Bob
    >
    Bob Williamson, Jun 28, 2013
    #16
  17. Bob Williamson

    BartC Guest

    "Bob Williamson" <> wrote in message
    news:kqjhv3$u7t$...
    > On 2013-06-28, BartC <> wrote:


    >> "Bob Williamson" <> wrote in message


    >>> To use the stat interface from a different language. Thanks to you and
    >>> everyone who answered.

    >>
    >> sizeof(dev_t) might be sufficient in that case.

    >
    > Bart thanks for your posting. It wouldn't be sufficient but it helps. We
    > would need to know what kind of data the field contains, signed/unsigned
    > etc. I'll add this to our list.


    OK, but my experience of dealing with APIs/etc specified in C is that the
    size of a type is more important than its signedness! (So that you can build
    correctly matching structs in the other language, although padding also
    needs to correspond, push the right number of bytes in function calls, etc).

    And you can't tell, in C, the size of most int types, without looking at the
    specs for an implementation or using sizeof(). I don't know if a macro such
    as issigned() can be created to tell you an int type's signedness.

    --
    Bartc
    BartC, Jun 28, 2013
    #17
  18. "BartC" <> writes:
    > "Bob Williamson" <> wrote in message
    > news:kqjhv3$u7t$...
    >> On 2013-06-28, BartC <> wrote:
    >>> "Bob Williamson" <> wrote in message
    >>>> To use the stat interface from a different language. Thanks to you and
    >>>> everyone who answered.
    >>>
    >>> sizeof(dev_t) might be sufficient in that case.

    >>
    >> Bart thanks for your posting. It wouldn't be sufficient but it helps. We
    >> would need to know what kind of data the field contains, signed/unsigned
    >> etc. I'll add this to our list.

    >
    > OK, but my experience of dealing with APIs/etc specified in C is that the
    > size of a type is more important than its signedness! (So that you can build
    > correctly matching structs in the other language, although padding also
    > needs to correspond, push the right number of bytes in function calls, etc).
    >
    > And you can't tell, in C, the size of most int types, without looking at the
    > specs for an implementation or using sizeof(). I don't know if a macro such
    > as issigned() can be created to tell you an int type's signedness.


    #define IS_SIGNED(type) ((type)-1 < (type)0)
    #define IS_FLOATING(type) ((type)1 / 2 != 0)

    Untested.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 28, 2013
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    391
    Christophe Hivert
    Feb 28, 2005
  2. Rahul T.
    Replies:
    3
    Views:
    8,678
    sundar
    Dec 3, 2004
  3. Gernot Frisch

    how to determine type-type?

    Gernot Frisch, Jan 12, 2005, in forum: C++
    Replies:
    3
    Views:
    391
    Ulrich Achleitner
    Jan 13, 2005
  4. ruksan
    Replies:
    1
    Views:
    435
    Karl Heinz Buchegger
    Mar 8, 2005
  5. Peña, Botp
    Replies:
    1
    Views:
    221
    Robert Klemme
    Jan 24, 2004
Loading...

Share This Page