473,396 Members | 1,853 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,396 software developers and data experts.

variadic arithmetic, boolean operators

(Note: C99 supports variadic macros, but C89 does not.)

I'm pretty sure what I'm trying to do is impossible, but I'll ask here
in case I'm missing something.

I'm trying to define generic, variadic arithmetic and boolean operators.
For example,

product (2, sum (3, 4), 5);

should expand to

(2) * ((3) + (4)) * (5);

Here is my pseudocode:

#define sum(x,y) \
(x) + (y)

#define sum(x, y, ...) \
(x) + sum (y, __VA_ARGS__)

int main (void)
{
sum (1, 2, 3, 4);
return 0;
}

Unfortunately, this has two problems:

- Macros cannot be overriden, hence the base case cannot be caught
generically. (You can define the base case as a function if the
definition (and declaration) precede the macro definition.)

- Macros expansion is not recursive (apparently).

For example, GCC's preprocessor outputs the following:

tmp.c:4:1: warning: "sum" redefined
tmp.c:1:1: warning: this is the location of the previous definition
# 1 "tmp.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "tmp.c"


int main (void)
{
(1) + sum (2, 3, 4);
return 0;
}

Can someone please either confirm that this is impossible in C, or
suggest how to go about it?

--
-trent
Physics is what results when you pollute mathematics with reality.
Nov 14 '05 #1
3 2016
Trent Buck wrote:
(Note: C99 supports variadic macros, but C89 does not.)

I'm pretty sure what I'm trying to do is impossible, but I'll ask here
in case I'm missing something.

I'm trying to define generic, variadic arithmetic and boolean operators.
For example,

product (2, sum (3, 4), 5);

should expand to

(2) * ((3) + (4)) * (5);

Here is my pseudocode:

#define sum(x,y) \
(x) + (y)

#define sum(x, y, ...) \
(x) + sum (y, __VA_ARGS__)

int main (void)
{
sum (1, 2, 3, 4);
return 0;
}

Unfortunately, this has two problems:

- Macros cannot be overriden, hence the base case cannot be caught
generically. (You can define the base case as a function if the
definition (and declaration) precede the macro definition.)

- Macros expansion is not recursive (apparently).


My suggestion is to change your design so that you are passing
containers (lists, vectors, arrays, etc.) to each function. Each
function would return the result. This design would eliminate
the need for variable argument lists.

int sum(int * array_of_integers,
unsigned int num_integers)
{
int result;
unsigned int index;
for (index = 0, result = 0;
index < num_integers;
++index)
{
result += *array_of_integers++;
}
return result;
}
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Nov 14 '05 #2
Up spake Thomas Matthews:
My suggestion is to change your design so that you are passing
containers (lists, vectors, arrays, etc.) to each function. Each
function would return the result. This design would eliminate
the need for variable argument lists.


Not a solution I'm particularly taken with, but better than anything I
had come up with. Thank you.

--
-trent
Turn off your targeting computer, Luke. ASS. AHS. You have a better
chance of making this shot by guessing.
Nov 14 '05 #3
If using gcc, I might say use the ({}) extension:

#define sum(s...) \
({ int args[] = { s }; \
mysum(args, sizeof(args)/sizeof(*args)); \
})

int mysum(int args[], int n)
{
int i,w=0;

for(i=0;i<n;i++) w+= args[i];
return w;
};
For C99, perhaps a make_list macro could be a useful
approach... so you would have

make_list(list, 3,4,5,6)
sum(list);

struct List {
int *arr;
int len;
};

#define make_list(L,...) \
do {
int args[] = { __VA_ARGS__ };
int len = sizeof(args) / sizeof(*args);
L.args = args;
L.len = len;
} while(0)
-Jmh

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: greg | last post by:
Discussion is invited on the following proto-PEP. ------------------------------------------------------------- PEP ??? - Overloadable Boolean Operators...
5
by: Lionel B | last post by:
Greetings, I am trying to implement "element-wise" arithmetic operators for a class along the following lines (this is a simplified example): // ----- BEGIN CODE ----- struct X { int a,b;
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
43
by: Mehta Shailendrakumar | last post by:
Hello, Can anyone suggest me operator to perform arithmetic shift in C? May it be for a perticular compiler. Thank you in advance. Regards, Shailendra
12
by: Raghu | last post by:
Hello all, I need your help on how to redefine teh function of arithmetic operators in C. for example : if there is an equation c = a/b; i want to execute my own algorithm for division...
2
by: pinkfloydhomer | last post by:
Can I use printf inside a variadic function that I am writing? Like: void print(const char * format,...) { va_list va; va_start(va, format); char buffer; // yeah, I know......
335
by: extrudedaluminiu | last post by:
Hi, Is there any group in the manner of the C++ Boost group that works on the evolution of the C language? Or is there any group that performs an equivalent function? Thanks, -vs
16
by: Shawnk | last post by:
I would like to perform various boolean operations on bitmapped (FlagsAttribute) enum types for a state machine design as in; ------------------- enum portState { Unknown, Open,
33
by: Stef Mientki | last post by:
hello, I discovered that boolean evaluation in Python is done "fast" (as soon as the condition is ok, the rest of the expression is ignored). Is this standard behavior or is there a compiler...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.