Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Defining file name? (http://www.chiefdelphi.com/forums/showthread.php?t=43542)

Mike 08-02-2006 13:13

Defining file name?
 
I've noticed this in a couple IFI files, and figured it was just some necessary thing for the IFI code, but now it's in Kevin's code and I'm curious.

Why, at the start of most .h files, do we have something similar to
Code:

#ifndef _TRACKING_H
#define _TRACKING_H

I searched all the project files and nothing else uses _TRACKING_H, so why have it?

Chris Hibner 08-02-2006 13:21

Re: Defining file name?
 
This is so that the code is included only once. For example, let's say you have three files: file1.h, file2.h, and file3.c.

file1.h is the following:
Code:

// file1.h
int var1, var2;
char var3, var4;

file2.h is the following:
Code:

// file2.h
#include "file1.h"
int var5;


finally, file3.c is the following:
Code:

// file3.c
#include "file1.h"
#include "file2.h"

// code here

If you did the above, then var1, var2, var3, and var4 would be defined twice, which is a bad thing. Using the #infdef/#define/#endif would keep this from happening.

Bongle 08-02-2006 17:28

Re: Defining file name?
 
Since the question was already answered, here's more info.

You can do other fun things with preprocessor stuff like that.

Example: What if you wanted your code to do something while you were debugging, and something else in your 'production' or complete competition code?

Code:

#define DEBUG

int shoot()
{
#ifdef DEBUG
  printf("shooting!")
#endif
  shootTheBall()
}

Basically, the compiler comes through and evaluates the if statement. If it is true, then it will include what is between #if and #endif. Otherwise, it is as if that code is not even there. It's an improvement over using actual code to block out bits, because it doesn't take any of your memory on the robot, and doesn't take any run-time computation to determine whether or not to run printf("shooting!").

So when you're debugging, you just #define DEBUG. When you want your final code to be faster/smaller, just remove the #define DEBUG line, and all your debug-related things will be removed.


All times are GMT -5. The time now is 21:57.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi