Your first C++ Program : Hello World!

Discussion in 'Code Snippets and Tutorials' started by Kadam, Dec 16, 2006.

Thread Status:
Not open for further replies.
  1. Kadam

    Kadam Level II

    Joined:
    Dec 15, 2006
    Messages:
    259
    Likes Received:
    0
    Code (Text):
    1. // My First Program!
    2.  
    3. #include <iostream>
    4. using namespace std;
    5.  
    6. int main ()
    7. {
    8.   cout << "Hello World!";
    9.   return 0;
    10. }

    In return when you run your program, you will recieve a nice, greeting hello world, if you want to change it go to line 6 and change hello world to whatever you want it to be.

    You're most probably wondering what // is, // is used for making comments, in the program below you will see the program above modified a little bit. Also note, comments do not effect your program, they're there for reference to what a certain code may do or if you want to know what a program is!

    Code (Text):
    1. // A welcome program for Neofriends!
    2.  
    3. #include <iostream>
    4. using namespace std;
    5.  
    6. int main ()
    7. {
    8.   cout << "Welcomes to Neofriends, Spam and Die >_>";
    9.   return 0;
    10. }
    A little explanation of some of the lines

    // A welcome program for Neofriends!
    This is a comment. All lines beginning with // are known as
    comments and do not have any effect on what the program does . The programmer can use them to include short explanations or observations inside the source code. As it is now, it is just an explanation of what our program is!

    #include <iostream>
    Lines beginning with a number/pound sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indications for the preprocessor. In this case the directive
    Code (Text):
    1. #include <iostream>
    tells the preprocessor to include the iostream standard file. Iostream includes the declarations of the basic standard input-output library in C++, and it is included because its needed later in the program.

    return 0;
    The return statement causes the main function to finish. return may be followed by a return code (in our example is followed by the return code 0). A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its debugging/running. This is the common way of ending a C++ program.
     
  2. Reconmiester

    Reconmiester Level II

    Joined:
    Dec 27, 2006
    Messages:
    268
    Likes Received:
    0
    This sort of explains Actionscript for me... Although I still can't wait for my reserved book on Actionscript to come.
     
  3. Anonymous

    Anonymous Guest

    wow, a LOT of this is like SCAR. I might accually try to learn this :)
     
  4. dikel16

    dikel16 Level II

    Joined:
    Dec 24, 2006
    Messages:
    218
    Likes Received:
    0
    hehe, i prefer C :p
    much easier for me
     
  5. zav75

    zav75 Level I

    Joined:
    Feb 28, 2007
    Messages:
    76
    Likes Received:
    6
    Location:
    Canada, Province of Québec
    @dikel16

    ok... but C++ is just the same thing, you can use C fonctions in C++ programmes because they are compiled by the same compiler. For example, visual studio C/C++ compiler.

    You must be an old school programmmer :p.

    zav75 - careful about gravedigging although I'm sure all the coders will be happy to have your input! - Marlene
     
Thread Status:
Not open for further replies.