473,480 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Avoiding #if defined (xxx) typos

4 New Member
I'm wondering how to avoid typos in #ifdef pre-processor directives.

Obviously if I type #ifdef OPTIMIZED, and OPTIMIZED is spelled incorrectly, it will default to NOT optimized. Is there any way to avoid this and get the compiler to throw an error?

I remember seeing some code that looked like:
Expand|Select|Wrap|Line Numbers
  1. #define CORE_ON  1
  2. #define CORE_OFF 0
  3. #if (SOME_DIRECTIVE == CORE_OFF)
  4. ...
  5. #endif
  6.  
But that still seems vulnerable to typo errors... any tricks you guys have used to avoid this?
Mar 21 '11 #1
1 1889
Banfa
9,065 Recognized Expert Moderator Expert
Well this is unlikely to be an error that you miss, a missed type preprocessor symbol is like to cause altered program behaviour that should so up in testing if it doesn't cause an outright compiler error.

However you can take advantage of the fact that an undefined symbol is given the numeric value 0 by the preprocessor to do something like

Expand|Select|Wrap|Line Numbers
  1. #define OPTION1 1
  2. #define OPTION2 2
  3. #define OPTION3 3
  4.  
  5. #define SELECTED_OPTION OPTION3
  6.  
  7. #if (SELECTED_OPTION == OPTION1)
  8. // option 1 code
  9. #elif (SELECTED_OPTION == OPTION2)
  10. // option 2 code
  11. #if (SELECTED_OPTION == OPTION3)
  12. // option 3 code
  13. #else
  14. #error SELECTED_OPTION Invalid
  15. #endif
  16.  
Note that your code doesn't do this because it defines CORE_OFF to 0 so you would not be able to tell the difference between CORE_OFF and any undefined symbol.
Mar 21 '11 #2

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

Similar topics

15
3557
by: Manish Jethani | last post by:
Hi all, Is there a way to detect typos in a Python program, before actually having to run it. Let's say I have a function like this: def server_closed_connection(): session.abost() Here,...
5
1095
by: Siemel Naran | last post by:
Is there any trick to avoid typos in set functions, such as void date::set(int day, int month, int year) { d_day = day; month = month; // oops: should be d_month = month d_year = year; }
3
4622
by: Dave | last post by:
Hello all, Please consider this code: #ifndef FOO_INCLUDED #define FOO_INCLUDED // File: foo.h class foo {
4
10118
by: JackyMove | last post by:
Dear all, I have encount the following problem. I have compiled a library "lib.lib" successfully using a simulator compatable to VC++ on Windows platform. Then I try to build an executatble in...
11
2877
by: icanoop | last post by:
I would like to do this MyClass x; istringstream("XXX") >> x; // Works in VC++ but not GCC instead of MyClass x; istringstream iss("XXX"); iss >> x; // Works in both GCC and VC++
1
5720
by: xendrax | last post by:
I was told on this site eval was bad but its the only way i can work out how to do it.. their example was use var imdesc = document.myform.value not var imdesc = eval("document.myform." +...
7
1999
by: onetitfemme | last post by:
Hi, for some reason Firefox is apparently inserting a new line somehow after some IPA symbols which I have written in their hexadecimal notation. Also, I would like to have two spaces by...
7
1724
by: chaz | last post by:
Hi, * this class is defined in assembly XXXXClsLib : Public class VVDB : inherits DBFuncs ... * Assembly YYYYFwApi has a reference to XXXXClsLib * this class B in YYYYFwApi has an imports stmt...
27
2590
by: Cephalobus_alienus | last post by:
Hello, I know that macros are evil, but I recently came across a problem that I couldn't figure out how to solve with templates. I wanted to create a set of singleton event objects, and wrote...
3
2015
by: joe.user0 | last post by:
Your C program is using library libA which has a structure struS. You need to modify struS but may not touch the library. How can your program redefine the structure struS while keeping the...
0
7033
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
6903
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
7027
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
6861
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...
1
4763
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4468
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...
0
2974
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1291
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 ...
1
557
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.