C macro

S

sunny

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
 
R

Richard Heathfield

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

Morris Dovey

sunny (in (e-mail address removed)) 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?
 
A

Ancient_Hacker

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


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

Tak-Shing Chan

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
 
S

spibou

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

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 said:
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.
 
C

Clever Monkey

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_(computer_science)>

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

<http://en.wikipedia.org/wiki/Introspection_(computer_science)>

People are simulating reflection in C:
<http://www.google.com/search?hl=en&lr=&q="reflection+in+C">
 
W

Walter Roberson

sunny wrote:
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 ;-)
 
K

Keith Thompson

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

Michael Mair

Ancient_Hacker schrieb:
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
 
S

sunny

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

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
 
M

Michael Wojcik

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 (e-mail address removed)

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
 

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
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top