View unanswered posts | View active topics It is currently Fri Nov 22, 2019 11:28 am






Reply to topic  [ 4 posts ] 
#if vs if()? 
Author Message
Novice

Joined: Tue Dec 16, 2014 10:25 am
Posts: 81
Post #if vs if()?
Like I said in the title, what is the difference between using #if and if()? Both of them are used but I've noticed that the #if are used more in drivers. Is there a difference or advantage to using one?

_________________
Thanks!


Mon Dec 29, 2014 1:44 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
Post Re: #if vs if()?
#if is a pre-processor macro, it is not part of the C language. The preprocessor would have gotten rid of all the #if statements before the source file is being fed to the compiler. #if is typically used to conditionally enable/disable a section of the code feeding to the compiler. For example, if I have added a bunch of nxtDisplayTextLine statements for debugging purpose into the code, I would not want this code be active for the competition code but I don't want to get rid of them in case I need to debug the code again. I can put that code in the #if section so it can be "disabled" and not compile for competition.


Mon Dec 29, 2014 3:40 am
Profile
Novice

Joined: Tue Dec 16, 2014 10:25 am
Posts: 81
Post Re: #if vs if()?
Ah ok. What other practical uses for #difine statements are there in FTC besides using different parts of the code at competition?

_________________
Thanks!


Tue Dec 30, 2014 7:47 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
Post Re: #if vs if()?
There are many uses of #define. You can define macros that looks like function calls. For example,
Code:
#define abs(n)      (((n) < 0)? -(n): (n))
#define min(a, b)   (((a) < (b))? (a): (b))
#define max(a, b)   ((a) > (b))? (a): (b))

You can also define constants especially if the constants are used in multiple places so that you can just change the define and they all will be changed. They also give you a better description on what the magic numbers are. For example:
Code:
#define SHORTGOAL_HEIGHT    24
#define MIDGOAL_HEIGHT      36
#define TALLGOAL_HEIGHT     48
#define PI                  3.14159265


Wed Dec 31, 2014 1:48 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.