The C/C++ Preprocessor
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: The C/C++ Preprocessor

  1. #1
    Senior Member
    Join Date
    May 2002
    Posts
    344

    Post The C/C++ Preprocessor

    Well, it defiently took me a long time to write this tutorial...it covers the preprocessor and what it does and how to use it. Also, this tutorial will cover how to make comments in C++ using preprocessor directives. We will learn how to write named macros and parameterized macros and how to make header files and how to use them in your programs. Because i was bored, at the end i included all of the Escape Characters i could think up of and i made a list of books that helped me learn C++ when i was young and that i would recommend. Anyways, download the .zip file and unzip it. Then open up Pre-processor.doc. If you don't have Microsoft Word, just save it as a Rich Text File (.rtf). The tutorial is pretty long, 7 pages to be exact, but i think it is worth reading. Anyways, enjoy guys...i spent a lot of my weekend writing this
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  2. #2
    Senior Member
    Join Date
    Jan 2002
    Posts
    1,207
    Nice tutorial. Anyway I'm going to be a pedantic bastard as usual

    const int PI = 3.141592654;
    How exactly is an int going to contain a number which isn't an integer then? Does your compiler not give this as an error?

    #include <iostream.h>
    Shouldn't we use iostream these days (as numerous other threads have mentioned) ? And std::cout ? Just like Mr. Stroustrup tells us to?

    Cheers

    Slarty

  3. #3
    Senior Member
    Join Date
    May 2002
    Posts
    344
    const int PI = 3.141592654;
    slatry, thanks for pointing that out...lol i meant to type
    Code:
    const double PI = 3.141592654;
    damn that was an embarrassing mistake...well when you type a tutorial that long you gotta make a small errors like that orelse you are too perfect to be human...

    #include <iostream.h>
    Yes, once again you are correct, but i didnt feel like doing it Because i was using the #define directive a lot, i thought it might have confused the reader if my code looked like:
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    using std::cin;
    #define PI 3.141592654
    more lines that i just didnt need (although it might have made my code a little bit more up to date)...both ways work correctly. If you have any questions about the using directives, feel free to PM me slarty...

    by the way----sorry memorY
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  4. #4
    Junior Member
    Join Date
    Oct 2003
    Posts
    6
    Nice tutorial. I have been teaching my nephew the basics of C++ programming. This doc was a valuable read for him. (Although even he pointed out that an integer cannot store decimal places! - obviously some of the basics I have already taught him have sunk in).

    Thanks for spending your time to help others.

    Cheers for now,
    Al76484
    A computer is like a woman - It never works and you shouldn\'t use it unless you have cleaned away the bugs. \"Al76484 - 2003\"

  5. #5
    Senior Member
    Join Date
    Dec 2002
    Posts
    180
    Great tutorial W.Eskimo. I'm learning some good stuff. Thanks. When the next one coming out?

    Freddy
    cybnut

  6. #6
    Senior Member
    Join Date
    May 2002
    Posts
    344
    When the next one coming out?
    well i dont know what to write my next one about...i have already covered pointers and reading/writting to files. Anyways if you have anything that you want to learn, maybe i already know it, just let me know and ill write one up
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  7. #7
    Member
    Join Date
    Jun 2003
    Posts
    86
    you said header files end with the extention of .h but they very well can end with .hpp... otherwise it is a great tutorial...

    you also wrote:

    #define CUBE(x) (x*x*x)

    when i think it should be:

    #define CUBE(x) ((x)*(x)*(x))

    because it would be bad practice. Consider the following:

    #define x 2 + 4
    #define CUBE (x*x*x)

    then your code would look like this after the preproccessor looks at it
    instead of CUBE (x*x*x)
    it would be CUBE (2+4*2+4*2+4)
    the answer you would be looking for is not (2+4*2+4*2+4) but ((2+4)*(2+4)*(2+4))

    if that explanation makes any sense... that is just somthing that annoys me a lot.
    You laugh because im different, i laugh because your all the same.

  8. #8
    Senior Member
    Join Date
    May 2002
    Posts
    344
    Code:
    #include <iostream.h>
    #define CUBE(x) (x*x*x)
    
    int main (void)
    {
    	double x;
    	x=2;
    	cout << x << " cubed ";
    	cout << "is " << CUBE(x);
    }
    you are correct kryptonite but if you look at the example, x is just 2.0 so my there wont be any confusion. However, if the programmer was lame enough to add #define x 2+4 to his code, then he will not get the answer he had expected. In the tutorial i explained the importance of parenthesis. I didnt need to do #define CUBE(x) ((x)*(x)*(x)) because i was just multiplying 2.0 with itself 3 times, but like you said, if i wanted to multiply 2+4 together 3 times, i should have added parenthesis.
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  9. #9
    Senior Member
    Join Date
    Nov 2001
    Posts
    4,786
    Damme White_Eskimo that really is one hell of a tutorial. Very nice work.

    Thanks!
    Bukhari:V3B48N826 “The Prophet said, ‘Isn’t the witness of a woman equal to half of that of a man?’ The women said, ‘Yes.’ He said, ‘This is because of the deficiency of a woman’s mind.’”

  10. #10
    well whatever .. i gess its gonna be a nice one .. am gonna mail to mah home .. yeap thut means me'z workin from me wrkplce.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides