By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,924 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,924 IT Pros & Developers. It's quick & easy.

C macro

P: n/a
Hi,
I have an intersting quesiton in my view. I have two write a
macro which tell what is the
type of argument being passed to it
i.e int, short, long, signed , unsigned..etc
like

typedef T unsigned int
value(T)

I have to printf() what kind of value T is...

Regards
Gurminder

Jul 12 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
sunny said:
Hi,
I have an intersting quesiton in my view.
An interesting question? Cool.
I have two write a macro which tell what is the
type of argument being passed to it i.e int, short,
long, signed , unsigned..etc like

typedef T unsigned int
value(T)

I have to printf() what kind of value T is...
Okay, understood. Now, you said you had an interesting question, but I don't
see a question.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 12 '06 #2

P: n/a

sunny wrote:
Hi,
I have an intersting quesiton in my view. I have two write a
macro which tell what is the
type of argument being passed to it
i.e int, short, long, signed , unsigned..etc
like

typedef T unsigned int
value(T)

I have to printf() what kind of value T is...

That's a bit of a challenge. Straight C doesnt have any type-inquiry
function at either compile-time or run-time, so just knowing the type
isnt of much help.

There is the rather barren sizeof() function, which will tell you how
many bytes T occupies, but that isnt very helpful, unless you assume
only integer-like types are going to be passed in. Even then it's
going to be a bit tricky to figure out if T is signed or unsigned.

Perhaps you could retell your question, in a bit more detail.

Jul 12 '06 #3

P: n/a
sunny (in 11**********************@75g2000cwc.googlegroups.c om) said:

| Hi,
| I have an intersting quesiton in my view. I have two
| write a macro which tell what is the
| type of argument being passed to it
| i.e int, short, long, signed , unsigned..etc
| like
|
| typedef T unsigned int
| value(T)
|
| I have to printf() what kind of value T is...

An interesting assignment indeed. When is your solution due?

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Jul 12 '06 #4

P: n/a
On Wed, 12 Jul 2006, sunny wrote:
Hi,
I have an intersting quesiton in my view. I have two write a
macro which tell what is the
type of argument being passed to it
i.e int, short, long, signed , unsigned..etc
like

typedef T unsigned int
value(T)

I have to printf() what kind of value T is...

Regards
Gurminder
If you want to do reflection in C you would need *much more*
than just a simple macro. At the very least, you would need to
create a struct wrapper containing a type field (enum) and a data
field (union of all data types used in your program). You would
then have to wrap all your "reflective" data with this struct.

Tak-Shing
Jul 12 '06 #5

P: n/a
sunny wrote:
Hi,
I have an intersting quesiton in my view. I have two write a
macro which tell what is the
type of argument being passed to it
i.e int, short, long, signed , unsigned..etc
like

typedef T unsigned int
value(T)

I have to printf() what kind of value T is...
I can think of a way to distinguish between signed and unsigned
but distinguishing between int and long I have no idea. I'll go
out on a limp and say that it can't be done.
Tak-Shing Chan wrote:
If you want to do reflection in C you would need *much more*
than just a simple macro. At the very least, you would need to
create a struct wrapper containing a type field (enum) and a data
field (union of all data types used in your program). You would
then have to wrap all your "reflective" data with this struct.
I have no idea what reflection means in this context.

Jul 12 '06 #6

P: n/a
sp****@gmail.com wrote:
Tak-Shing Chan wrote:
> If you want to do reflection in C you would need *much more*
than just a simple macro. At the very least, you would need to
create a struct wrapper containing a type field (enum) and a data
field (union of all data types used in your program). You would
then have to wrap all your "reflective" data with this struct.

I have no idea what reflection means in this context.
Typical in OO languages is reflection:

"More generally, reflection is an activity in computation that allows an
object to have information about the structure and reason about its own
computation. The programming paradigm driven by reflection is called
reflective programming."

<http://en.wikipedia.org/wiki/Reflection_%28computer_science%29>

Compare with introspection: The capability of some languages to
determine the type of objects at runtime.

<http://en.wikipedia.org/wiki/Introspection_%28computer_science%29>

People are simulating reflection in C:
<http://www.google.com/search?hl=en&lr=&q=%22reflection+in+C%22>
Jul 12 '06 #7

P: n/a
In article <11*********************@b28g2000cwb.googlegroups. com>,
<sp****@gmail.comwrote:
>sunny wrote:
>I have to printf() what kind of value T is...
>I can think of a way to distinguish between signed and unsigned
but distinguishing between int and long I have no idea. I'll go
out on a limp and say that it can't be done.
if ( (unsigned T) ULONG_MAX == ULONG_MAX ) ...

Of course if int and long have the same range then it becomes
quite difficult to tell them apart ;-)
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Jul 12 '06 #8

P: n/a
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <11*********************@b28g2000cwb.googlegroups. com>,
<sp****@gmail.comwrote:
>>sunny wrote:
>>I have to printf() what kind of value T is...
>>I can think of a way to distinguish between signed and unsigned
but distinguishing between int and long I have no idea. I'll go
out on a limp and say that it can't be done.

if ( (unsigned T) ULONG_MAX == ULONG_MAX ) ...

Of course if int and long have the same range then it becomes
quite difficult to tell them apart ;-)
You can tell them apart at the cost of a compilation failure. int and
long are compatible for most purposes, but int* and long* are not. If
you have a typedef T that could be either int or long, then of these
two expressions:
(T*)0 == (int*)0
(T*)0 == (long*)0
one will compile and one will not (more precisely one is a constraint
violation requiring a diagnostic).

This could be useful for some kind of pre-compilation configuration
system, but I can't think of any way to use it within a single legal
program.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 12 '06 #9

P: n/a
Ancient_Hacker schrieb:
<snip>
There is the rather barren sizeof() function, which will tell you how
sizeof is an operator.
Example of use without parens:
....
p = malloc(number * sizeof *p);
....

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jul 12 '06 #10

P: n/a

sunny wrote:
Hi,
I have an intersting quesiton in my view. I have two write a
macro which tell what is the
type of argument being passed to it
i.e int, short, long, signed , unsigned..etc
like

typedef T unsigned int
value(T)

I have to printf() what kind of value T is...

Regards
Gurminder
okay,
one hint I got is to declare a variable of that type in
macro.
Also the type is not float.

typedef T unsigned int

value(T)
T x;
:
:
:
printf()

So how to do this, any suggestions please.

Regards
Gurminder

Jul 13 '06 #11

P: n/a

In article <ln************@nuthaus.mib.org>, Keith Thompson <ks***@mib.orgwrites:
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <11*********************@b28g2000cwb.googlegroups. com>,
<sp****@gmail.comwrote:
>sunny wrote:
>I have to printf() what kind of value T is...
Of course if int and long have the same range then it becomes
quite difficult to tell them apart ;-)

You can tell them apart at the cost of a compilation failure. int and
long are compatible for most purposes, but int* and long* are not. If
you have a typedef T that could be either int or long, then of these
two expressions:
(T*)0 == (int*)0
(T*)0 == (long*)0
one will compile and one will not (more precisely one is a constraint
violation requiring a diagnostic).

This could be useful for some kind of pre-compilation configuration
system, but I can't think of any way to use it within a single legal
program.
It could be used to verify that a macro invocation with both type and
identifier as parameters used the correct type, to some extent.

Given that C does not preserve type information in the result of
translation (so it's not available at runtime), nor provide a
mechanism for obtaining an identifier's type during translation (so
it's not available at compile time), the only way to achieve some-
thing like what the OP is asking for is to explicitly supply the type
to the macro.

To reduce error it would then be useful (assuming there's any utility
at all to the exercise) to validate the supplied type.

So:

-----
#include <stdio.h>

#define DO_STRINGIZE(x) #x
#define STRINGIZE(x) DO_STRINGIZE(x)

#define PRINT_TYPE(type,id) \
{ type * t##id = & id; \
puts("Type of " STRINGIZE(id) " is " STRINGIZE(type)); }

int main(void)
{
int i;
long l;
const char *p;

PRINT_TYPE(int, i);
PRINT_TYPE(long, l);
PRINT_TYPE(const char *, p);
return 0;
}
-----

Since this relies on a useful diagnostic from the implementation,
though, it's a bit hit-or-miss.

--
Michael Wojcik mi************@microfocus.com

Reversible CA's are -automorphisms- on shift spaces. It is a notorious
fact in symbolic dynamics that describing such things on a shift of finite
type are -fiendishly- difficult. -- Chris Hillman
Jul 14 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.