473,221 Members | 2,136 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,221 software developers and data experts.

C macro

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
11 1631
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

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

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

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 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...
2
by: Pete | last post by:
In Access 95/97 I used to be able to create pull down menus (File,Edit ...) from a macro. It seems there used to be some wizard for that. However in Access 2000 it seems you have to build your...
7
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead....
3
by: Alexander Ulyanov | last post by:
Hi all. Is it possible to pass the whole blocks of code (possibly including " and ,) as macro parameters? I want to do something like: MACRO(FOO, "Foo", "return "Foobar";", "foo();...
8
by: lasek | last post by:
Hi...in some posts i've read...something about using macro rather then function...but difference ??. Best regards....
12
by: Laurent Deniau | last post by:
I was playing a bit with the preprocessor of gcc (4.1.1). The following macros expand to: #define A(...) __VA_ARGS__ #define B(x,...) __VA_ARGS__ A() -nothing, *no warning* A(x) -x ...
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
5
by: Bill | last post by:
This database has no forms. I am viewing an Access table in datasheet view. I'd like to execute a macro to execute a function (using "runcode"). In the function, I'll reading data from the record...
0
by: =?Utf-8?B?TGV0emRvXzF0?= | last post by:
I'd like to create a Macro that will sort some raw data, apprx 20k lines, remove some lines based upon a condition in a certain column. Then copy this data into a new spreadsheet and sort the ...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

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.