Question about a preprocessor operation

L

ludovicd

Hi to all,

There is my question, Let's say I got a fonction which takes a numeric
argument long enough so that there has to be spaces in it to represent
it correctly. (Ex. 0xf63a002c5ff0338ec becoming -> 0xf63a 002c 5ff0
338ec) is there a way to tell the preprocessor to make:

fct(0xf63a 002c 5ff0 338ec) into ===> fct(0xf63a) f(002c)
(5ff0) (338ec) (exactly as this without the "0x" before the three
lastest forms.



Thanks for your anwsers,


Ludovic
 
B

Ben Pfaff

is there a way to tell the preprocessor to make:

fct(0xf63a 002c 5ff0 338ec) into ===> fct(0xf63a) f(002c)
(5ff0) (338ec) (exactly as this without the "0x" before the three
lastest forms.

No.
 
S

santosh

Hi to all,

There is my question, Let's say I got a fonction which takes a numeric
argument long enough so that there has to be spaces in it to represent
it correctly. (Ex. 0xf63a002c5ff0338ec becoming -> 0xf63a 002c 5ff0
338ec) is there a way to tell the preprocessor to make:

fct(0xf63a 002c 5ff0 338ec) into ===> fct(0xf63a) f(002c)
(5ff0) (338ec) (exactly as this without the "0x" before the three
lastest forms.

I'm not sure, but I think you could partially achieve your objective
with the ## preprocessor operator, but it's rather tricky to use.

If can at all modify the function, I suggest making it recieve the
value in it's string form and convert it within the function. This way,
you can include spaces and whatnot, and prune them out yourself, before
passing the string to the conversion function.

Also the C preprocessor uses uintmax_t type, so _very_ large values,
such as your example above, will be rejected. You'll need to use a
bignum library to manipulate such integers.
 
D

David T. Ashley

There is my question, Let's say I got a fonction which takes a numeric
argument long enough so that there has to be spaces in it to represent
it correctly. (Ex. 0xf63a002c5ff0338ec becoming -> 0xf63a 002c 5ff0
338ec) is there a way to tell the preprocessor to make:

fct(0xf63a 002c 5ff0 338ec) into ===> fct(0xf63a) f(002c)
(5ff0) (338ec) (exactly as this without the "0x" before the three
lastest forms.

I examined Ben Pfaff's reply. Let me give you more perspective.

There are a LOT of things that the C preprocessor won't do. Here are two
examples from microcontroller work:

#1: It is sometimes desirable to calculate a numerator and denominator so
that a given real number can be approximated by a single integer
multipication followed by a single integer division. For an inexpensive
microcontroller, it might be desirable to put into the source code a real
number (3.141592654, for example) and have this used in the code as the
numerator and denominator of the best rational approximation with numerator
and denominator not exceeding 255. (This approximation is 245/78, by the
way, and can be found by a continued fraction algorithm.) The C
preprocessor can't make the mapping from (3.141592654, 255, 255) to
(245,78).

#2: Software timers are often grouped together in microcontroller work for
efficiency (a timer as I'm using it here is a byte which is periodically
decremented, but not below zero). The preprocessor can't "collect and
combine" timer definitions.

There are a lot of approaches to "escape from" the C preprocessor. People
often use scripting languages or generalize the build process to include
source code which is "decorated" with constructs which are filled-in by
another tool which can make sophisticated mappings beyond the capabilities
of the 'C' preprocessor.

You are not the first to notice that the C preprocessor is simplistic.

People usually write their own tools and introduce another phase or two to
compilation.
 
S

santosh

David said:
I examined Ben Pfaff's reply. Let me give you more perspective.

There are a LOT of things that the C preprocessor won't do. Here are two
examples from microcontroller work:
There are a lot of approaches to "escape from" the C preprocessor. People
often use scripting languages or generalize the build process to include
source code which is "decorated" with constructs which are filled-in by
another tool which can make sophisticated mappings beyond the capabilities
of the 'C' preprocessor.

Probably the most common such tool is the m4 generic macro processor,
as it's quite standard and available in a large variety of systems.
 
C

CBFalconer

David T. Ashley said:
.... snip ...

There are a LOT of things that the C preprocessor won't do. Here
are two examples from microcontroller work:

#1: It is sometimes desirable to calculate a numerator and
denominator so that a given real number can be approximated by a
single integer multipication followed by a single integer division.
For an inexpensive microcontroller, it might be desirable to put
into the source code a real number (3.141592654, for example) and
have this used in the code as the numerator and denominator of the
best rational approximation with numerator and denominator not
exceeding 255. (This approximation is 245/78, by the way, and can
be found by a continued fraction algorithm.) The C preprocessor
can't make the mapping from (3.141592654, 255, 255) to (245,78).

No, but you can use:

#define PI 245 / 78
or, better:
#define PI 355 / 113

if you can use values larger than 255. That gives you about 3.5
more significant digits. The parentheses are deliberatly omitted,
so you can get the integer accuracy by using it as:

x = 100 * PI;

With parentheses, you might as well use the Iowa legislature
defined value of 3.

--
Some informative links:
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/> (taming google)
<http://members.fortunecity.com/nnqweb/> (newusers)
 

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
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top