C/C++ - Hello World (very simple)-VBForums
Page 1 of 2 12 LastLast
Results 1 to 40 of 43

Thread: C/C++ - Hello World (very simple)

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2003
    Posts
    34

    C/C++ - Hello World (very simple)

    Code:
    #include <iostream.h>
    
    int main()
    {
    
    //This prints "Hello World" and <<endl makes a new line
    cout<<"Hello World"<<endl;
    
    return 0;
    }
    Explaination:

    "#include <iostream.h>" - This is the header for cout/cin, in other words we include iostream.h so we can use the functions cout<<, and cin>>.

    "int main()" - this is where our program actually starts.

    "cout<<"Hello World"<<endl;" - This is what actually prints the text to the screen.

    Just a simple example !
    Last edited by BeholderOf; Oct 31st, 2003 at 04:17 PM.
    a 17 year old kid who has nothing better to do !
    and i never said i was right !!!

  2. #2
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,981
    Use <iostream> and put the using namespace directive at the top, or put std:: in front of each instance from within <iostream>

    return 0 also isn't required.

    Sorry, but this forum is more for code snippets and stuff and not tutorials.

  3. #3
    Hyperactive Member Iron Skull's Avatar
    Join Date
    Aug 2005
    Location
    The Netherlands
    Posts
    325

    Re: C/C++ - Hello World (very simple)

    cout<<"Hello World\n";

    Is better it uses less memory

    506C65617365205261746520506F7374732E2E2E

  4. #4
    Fanatic Member
    Join Date
    Oct 2004
    Posts
    751

    Re: C/C++ - Hello World (very simple)

    Quote Originally Posted by kasracer
    Use <iostream> and put the using namespace directive at the top, or put std:: in front of each instance from within <iostream>

    return 0 also isn't required.

    Sorry, but this forum is more for code snippets and stuff and not tutorials.
    Thats what it is .

    kasracer, every programmer does things differently .
    My Projects: [ Instant Messagener Client/Server ] [ VBPictochat ]

    My Sites:
    [ Datanethost ]
    [ Helpdesk ]

    Remember if my post was helpful then Rate This Post.

  5. #5
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: C/C++ - Hello World (very simple)

    Quote Originally Posted by k1ll3rdr4g0n
    Thats what it is .

    kasracer, every programmer does things differently .
    Maybe, but while including <iostream.h> may work, it isn't right.

    Some compilers may have the STL headers with the .h extension and no namespace for backwards compatibility, but I know for certain that some don't.

    In fact, visual studio 2003 reports:
    main.cpp fatal error C1083: Cannot open include file: 'iostream.h': No such file or directory
    g++ reports:
    from test.cxx:1:
    /usr/include/c++/4.0.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  6. #6
    Fanatic Member
    Join Date
    Oct 2004
    Posts
    751

    Re: C/C++ - Hello World (very simple)

    Quote Originally Posted by sunburnt
    Maybe, but while including <iostream.h> may work, it isn't right.

    Some compilers may have the STL headers with the .h extension and no namespace for backwards compatibility, but I know for certain that some don't.

    In fact, visual studio 2003 reports:


    g++ reports:
    I was referring more to the return 0 than anything else.
    My Projects: [ Instant Messagener Client/Server ] [ VBPictochat ]

    My Sites:
    [ Datanethost ]
    [ Helpdesk ]

    Remember if my post was helpful then Rate This Post.

  7. #7
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: C/C++ - Hello World (very simple)

    Well, in that case I agree with you .
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  8. #8
    Addicted Member
    Join Date
    Jul 2002
    Location
    Toronto, ON Canada
    Posts
    153

    Re: C/C++ - Hello World (very simple)

    The cout won't work in C. This is ONLY C++

  9. #9
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,958

    Re: C/C++ - Hello World (very simple)

    might want to add a

    Code:
    cin.get();
    after the cout , so you can see what on the screen.

  10. #10
    Addicted Member
    Join Date
    Jul 2002
    Location
    Toronto, ON Canada
    Posts
    153

    Re: C/C++ - Hello World (very simple)

    Or just run the .exe from command/terminal like its meant to be done

  11. #11
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    www.vbforums.com
    Posts
    1,137

    Re: C/C++ - Hello World (very simple)

    The helloworld program is basically found in most C++ books for beginners...
    Thread Helpers 101
    I. Don't forget to
    rate my post if it has helped you.
    II. Mark your threads Resolved
    if it has been answered.
    III. Wrap your code snippets with code tags to make it readable.
    ____________________________________________________________________
    CodeBank Entries

  12. #12
    Fanatic Member
    Join Date
    Oct 2004
    Posts
    751

    Re: C/C++ - Hello World (very simple)

    Quote Originally Posted by Jmacp
    might want to add a

    Code:
    cin.get();
    after the cout , so you can see what on the screen.
    Or a:
    Code:
    system("pause");
    If your running windows.
    .
    My Projects: [ Instant Messagener Client/Server ] [ VBPictochat ]

    My Sites:
    [ Datanethost ]
    [ Helpdesk ]

    Remember if my post was helpful then Rate This Post.

  13. #13
    Hyperactive Member Iron Skull's Avatar
    Join Date
    Aug 2005
    Location
    The Netherlands
    Posts
    325

    Re: C/C++ - Hello World (very simple)

    Quote Originally Posted by yitzle
    The cout won't work in C. This is ONLY C++
    ok for you then
    Code:
    printf("Hello World");

    506C65617365205261746520506F7374732E2E2E

  14. #14
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,981

    Re: C/C++ - Hello World (very simple)

    Quote Originally Posted by k1ll3rdr4g0n
    Or a:
    Code:
    system("pause");
    If your running windows.
    pause will work on linux and I believe OS X as well.

    You have to be careful when using 'clear' and 'cls' as Windows uses cls to clear the screen and linux uses clear.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  15. #15
    Addicted Member
    Join Date
    Jul 2002
    Location
    Toronto, ON Canada
    Posts
    153

    Re: C/C++ - Hello World (very simple)

    Currently logged into Linux via SSH.
    I type "pause" and get "pause: Command not found."
    Hmm.
    : man pause
    SYNOPSIS
    #include <unistd.h>
    int pause(void);
    Could be the admin just took out pause...
    : bash
    : pause
    bash: pause: command not found

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: C/C++ - Hello World (very simple)

    No such thing in Linux. Not that it would be terribly hard to write.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  17. #17
    Addicted Member
    Join Date
    Jul 2002
    Location
    Toronto, ON Canada
    Posts
    153

    Re: C/C++ - Hello World (very simple)

    Lemme try writing it!

    VB Code:
    1. #include <unistd.h>
    2. int main (void) { pause(); return 0; }
    Save that as a pause.c and run 'gcc -o pause pause.c'. Then 'chmod 611 pause' and 'mv pause /usr/bin/'
    Now your Linux machine got pause
    Oh. You need root access I think to write to /usr/bin/. So much for that

    Off topic. How did a 'Hello World' thread diverge so much? (Or is that on topic?)

  18. #18
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: C/C++ - Hello World (very simple)

    Sorry, doesn't work. pause() waits for a signal to be sent, and console input doesn't trigger one.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  19. #19
    Addicted Member
    Join Date
    Jul 2002
    Location
    Toronto, ON Canada
    Posts
    153

    Re: C/C++ - Hello World (very simple)

    No? Oh...
    Oh well. Replace the pause() with getch()...
    No. getch is in conio which is not on Linux.
    OK. I give up. All Linux mathods of input seem to require you hit enter...

  20. #20
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: C/C++ - Hello World (very simple)

    I believe you need to output a special escape sequence or perhaps an ioctl() to set a console into immediate mode, where it won't wait for enter being pressed before reporting back data. Then you can use getch().
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  21. #21
    Addicted Member
    Join Date
    Jul 2002
    Location
    Toronto, ON Canada
    Posts
    153

    Re: C/C++ - Hello World (very simple)

    DESCRIPTION
    The getch, wgetch, mvgetch and mvwgetch, routines read a character
    from the window. ... In delay mode, the program waits until the system
    passes text through to the program. Depending on the setting of
    cbreak, this is after one character (cbreak mode), or after the first
    newline (nocbreak mode)...
    I tried using getch but gcc told me wgetch wasn't defined or something.

  22. #22
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: C/C++ - Hello World (very simple)

    Well, those are curses functions. Curses is a special console handling library.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  23. #23
    Addicted Member
    Join Date
    Jul 2002
    Location
    Toronto, ON Canada
    Posts
    153

    Re: C/C++ - Hello World (very simple)

    Yup. getch() is either in curses.h or conio.h - and Linux doesn't have conio.h
    So getch can not be used to write the pause command...

  24. #24
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: C/C++ - Hello World (very simple)

    Ha!
    Code:
    #include <unistd.h>
    #include <termios.h>
    #include <string.h>
    
    int main()
    {
      printf("Press any key to continue.\n");
      struct termios trm;
      memset(&trm, 0, sizeof(struct termios));
      cfmakeraw(&trm);
      struct termios old;
      tcgetattr(STDIN_FILENO, &old);
      tcsetattr(STDIN_FILENO, TCSAFLUSH, &trm);
      char buf;
      read(STDIN_FILENO, &buf, 1);
      tcsetattr(STDIN_FILENO, TCSAFLUSH, &old);
      return 0;
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  25. #25
    Addicted Member
    Join Date
    Jul 2002
    Location
    Toronto, ON Canada
    Posts
    153

    Re: C/C++ - Hello World (very simple)

    Not that I doubted you, but it works fine.
    You bored on Sundays?
    Now I got to look up all those funny include files...

  26. #26
    New Member
    Join Date
    Aug 2006
    Posts
    4

    Re: C/C++ - Hello World (very simple)

    Mine keeps flickering command prompt.

  27. #27
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: C/C++ - Hello World (very simple)

    Quote Originally Posted by yitzle
    The cout won't work in C. This is ONLY C++
    And for C:
    Code:
    //This needs to be included so that 'printf()' can be used
    #include <stdio.h>
    
    int main(void)
    {
        printf("Hello World!\n");
        return 0;
    }

  28. #28
    Lively Member
    Join Date
    Oct 2008
    Location
    I live in Iceland :)
    Posts
    107

    Re: C/C++ - Hello World (very simple)

    Um may i explain a little better?
    and change the code to my normals?
    Code:
    #include <iostream>
    Using namespace std;
    
    int main
    {
    cout << "Hello World" << endl;
    system("PAUSE")
    return 0
    }
    Explanation:
    start with a { // this is needed.
    cout is a short for Console Out, meaning that it will send something out to the console.
    the << is kind of hard to explain, but it needs to be like that, if you want to do a cin ( Console In ) then do a >> - then the "Hello World" is obviously telling the cout to send out Hello World into the console. and then the << again. endl; means it will end the current line. End Line.
    System("PAUSE") will tell the console to PAUSE and keep showing until a button is pressed.
    return 0 means to shut down the console without any errors - return 1 means with errors
    then end with a } // this is also needed.

  29. #29
    Addicted Member
    Join Date
    Jul 2002
    Location
    Toronto, ON Canada
    Posts
    153

    Re: C/C++ - Hello World (very simple)

    Quote Originally Posted by Hlinzi
    Um may i explain a little better?
    and change the code to my normals?
    Code:
    #include <iostream>
    Using namespace std;
    
    int main
    {
    cout << "Hello World" << endl;
    system("PAUSE")
    return 0
    }
    Explanation:
    start with a { // this is needed.
    cout is a short for Console Out, meaning that it will send something out to the console.
    the << is kind of hard to explain, but it needs to be like that, if you want to do a cin ( Console In ) then do a >> - then the "Hello World" is obviously telling the cout to send out Hello World into the console. and then the << again. endl; means it will end the current line. End Line.
    System("PAUSE") will tell the console to PAUSE and keep showing until a button is pressed.
    return 0 means to shut down the console without any errors - return 1 means with errors
    then end with a } // this is also needed.
    You can change it, but you might want to confirm it compiles before posting it.
    Code:
    Using namespace std;
    That has to be lowercase "using"

    Code:
    int main
    You need to define the parameters main gets
    Code:
    system("PAUSE")
    return 0
    }
    You missed some semicolons.
    You missed including whatever library system() is part of.

  30. #30
    Lively Member
    Join Date
    Oct 2008
    Location
    I live in Iceland :)
    Posts
    107

    Re: C/C++ - Hello World (very simple)

    lol sry:P

  31. #31
    Hyperactive Member Maven's Avatar
    Join Date
    Feb 2003
    Location
    Greeneville, TN
    Posts
    321

    Re: C/C++ - Hello World (very simple)

    Quote Originally Posted by Iron Skull
    cout<<"Hello World\n";

    Is better it uses less memory
    The "endl" statement does two different things: first it inserts a newline character "\n" then it flushes the output stream. So in effect it saves you an extra function call.



    The proper way to insert header files is as follows:

    #include <iostream>
    using namespace std;

    Only use "using namespace std;" in code files! In header files use the prefix to identify objects such as: "std::string myvariable". A using namespace in header files is bad practice because it causes that namespace to be used globally. Having a namespace used globally can cause problems with name conflicts when your using multiple libraries and user defined types.
    Education is an admirable thing, but it is well to remember from time to time that nothing that is worth knowing can be taught. - Oscar Wilde

  32. #32
    Addicted Member
    Join Date
    Jul 2002
    Location
    Toronto, ON Canada
    Posts
    153

    Re: C/C++ - Hello World (very simple)

    The \n also causes the buffer to be flushed.

  33. #33
    Hyperactive Member Maven's Avatar
    Join Date
    Feb 2003
    Location
    Greeneville, TN
    Posts
    321

    Re: C/C++ - Hello World (very simple)

    Quote Originally Posted by yitzle
    The \n also causes the buffer to be flushed.
    The '\n' character does not cause a buffer to flush.
    Education is an admirable thing, but it is well to remember from time to time that nothing that is worth knowing can be taught. - Oscar Wilde

  34. #34
    Hyperactive Member Maven's Avatar
    Join Date
    Feb 2003
    Location
    Greeneville, TN
    Posts
    321

    Re: C/C++ - Hello World (very simple)

    Quote Originally Posted by Kasracer
    return 0 also isn't required.
    When the main function is declared integer, the function should return a integer. If you declare the function as void main, the function will not require a return.
    Education is an admirable thing, but it is well to remember from time to time that nothing that is worth knowing can be taught. - Oscar Wilde

  35. #35
    Addicted Member
    Join Date
    Jul 2002
    Location
    Toronto, ON Canada
    Posts
    153

    Re: C/C++ - Hello World (very simple)

    Quote Originally Posted by Maven
    The '\n' character does not cause a buffer to flush.
    Three programs. Two types of behaviors. Can you guess why they behave like they do?
    Code:
    #include <iostream>
    using namespace std;
    int main ()
    {   
        int *j; j = 0;
        cout << "Hello" << endl;
        *j = 15;
        return 0;
    }
    Output:
    Hello
    Segmentation fault
    Code:
    #include <iostream>
    using namespace std;
    int main ()
    {   
        int *j; j = 0;
        cout << "Hello";
        *j = 15;
        return 0;
    }
    Output:
    Segmentation fault
    Why is the output different? (Hint: it has to do with what did - or did not - occur before the seg fault occurred.)

    Code:
    #include <iostream>
    using namespace std;
    int main ()
    {   
        int *j; j = 0;
        cout << "Hello\n";
        *j = 15;
        return 0;
    }
    Guess what the output is...

    Hello
    Segmentation fault
    Maybe the \n does flush buffers after all...

    Quote Originally Posted by Maven
    When the main function is declared integer, the function should return a integer. If you declare the function as void main, the function will not require a return.
    Try compiling this code:
    Code:
    void main ()  
    {
        return;
    }
    The GNU g++ compile says:
    t.cpp:1: error: ‘::main’ must return ‘int’
    t.cpp: In function ‘int main()’:
    t.cpp:3: error: return-statement with no value, in function returning ‘int’
    which indicates that main() may not be defined at void.
    The reason is that the return value of main() is the exit status of the program (assuming no exit() calls). As such, main() must return an int.

    Code:
    int main () {}
    g++ compiles it without warnings just fine. It simply assumes that no return means no error and has the program exit with exit status 0.

  36. #36
    Hyperactive Member Maven's Avatar
    Join Date
    Feb 2003
    Location
    Greeneville, TN
    Posts
    321

    Re: C/C++ - Hello World (very simple)

    Quote Originally Posted by yitzle
    Three programs. Two types of behaviors. Can you guess why they behave like they do?
    Code:
    #include <iostream>
    using namespace std;
    int main ()
    {   
        int *j; j = 0;
        cout << "Hello" << endl;
        *j = 15;
        return 0;
    }
    Output:
    Code:
    #include <iostream>
    using namespace std;
    int main ()
    {   
        int *j; j = 0;
        cout << "Hello";
        *j = 15;
        return 0;
    }
    Output:

    Why is the output different? (Hint: it has to do with what did - or did not - occur before the seg fault occurred.)

    Code:
    #include <iostream>
    using namespace std;
    int main ()
    {   
        int *j; j = 0;
        cout << "Hello\n";
        *j = 15;
        return 0;
    }
    Guess what the output is...



    Maybe the \n does flush buffers after all...


    Try compiling this code:
    Code:
    void main ()  
    {
        return;
    }
    The GNU g++ compile says:

    which indicates that main() may not be defined at void.
    The reason is that the return value of main() is the exit status of the program (assuming no exit() calls). As such, main() must return an int.

    Code:
    int main () {}
    g++ compiles it without warnings just fine. It simply assumes that no return means no error and has the program exit with exit status 0.
    Code:
    void main ()  
    {
        return;
    }
    replace with

    Code:
    void main ()  
    {
    
    }
    Education is an admirable thing, but it is well to remember from time to time that nothing that is worth knowing can be taught. - Oscar Wilde

  37. #37
    Hyperactive Member Maven's Avatar
    Join Date
    Feb 2003
    Location
    Greeneville, TN
    Posts
    321

    Re: C/C++ - Hello World (very simple)

    Quote Originally Posted by yitzle
    Maybe the \n does flush buffers after all..
    The C++ standard is very clear on this, if you need to flush the output stream then you have to call endl or flush. The difference you see is how GCC decided to handle it's own buffers internally.

    For example:

    cout << "test." << endl;
    cout << "test\n";
    system ("pause");

    On some compilers you may see two and on others you may only see 1. But you will always see endl regardless of compiler or system.
    Education is an admirable thing, but it is well to remember from time to time that nothing that is worth knowing can be taught. - Oscar Wilde

  38. #38
    Addicted Member
    Join Date
    Jul 2002
    Location
    Toronto, ON Canada
    Posts
    153

    Re: C/C++ - Hello World (very simple)

    Quote Originally Posted by Maven
    Code:
    void main ()  
    {
        return;
    }
    replace with

    Code:
    void main ()  
    {
    
    }
    The latter gives me:
    t.cpp:1: error: ‘::main’ must return ‘int’

  39. #39
    Hyperactive Member Maven's Avatar
    Join Date
    Feb 2003
    Location
    Greeneville, TN
    Posts
    321

    Re: C/C++ - Hello World (very simple)

    Quote Originally Posted by yitzle
    The latter gives me:
    Must be gcc specific, that will compile on several different compilers. It could be legacy at this point though, was a old method used in teaching. In all honesty, your suppose to always return to the operating system when your application exits. GCC may have decided to go ahead and enforce it.
    Last edited by Maven; Feb 5th, 2009 at 09:36 AM.
    Education is an admirable thing, but it is well to remember from time to time that nothing that is worth knowing can be taught. - Oscar Wilde

  40. #40
    Hyperactive Member Maven's Avatar
    Join Date
    Feb 2003
    Location
    Greeneville, TN
    Posts
    321

    Re: C/C++ - Hello World (very simple)

    Quote Originally Posted by Maven
    Must be gcc specific, that will compile on several different compilers. It could be legacy at this point though, was a old method used in teaching. In all honesty, your suppose to always return to the operating system when your application exits. GCC may have decided to go ahead and enforce it.
    GCC on buffering:
    http://gcc.gnu.org/onlinedocs/libstd...11ch25s02.html
    Education is an admirable thing, but it is well to remember from time to time that nothing that is worth knowing can be taught. - Oscar Wilde

Page 1 of 2 12 LastLast

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.