Thread: #define ??

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    6

    Question #define ??

    Please somebody, what #define #ifndef #endif does ?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Okay maybe I can shed some light on this. Usually you will see all 3 of those in header files. When you see # followed by something, those are pre-processor directives. Take for example a simple header file
    Code:
    #ifndef MYFILE_H
    #define MYFILE_H
    
    // Declarations here
    const float PI = 3.1415
    
    // Function prototypes perhaps
    void Foo( void );
    
    #endif
    We'll take this one line at a time. The first line is #ifndef MYFILE_H This tells the compiler if this symbol (MYFILE_H) is NOT defined, then procede. The next line is #define MYFILE_H This says define the symbol MYFILE_H so the compiler will not include the file more than once. At this point you can declare any constants or function prototypes, classes, structs, etc. you want. The very last line is #endif this simply just states it is the end of what you wanted to include. Hope this cleared up a little bit of confusion for you. Also, you can use #define in your programs instead of const. Take the following code for example
    Code:
    #include <iostream>
    using namespace std;
    
    #define MAX_SIZE 100
    
    int main( void )
    {
      char Array[ MAX_SIZE ];
    
      return ( 0 );
    }
    Although this program isn't terribly interesting it does do something. When you use #define you don't put a semicolon after the statement. What this does is actually put 100 wherever the compiler see's MAX_SIZE in your program. It is a direct swap and doesn't take memory like const int MAX_SIZE = 100; would. No type checking is done of #define's so it is generally not as safe. It's up to you which you use, but pretty much everyone in C++ uses const.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    6
    Did I understood right MYFILE_h is like MYFILE.h and then you define it. But why you cant just include header with all those definations?
    Code:
    #include "main.h"
    
    int main()
    {
    
    	fn();
    	return 0;
    
    }
    This is where I use fn() and I define it in main.h

    Code:
    void fn();
    void fn()
    {
    
    	//Bla bla
    }
    Or I just didnt understand anythink??

  4. #4
    Unregistered
    Guest

    Question

    I'm beginner in programming in C/C++, and also I'm not god in English..

    I think it's not a good idea to use real code in headers. In my opinion headers are for declarations classes, variables, constants.. and other things which names I don't know in English

    But I wanted to create my own library with some useful functions(gcd, testing primes, ..) and also something for mouse. I thought it's not very difficult, so I had read some original headers(for example stdio.h). But I have found out that there is no code here, only some __some_function_or_variable_name and another unreadable constructions so the code must be somewhere else.

    So if someone know how to write my own library (and how to use it with my compiler - I have BC++ 3.1 and not-running DJGPP, I don't have enough RAM, only 8MB please write it..

    But after reading some messages on this board I think it's not possible..

    and if you find some mistakes in my English (and you will find certainly please correct them..

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    6
    No, I use headers for function declarations and stuff like that...
    And in source files you can use functions that you have declared in header file. That is like you include time.h and use it's functions include you'r own header and use it's functions.
    And what did you mean with real code

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    Originally posted by Fake
    Did I understood right MYFILE_h is like MYFILE.h and then you define it. But why you cant just include header with all those definations?
    You can, but #defining a unique name for each header you can make sure your compiler doesn't load things twice, which could happen if header 1 loads header 2 and you forgot about it.
    Also, check this out:

    Header1:
    Code:
     #ifndef header1isnowloaded
     #define header1isnowloaded
     struct blabla {somestuff in struct};
     #endif
    Header2:
    Code:
     #ifdef header1isnowloaded
     extern struct blabla Blablastruct1, Blablastruct2;
     void SomeFunkyFunction(struct blabla *blabla_input);
     #endif
     void SomeFunctionThatDoesntRequireABlablaStruct(int a);
    That's how you could use defines. If you now load Header2 without loading Header1 first, you still get the functions and declared stuff outside of the #ifdef block.

    (Okay, that example was a bit weird...)
    Last edited by Boksha; 05-31-2002 at 09:39 AM.

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    6
    Ok now I got it. Thanks a lot !!!!

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    29
    What I mean by 'real code'?

    Let's see:

    // this is file someprog.h

    class SomeC {
    public:
    int x;
    void set(int setvalue);
    }

    // file: someprog.cpp

    int main(void)
    {
    SomeC someclass;
    someclass.set(1);
    return 0;
    }

    And this won't work, because function SomeC::set() has a prototype(in header someprog.h), but its body isn't defined, and I meaned this missing code as real code, for example
    void SomeC::set(int setvalue)
    {
    x = c;
    }

    If you see some original header (e.g. stdio.h), there is no real code, only declarations; and where is the 'real code' ?
    Well, these functions from header(printf, ..) are running good, so somewhere must be the code.. I think it's in some *.lib or *.obj files, but how can I create them?

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    317
    If I understand your question, you create a header file and put your class and function declarations in it. For example we'll say 'myfirstheader.h'. Then you create a source file where you define all of your function and class declarations and name it 'myfirstheader.cpp'. Then in your program when you use the preprocessor directive #define "myfirstheader.h" the compiler will group that with 'myfirstheader.cpp' when it compiles your program. Now as common practice header files are usually shipped uncompiled to give the user access to the protoypes and their associated source files are shipped compiled for space saving reasons, as well as to prevent alterations.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer within a Struct
    By Bladactania in forum C Programming
    Replies: 11
    Last Post: 04-03-2009, 10:20 PM
  2. Why?!?
    By p3rry in forum C Programming
    Replies: 3
    Last Post: 01-08-2009, 12:52 PM
  3. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  4. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  5. float toolbar!
    By c-- in forum Windows Programming
    Replies: 5
    Last Post: 02-04-2003, 09:44 AM