Click to See Complete Forum and Search --> : C/C++ - Hello World (very simple)
BeholderOf
Oct 31st, 2003, 05:10 PM
#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
Oct 31st, 2003, 11:59 PM
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
Nov 15th, 2005, 03:54 PM
cout<<"Hello World\n";
Is better it uses less memory ;)
k1ll3rdr4g0n
Nov 17th, 2005, 06:03 PM
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 :p.
sunburnt
Nov 17th, 2005, 07:20 PM
Thats what it is ;).
kasracer, every programmer does things differently :p.
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
Nov 17th, 2005, 07:54 PM
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. :)
sunburnt
Nov 17th, 2005, 08:44 PM
Well, in that case I agree with you ;).
yitzle
Dec 4th, 2005, 05:00 PM
The cout won't work in C. This is ONLY C++
Jmacp
Jan 3rd, 2006, 08:18 PM
might want to add a
cin.get();
after the cout , so you can see what on the screen. :bigyello:
yitzle
Jan 4th, 2006, 01:24 AM
Or just run the .exe from command/terminal like its meant to be done :)
KGComputers
Mar 9th, 2006, 02:35 AM
The helloworld program is basically found in most C++ books for beginners...
:) :) :)
k1ll3rdr4g0n
Apr 9th, 2006, 08:54 PM
might want to add a
cin.get();
after the cout , so you can see what on the screen. :bigyello:
Or a:
system("pause");
If your running windows.
;).
Iron Skull
Jun 24th, 2006, 10:53 AM
The cout won't work in C. This is ONLY C++
ok for you then
printf("Hello World");
Kasracer
Jul 16th, 2006, 02:49 PM
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
Jul 16th, 2006, 02:54 PM
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
Jul 16th, 2006, 04:35 PM
No such thing in Linux. Not that it would be terribly hard to write.
yitzle
Jul 16th, 2006, 05:53 PM
Lemme try writing it!
#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 :)
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?)
CornedBee
Jul 16th, 2006, 05:58 PM
Sorry, doesn't work. pause() waits for a signal to be sent, and console input doesn't trigger one.
yitzle
Jul 16th, 2006, 06:19 PM
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...
CornedBee
Jul 16th, 2006, 06:22 PM
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
Jul 16th, 2006, 06:26 PM
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
Jul 16th, 2006, 06:30 PM
Well, those are curses functions. Curses is a special console handling library.
yitzle
Jul 16th, 2006, 06:35 PM
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
Jul 16th, 2006, 06:45 PM
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
Jul 16th, 2006, 10:46 PM
Not that I doubted you, but it works fine.
You bored on Sundays? :)
Now I got to look up all those funny include files...
Bigg123
Aug 29th, 2006, 03:26 PM
Mine keeps flickering command prompt.
x-ice
Nov 9th, 2006, 10:46 AM
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
Nov 23rd, 2008, 07:41 AM
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
Nov 23rd, 2008, 10:03 AM
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.
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
Nov 23rd, 2008, 02:26 PM
lol sry:P
Maven
Jan 31st, 2009, 01:31 PM
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
Jan 31st, 2009, 08:18 PM
The \n also causes the buffer to be flushed.
Maven
Feb 3rd, 2009, 07:01 PM
The \n also causes the buffer to be flushed.
The '\n' character does not cause a buffer to flush.
Maven
Feb 3rd, 2009, 07:18 PM
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
Feb 3rd, 2009, 10:53 PM
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
Feb 5th, 2009, 09:45 AM
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:
#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.)
#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:
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.
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.
void main ()
{
return;
}
replace with
void main ()
{
}
Maven
Feb 5th, 2009, 10:15 AM
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
Feb 5th, 2009, 10:19 AM
void main ()
{
return;
}
replace with
void main ()
{
}
The latter gives me:
t.cpp:1: error: ‘::main’ must return ‘int’
Maven
Feb 5th, 2009, 10:29 AM
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
Feb 5th, 2009, 11:09 AM
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/libstdc++/manual/bk01pt11ch25s02.html
yitzle
Feb 5th, 2009, 11:16 AM
GCC on buffering:
http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html
Interesting. Thanks for the link.
Maven
Feb 6th, 2009, 12:30 AM
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
Mar 10th, 2009, 01:25 AM
#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 !
This is the basic of c++. Every one know about it...
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.