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

Replace a #define by a method

Dear all,

I am a beginner of C/C++ programmation and I don't understand how the #define command works.

I would like to make this statement

#define MAX2(V1, V2) (V1>V2 ? V1 : V2)

as a C routine in order to use it in other C programs. However, I don't know what type to put for the retourning value.

Could you help me, please?

thanks a lot,

Marcelo
Dec 13 '05 #1
4 1780
Marcelo wrote:
Dear all,

I am a beginner of C/C++ programmation and I don't understand how the
#define command works.

I would like to make this statement

#define MAX2(V1, V2) (V1>V2 ? V1 : V2)

as a C routine in order to use it in other C programs. However, I don't
know what type to put for the retourning value.

Could you help me, please?

thanks a lot,

Marcelo


Hello Marcelo,

#define is a pre-processor directive. Perhaps you want a normal
function, e.g.:
int MAX2(const int& V1, const int& V2)
{
return std::max(V1,V2);
}
with int replaced by the type you want, or perhaps a template?

Regards,
Peter Jansson
Dec 13 '05 #2
On Tue, 13 Dec 2005 22:47:17 +0100 in comp.lang.c++, Marcelo
<ma********@yahoo.com> wrote,
I am a beginner of C/C++ programmation and I don't understand how the #define command works.
Greetings. Since the answers are often different for C vs. C++ you
should make up your mind which one you want to use. In comp.lang.c++
it's C++.

#define works just as if the expanded text were written right into the
source at the point where it is invoked.
I would like to make this statement

#define MAX2(V1, V2) (V1>V2 ? V1 : V2)

as a C routine in order to use it in other C programs. However, I don't know what type to put for the retourning value.


In C++, you should use std::max().
The return type is the same as the argument type.

In C, the #define macro is passably OK and you can use it in multiple
programs by putting it in a header that you include, just as easily as
if it were a function. Beware of issues such as arguments with side
effects.

Dec 13 '05 #3
Marcelo wrote:
Dear all,

I am a beginner of C/C++ programmation and I don't understand how the
#define command works.

I would like to make this statement

#define MAX2(V1, V2) (V1>V2 ? V1 : V2)

as a C routine in order to use it in other C programs. However, I don't
know what type to put for the retourning value.

Could you help me, please?

thanks a lot,

Marcelo

Great ideas and explanations, thanks
Dec 13 '05 #4
Marcelo wrote:
Dear all,

I am a beginner of C/C++ programmation and I don't understand how the
#define command works.

I would like to make this statement

#define MAX2(V1, V2) (V1>V2 ? V1 : V2)

as a C routine in order to use it in other C programs. However, I don't
know what type to put for the retourning value.

Could you help me, please?

thanks a lot,

Marcelo

The STL libraries are based on templates and yes std::max() should fit
the bill. If you're likely to need generic classes and functions that
are based on specific types then I'd recommend you learn more about
template programming.

JB
Dec 14 '05 #5

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

Similar topics

12
by: Barnes | last post by:
Does anyone know of a good way to use the JavaScript string.replace() method in an ASP form? Here is the scenario: I have a form that cannot accept apostrophes. I want to use the replace() so...
2
by: foo | last post by:
I'm creating a debug class called debug_mem_allocation for the purpose of finding memory leaks. I used macro's to replace the new and delete operators. My problem is with trying to replace the...
4
by: Rayer | last post by:
I have got some project src, and find it out that there is no ppl using "const" to define a constance, but using #define still more. Discussing with my friend, he said 1. #define is much easier in...
18
by: james | last post by:
Hi, I am loading a CSV file ( Comma Seperated Value) into a Richtext box. I have a routine that splits the data up when it hits the "," and then copies the results into a listbox. The data also...
8
by: Aussie Rules | last post by:
Hi, I have a string that is built on the fly, and often of different lengths. The string contains a series of numbers with a comma seperating each number. For example ...
3
by: Roger | last post by:
I'm having a little trouble using the Regex.Replace method. I am trying to perform a search and replace based on string pattern. I've been successful in replacing string from it's starting...
11
by: jarod1701 | last post by:
Hi, i'm currently trying to replace an unknown string using regular expressions. For example I have: user_pref("network.proxy.http", "server1") What do I have to do to replace the...
5
by: MoslyChang | last post by:
Hi, All When I look at effective c++,item2 and item3. I have some basic questions , Does anyone be familar with this topic? it suggests const is perfer to #define, then I think how to replace...
15
by: gjhames | last post by:
I wish to replace several characters in my string to only one. Example, "-", "." and "/" to nothing "" I did like that: my_string = my_string.replace("-", "").replace(".", "").replace("/",...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.