473,722 Members | 2,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to verify a preprocessor macros defined with no value

3 New Member
How to verify a preprocessor macros defined with no value , and only if defined with a value should the code within that be exectued.

#ifdef ABC
int a;
#endif

int a is available only if a value of ABC is available.

ABC is defined as
#define ABC

Some Old threads related to the same were observed but none gave correct working answers.
Sep 24 '09 #1
8 7079
Tassos Souris
152 New Member
Try this:
Expand|Select|Wrap|Line Numbers
  1. #if defined( ABC ) && ABC == VALUE_YOU_WANT
  2. ...
  3. #endif
  4.  
Sep 24 '09 #2
Banfa
9,065 Recognized Expert Moderator Expert
You should note that if ABC is defined as shown in the OP

#define ABC

the preprocessor automatically assigns it the value 0. That means that you can not differentiate between

#define ABC

and

#define ABC 0
Sep 24 '09 #3
manjuks
72 New Member
Hi All,

#define ABC

When I tried to print this one i got an compilation error

But It got printed if I define like as below
#define ABC 100

Last post says that #define ABC will automatically assign 0 to ABC

Please clarify me

Thanks,
Manjunath
Sep 25 '09 #4
Banfa
9,065 Recognized Expert Moderator Expert
Sorry my mistake it should say that a defined preprocessor symbol that is defined without a value such as

#define ABC

is treated as it it had the value 0 in other pre-processor commands such as

#if ABC

Within the body of the C/C++ code a normal text substitution will occur so ABC would be replace with nothing which might lead to syntax errors such as

printf("%d\n", ABC);

would be preprocessed to

printf("%d\n", );

which is a syntax error.
Sep 25 '09 #5
congot
3 New Member
@Tassos Souris

On using the above method, I received parse error.
Aslo, the value of the macro is not a numeric value. Then is it possible to check like the above??
Sep 25 '09 #6
Banfa
9,065 Recognized Expert Moderator Expert
Err, no the preprocessor is a simple thing and only knows about numeric values.

What is it the value it is defined to?
Sep 25 '09 #7
congot
3 New Member
@Banfa
it is defined as a function:

#define ABC foo()
Sep 25 '09 #8
Banfa
9,065 Recognized Expert Moderator Expert
Your problem is that you have defined a function like macro but you haven't used function like macro syntax. You should be using one of the 2 definitions

#define ABC() foo()

or

#define ABC foo

The first is a function like macro that in fact calls a function (foo). The second is a text replacement of one function name for another.

Both may be used in code like this

ABC();

I think the second one will work better for you because if ABC is defined as

#define ABC

the code becomes

();

which is a syntax error. And if you want the macro to expand to nothing that is easily achieved like this

#define ABC DO_NOTHING
#define DO_NOTHING()

or this

#define ABC()
Sep 25 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

8
5709
by: Bram Stolk | last post by:
Hi there, What could I use to parse CPP macros in Python? I tried the Parnassus Vaults, and python lib docs, but could not find a suitable module. Thanks, Bram
25
9127
by: Sabyasachi Basu | last post by:
While trying to port some stuff from Unix to Windows, I encountered a strange behaviour of function macros with empty arguments. Here is a small snippet which illustrates the problem: #include <iostream> #include <string> using namespace std; #define B(X, Y) Y
205
10660
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
4
1959
by: Jay Wolfe | last post by:
Hi, If I define a macro as follows #define COSTHETA cos(45.0*3.14159256/180.0) I believe that any time COSTHETA appears it is simply replaced by the defined text, which results in the cos(...) being calculated in my program every time. Is there a way to get the preprocessor to calculate the cosine and just replace COSTHETA with its final number? I know I could define a
7
2112
by: Santa Claus | last post by:
I have the following problem: I would like for a piece of code to be compiled only if a certain macro has been defined AND has some specific value. Let me illustrate: #ifdef SYMBOL f() ; #endif will compile the f() line whenever SYMBOL is defined. Thus, if I have
16
4495
by: Trying_Harder | last post by:
Is it possible to redefine a macro with global scope after undefining it in a function? If yes, could someone explain how? /If/ my question above isn't very clear you can refer to the following example. eg cosider 2 files sample.c and sample.h sample.h
13
2132
by: Chris Croughton | last post by:
Is the following code standard-compliant, and if so what should it do? And where in the standard defines the behaviour? #include <stdio.h> #define DEF defined XXX int main(void) { int defined = 2;
3
2019
by: Avin Patel | last post by:
Hi, I have written C# code. And I have used preprocessor / macro in it. I want to get the C# code after processing preprocessor, I have defined durcng compile time. How can I get this code? Is there any csc compiler option, that can produce the code after processing the macros/preprocessors? Thank you,
7
3360
by: Rohit | last post by:
Hi, I am working on a switch module which after reading voltage through a port pin and caterogizing it into three ranges(open,low or high), passes this range to a function switch_status() with parameters value and signal ID. Signal Id is used to get a user configurable parameter inside a configuration file, which depends on the type of switch. I have implemented it as under. Please ignore those magic numbers as I have mimized logic to...
0
8739
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
9384
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...
0
9238
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9157
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
9088
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
4502
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...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2147
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.