niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C
C macro
Post new topic   Reply to topic Page 1 of 1 [12 Posts] View previous topic :: View next topic
Author Message
sgurminder@gmail.com
*nix forums beginner


Joined: 18 May 2006
Posts: 3

PostPosted: Wed Jul 12, 2006 6:09 pm    Post subject: C macro Reply with quote

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
Back to top
neutron*star
*nix forums Guru


Joined: 21 Feb 2005
Posts: 2039

PostPosted: Wed Jul 12, 2006 6:28 pm    Post subject: Re: C macro Reply with quote

sunny said:

Quote:
Hi,
I have an intersting quesiton in my view.

An interesting question? Cool.

Quote:
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)
Back to top
Morris Dovey
*nix forums addict


Joined: 22 Jun 2005
Posts: 90

PostPosted: Wed Jul 12, 2006 6:47 pm    Post subject: Re: C macro Reply with quote

sunny (in 1152727780.118600.144940@75g2000cwc.googlegroups.com) 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
Back to top
Ancient_Hacker
*nix forums beginner


Joined: 31 May 2006
Posts: 42

PostPosted: Wed Jul 12, 2006 6:48 pm    Post subject: Re: C macro Reply with quote

sunny wrote:
Quote:
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.
Back to top
Tak-Shing Chan
*nix forums beginner


Joined: 10 Jul 2006
Posts: 19

PostPosted: Wed Jul 12, 2006 6:57 pm    Post subject: Re: C macro Reply with quote

On Wed, 12 Jul 2006, sunny wrote:

Quote:
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
Back to top
spibou@gmail.com
*nix forums Guru Wannabe


Joined: 29 May 2006
Posts: 122

PostPosted: Wed Jul 12, 2006 7:08 pm    Post subject: Re: C macro Reply with quote

sunny wrote:

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

Quote:
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.
Back to top
void * clvrmnky()
*nix forums Guru Wannabe


Joined: 01 Mar 2006
Posts: 152

PostPosted: Wed Jul 12, 2006 7:22 pm    Post subject: Re: C macro Reply with quote

spibou@gmail.com wrote:
Quote:
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>
Back to top
Walter Roberson
*nix forums Guru


Joined: 19 Feb 2005
Posts: 1300

PostPosted: Wed Jul 12, 2006 8:05 pm    Post subject: Re: C macro Reply with quote

In article <1152731323.655956.73250@b28g2000cwb.googlegroups.com>,
<spibou@gmail.com> wrote:
Quote:
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 Wink
--
"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
Back to top
Keith Thompson
*nix forums Guru


Joined: 28 Feb 2005
Posts: 5173

PostPosted: Wed Jul 12, 2006 8:54 pm    Post subject: Re: C macro Reply with quote

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
Quote:
In article <1152731323.655956.73250@b28g2000cwb.googlegroups.com>,
spibou@gmail.com> wrote:
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 Wink

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) kst-u@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.
Back to top
Michael Mair
*nix forums Guru


Joined: 19 Feb 2005
Posts: 963

PostPosted: Wed Jul 12, 2006 9:11 pm    Post subject: Re: C macro Reply with quote

Ancient_Hacker schrieb:
<snip>
Quote:
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.
Back to top
sgurminder@gmail.com
*nix forums beginner


Joined: 18 May 2006
Posts: 3

PostPosted: Thu Jul 13, 2006 7:06 am    Post subject: Re: C macro Reply with quote

sunny wrote:
Quote:
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
Back to top
Michael Wojcik
*nix forums Guru


Joined: 10 Apr 2005
Posts: 336

PostPosted: Fri Jul 14, 2006 7:10 pm    Post subject: Re: C macro Reply with quote

In article <ln7j2ibl2l.fsf@nuthaus.mib.org>, Keith Thompson <kst-u@mib.org> writes:
Quote:
roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <1152731323.655956.73250@b28g2000cwb.googlegroups.com>,
spibou@gmail.com> wrote:
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 michael.wojcik@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
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [12 Posts] View previous topic :: View next topic
The time now is Sun Nov 23, 2008 11:46 am | All times are GMT
navigation Forum index » Programming » C
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Macro expansion in armcc srinu.fsl@gmail.com C 2 Tue Jul 11, 2006 10:37 am
No new posts Variable Number of Arguments in Macro Praveen.Kumar.SP@gmail.co C++ 10 Thu Jun 29, 2006 2:05 pm
No new posts String macro as wchar_t pointer? Heiner C 2 Fri Jun 23, 2006 9:11 pm
No new posts Keyboard macro in Linux Daniel Webb Debian 0 Tue Jun 13, 2006 3:40 am
No new posts [long] Cannot compile my assignment: "(W) Too many argume... jasonspiro4+moznews@gmail AIX 3 Fri Jun 09, 2006 1:20 pm

Acer Monitors | Credit Cards | Advertising | Credit Counseling | Credit Report
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.4039s ][ Queries: 16 (0.2891s) ][ GZIP on - Debug on ]