473,386 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 3771
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Andrew Dalke | last post by:
Here's a proposed Q&A for the FAQ based on a couple recent threads. Appropriate comments appreciated X.Y: Why doesn't Python have macros like in Lisp or Scheme? Before answering that, a...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
4
by: Dave | last post by:
Hello all, I hope the context to my problem shown below is adequate but not overwhelming. I tried to keep it minimal. At the line indicated (in comment form) in the source below, the...
7
by: Jim Cook | last post by:
We have a macro which takes various index constants as an argument and offsets into an array. The macro can be an Lvalue or Rvalue. The index is not zero based. I would like a compile time error...
44
by: Simon Morgan | last post by:
Hi, Can somebody please help me grok the offsetof() macro? I've found an explanation on http://www.embedded.com/shared/printableArticle.jhtml?articleID=18312031 but I'm afraid it still...
8
by: sathyashrayan | last post by:
/* ** PLURALTX.C - How to print proper plurals ** public domain - original algorithm by Bob Stout */ #include <stdio.h> #define plural_text(n) &"s"
6
by: Efim | last post by:
Hi all, Due to performance issue, I want to pevent execution of ToString() function in the code like the following: if(reporting_level & DEBUG_LEVEL) log(reporting_level,string.Format("Event of...
1
by: todWulff | last post by:
Good day folks. Let me open with the statement that I am not a C++/C programmer. The environment that I am programming in is ARMbasic, an embedded BASIC targeted toward ARM-based...
6
by: Jon | last post by:
Normally I can search and find answers to things like this but with this one I'm not even sure what to search for and haven't had any luck. Anyway, I'm trying to use a template name as a template...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.