Thread: Defining

  1. #1
    Tigerworks
    Guest

    Defining

    Can you define something to be used ANYWHERE in the code?
    For example:

    #define start {
    #define end }

    Then do something along the lines of:

    if (a < b)
    start
    code...
    end

    Would that work? It's probably not a good idea, I'm just curious where you can use defines like that.

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Well, it works for me...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define start {
    #define end }
    
    int main(void)
    start
      printf("hello world\n");
      system("PAUSE");
      return 0;
    end
    Wow... I didn't know it...

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    #include <stdio.h>
    
    typedef int This;
    
    #define makes main()
    #define read return 0;
    #define code {
    #define though }
    #define hard printf("Hi there!\n");
    #define to printf("How are you doing?\n");
    
    This makes
    code
       hard
       to
       read
    though
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Magos, begin's and end's is for the lazy pascal programmers
    It's really ugly...

  5. #5
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    #define is just a text editor. Before the code is compiled, the compiler just runs through and changes anything of that name to the text provided above. You can define anything and use it in the code.

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >#define is just a text editor.

    The #define is used by the preprocessor, which runs before the compiler runs. The preprocessor replaces things defined by #define. There are more such things, they are called preprocessor directives.

    Use such #define's only when necessary. For example for defining a macro or a constant.

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    32
    But if you did, say:
    Code:
    puts("Press any key to start");
    would the "start" in that string be replaced by a {?
    Or does the preprocessor not replace inside strings?

    I know it's not a good idea to define things like that, I was just curious how and where you can use defines.
    Last edited by tigs; 03-09-2003 at 05:24 AM.
    - Tigs

  8. #8
    ! |-| /-\ +3 1337 Yawgmoth's Avatar
    Join Date
    Dec 2002
    Posts
    187
    It doesn't replace stuff inside strings.
    L33t sp3@k sux0rz (uZ it t@k3s 10 m1|\|ut3s 2 tr@nzl@te 1 \/\/0rd & th3n j00 h@\/3 2 g3t p@$t d@ m1zpelli|\|gz, @tr0(i0u$ gr@mm@r @|\|d 1n(0/\/\pr3#3|\|$1bl3 $l@|\|g. 1t p\/\/33nz j00!!

    Speling is my faverit sujekt

    I am a signature virus. Add me to your signature so that I may multiply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Defining const in a class
    By g4j31a5 in forum C++ Programming
    Replies: 5
    Last Post: 11-20-2006, 11:27 AM
  2. Defining matricies
    By rich999 in forum C++ Programming
    Replies: 6
    Last Post: 12-02-2005, 07:59 PM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM