Use macro parameter in string; Find type definition

L

leo.hou

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
 
F

Flash Gordon

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.

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.
 
R

Robert Gamble

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top