473,698 Members | 2,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1795
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********@yah oo.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
8157
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 that the apostrophe is automatically replace with two '' . Reason being--SQL Server does not like apostrophes being sent to database. I've tried to do this on the server side in the SQL area of the ASP page by writing a function (with some great...
2
6986
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 delete operator with a macro. I can't replace the delete operator by using void* as the first parameter, because then my code will not be able to modify the calling function's pointer, nor would it get the source file name and line number.
4
2246
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 modify values in program for several propose, and 2. const takes memory but #define not. However, most modern textbook (for example, C++ Primal Plus 4/e) still suggest to use const to replace #define. I know that use #define will have some...
18
4618
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 has some different characters in it that I am trying to remove. The small a with two dots over it and the small y with two dots over it. Here is my code so far to remove the small y: Private Sub Button2_Click(ByVal sender As System.Object, ByVal...
8
1554
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 '2,4,7,8,12,45,67,8,9,33' or it could be '2,6,9' The problem I have is that i am passing this into a stored procedure 'in'
3
1890
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 position to the end of the string value, however, I want to better define an ending position. Below follows the code I'm using: string a = "Hello, World, How are you?"; textBox1.Text = Regex.Replace(a, "W.*", "Mickey");
11
3659
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 "server1" part (which could be
5
3228
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 #define with const. example: 2 header file StringGrid1.h StringGrid2.h
15
1578
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("/", "").replace(")", "").replace("(", "") But I think it's a ugly way. What's the better way to do it?
0
8604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8895
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7728
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.