# VBForums CodeBank > CodeBank - C++ >  C/C++ - Hello World (very simple)

## BeholderOf

```
#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 !

----------


## 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.

----------


## Iron Skull

cout<<"Hello World\n";

Is better it uses less memory  :Wink:

----------


## k1ll3rdr4g0n

> 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  :Wink: .

kasracer, every programmer does things differently   :Stick Out Tongue: .

----------


## sunburnt

> 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>.

----------


## k1ll3rdr4g0n

> 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.   :Smilie:

----------


## sunburnt

Well, in that case I agree with you   :Wink: .

----------


## yitzle

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

----------


## Jmacp

might want to add a 



```
cin.get();
```

after the cout , so you can see what on the screen.    :big yellow:

----------


## yitzle

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

----------


## KGComputers

The helloworld program is basically found in most C++ books for beginners...
 :Smilie:   :Smilie:   :Smilie:

----------


## k1ll3rdr4g0n

> might want to add a 
> 
> 
> 
> ```
> cin.get();
> ```
> 
> after the cout , so you can see what on the screen.


Or a:


```
system("pause");
```

If your running windows.
 :Wink: .

----------


## Iron Skull

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


ok for you then 


```
printf("Hello World");
```

----------


## Kasracer

> Or a:
> 
> 
> ```
> 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.

----------


## yitzle

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

----------


## CornedBee

No such thing in Linux. Not that it would be terribly hard to write.

----------


## yitzle

Lemme try writing it!


VB Code:
#include <unistd.h>
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  :Smilie: 
Oh. You need root access I think to write to /usr/bin/. So much for that  :Frown: 

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

----------


## CornedBee

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

----------


## yitzle

No? Oh...  :Frown: 
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...

----------


## CornedBee

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().

----------


## yitzle

> 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.

----------


## CornedBee

Well, those are curses functions. Curses is a special console handling library.

----------


## yitzle

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...

----------


## CornedBee

Ha!


```
#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;
}
```

----------


## yitzle

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

----------


## Bigg123

Mine keeps flickering command prompt.

----------


## x-ice

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


And for C:

```
//This needs to be included so that 'printf()' can be used
#include <stdio.h>

int main(void)
{
    printf("Hello World!\n");
    return 0;
}
```

----------


## Hlinzi

Um may i explain a little better?
and change the code to my normals?


```
#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.

----------


## yitzle

> Um may i explain a little better?
> and change the code to my normals?
> 
> 
> ```
> #include <iostream>
> Using namespace std;
> 
> int main
> ...


You can change it, but you might want to confirm it compiles before posting it.


```
Using namespace std;
```

That has to be lowercase "using"



```
int main
```

You need to define the parameters main gets


```
system("PAUSE")
return 0
}
```

You missed some semicolons.
You missed including whatever library system() is part of.

----------


## Hlinzi

lol sry:P

----------


## Maven

> 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.

----------


## yitzle

The \n also causes the buffer to be flushed.

----------


## Maven

> The \n also causes the buffer to be flushed.


The '\n' character does not cause a buffer to flush.

----------


## Maven

> 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.

----------


## yitzle

> 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?


```
#include <iostream>
using namespace std;
int main ()
{   
    int *j; j = 0;
    cout << "Hello" << endl;
    *j = 15;
    return 0;
}
```

Output: 


> Hello
> Segmentation fault




```
#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.)



```
#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...




> 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:


```
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.



```
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.

----------


## Maven

> Three programs. Two types of behaviors. Can you guess why they behave like they do?
> 
> 
> ```
> #include <iostream>
> using namespace std;
> int main ()
> {   
>     int *j; j = 0;
> ...




```
void main ()  
{
    return;
}
```

replace with



```
void main ()  
{

}
```

----------


## Maven

> 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.

----------


## yitzle

> ```
> void main ()  
> {
>     return;
> }
> ```
> 
> replace with
> 
> ...


The latter gives me:



> t.cpp:1: error: ::main must return int

----------


## Maven

> 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.

----------


## 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

----------


## yitzle

> GCC on buffering:
> http://gcc.gnu.org/onlinedocs/libstd...11ch25s02.html


Interesting. Thanks for the link.

----------


## Maven

> Interesting. Thanks for the link.


For small applications, I do not think that endl or '\n' will matter much. When you begin writing intensive io applications, it will become very important to have tighter control on your buffering. If you was writing a web application with fastcgi, for example, then it becomes critical to control output. While a web application in C++ is far away from novice level, it's a good idea to start developing understanding of buffering.

----------


## deepu8

> ```
> #include <iostream.h>
> 
> int main()
> {
> 
> //This prints "Hello World" and <<endl makes a new line
> cout<<"Hello World"<<endl;
> 
> ...


This is the basic of c++. Every one know about it...

----------

