using define to change the syntax keywords,,

This is a discussion on using define to change the syntax keywords,, within the C++ Programming forums, part of the General Programming Boards category; i was just wondering if this is perfectly ok todo Code: #define is if #define r return #include <iostream> using ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    310

    using define to change the syntax keywords,,

    i was just wondering if this is perfectly ok todo
    Code:
    #define is if
    #define r return
    
    #include <iostream>
    using namespace std;
    
    int main()
    {
    int a = 1; 
    int b = 2;
    is(a > b){cout<<"should print to screen"<<endl;
    r 0;
    }
    i just see this way as changing the syntax keywords but not what they do etc..

    and it compiles fine in devcpp

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Only in joke or fun code.

    Using the preprocessor to redefine the language just ends up an unreadable mess for everyone else, and probably even for you later on.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you fix the syntax errors (I doubt it compiles fine in DevCpp, there is a missing closing brace) then it is legal. However, it is a really bad idea.

    One reason is apparent if you attempt to compile it on VC++ 2003. It won't work because iostream includes a file that has an internal implementation function called is. Your #define causes that to be changed to an if because a #define is like a global search and replace for all code after it.

    Basically, it is generally too dangerous to use #define's this way to make it worthwhile.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    310
    yeh i figured it would have some negative afect doing it ,,
    thanks for the replys

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. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  3. whats wrong here
    By sreetvert83 in forum C++ Programming
    Replies: 15
    Last Post: 09-21-2005, 10:05 AM
  4. Help getting multiple keypresses in a DOS compiler
    By Nongan in forum Game Programming
    Replies: 2
    Last Post: 04-01-2005, 09:07 PM
  5. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 06:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21