468,512 Members | 1,423 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Use macro parameter in string; Find type definition

Hi experts,

I am new to linux and all the type definitions are driving me mad. What
is the best way to check a type definition in linux? When I use man
page to check some function definition, I may come across new types.
How do you guys look up their definitions?

I tried to lookup ssize_t, what I do is trace (grep in glibc source
code) to 'typedef __ssize_t ssize_t;', then '__STD_TYPE __SSIZE_T_TYPE
__ssize_t;' then '#define __SSIZE_T_TYPE __SWORD_TYPE' and finally '#
define __SWORD_TYPE int'. So 'int' it is, but that took me about 10
minutes... What is a better way to do so? (any man page, web search,
magic glibc function or bash command that can give me the definition
right away?)

=====
Another question popped up when I was trying to do something like:
#define SIZE_OF_T(T) printf("size = %d\n", sizeof(T))
to make my life a bit easier. How can I use 'T' in the "size ="
formating string? I want to get something like
size of ssize_t = 4
as output. Can anyone help?

Thanks in advance
Best Regards
Leo

Aug 29 '06 #1
2 3506
le*****@gmail.com wrote:
Hi experts,

I am new to linux and all the type definitions are driving me mad. What
is the best way to check a type definition in linux? When I use man
page to check some function definition, I may come across new types.
How do you guys look up their definitions?

I tried to lookup ssize_t, what I do is trace (grep in glibc source
code) to 'typedef __ssize_t ssize_t;', then '__STD_TYPE __SSIZE_T_TYPE
__ssize_t;' then '#define __SSIZE_T_TYPE __SWORD_TYPE' and finally '#
define __SWORD_TYPE int'. So 'int' it is, but that took me about 10
minutes... What is a better way to do so? (any man page, web search,
magic glibc function or bash command that can give me the definition
right away?)
When a type name is actually a macro (or series of macros), you can
simply run the pre-expanded form through the preprocessor (try gcc -E
on linux) or use a macro to print the before and after names (see
below). Defining a new type with typedef is done in the compiler
proper and is much trickier to unroll, I don't know of a better way to
do this than what you are doing but there are probably tools that can
do this.
=====
Another question popped up when I was trying to do something like:
#define SIZE_OF_T(T) printf("size = %d\n", sizeof(T))
to make my life a bit easier. How can I use 'T' in the "size ="
formating string? I want to get something like
size of ssize_t = 4
as output. Can anyone help?
Take a look at the # preprocessor operator which converts a macro
argument into a string literal. The following will work for macro
expanded names, it prints the unexpanded name, the expanded name
(actual type) and the size:

#include <stdio.h>

#define my_type int
#define my_type2 my_type

#define EXPAND(T) #T
#define PRINT_TYPE(T) printf("size of " #T " (" EXPAND(T) ") %d\n",
(int)sizeof(T))

int main (void) {
PRINT_TYPE(my_type);
PRINT_TYPE(my_type2);
return 0;
}

$ gcc -W -Wall -pedantic -std=c99 leo.c -o leo
$ ./leo
size of my_type (int) 4
size of my_type2 (int) 4

Robert Gamble

Aug 29 '06 #2
le*****@gmail.com wrote:
Hi experts,

I am new to linux and all the type definitions are driving me mad. What
is the best way to check a type definition in linux? When I use man
page to check some function definition, I may come across new types.
How do you guys look up their definitions?
As a general rule the type definitions are provided so you don't need to
know what the actual type is. This is done because the types vary from
system to system.
I tried to lookup ssize_t, what I do is trace (grep in glibc source
ssize_t is, I believe, defined by the POSIX standard basically as a
signed equivalent to size_t. size_t is the type for describing how big
objects are and counts of objects and is defined by the C standard and
should be mentioned in any good text book.

<snip>
minutes... What is a better way to do so? (any man page, web search,
magic glibc function or bash command that can give me the definition
right away?)
A decent book on C for the types defined by the C standard, or a (draft)
copy of the C standard for which see http://clc-wiki.net/wiki/c_standard
The POSIX standard or a decent book on *nix programming for the rest.
=====
Another question popped up when I was trying to do something like:
#define SIZE_OF_T(T) printf("size = %d\n", sizeof(T))
to make my life a bit easier. How can I use 'T' in the "size ="
formating string? I want to get something like
size of ssize_t = 4
as output. Can anyone help?
Cast to unsigned in and use the appropriate format string, see the
manpage for printf for the format string for unsigned int. However, I
would say once again that you don't need to know the actual underlying
types if you write your program properly.
--
Flash Gordon
Aug 29 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

25 posts views Thread by Andrew Dalke | last post: by
699 posts views Thread by mike420 | last post: by
7 posts views Thread by Jim Cook | last post: by
44 posts views Thread by Simon Morgan | last post: by
8 posts views Thread by sathyashrayan | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.