-
Oct 31st, 2003, 05:10 PM
#1
Thread Starter
Member
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 05:17 PM.
a 17 year old kid who has nothing better to do !
and i never said i was right !!!
-
Oct 31st, 2003, 11:59 PM
#2
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.
-
Nov 15th, 2005, 03:54 PM
#3
-
Nov 17th, 2005, 06:03 PM
#4
Fanatic Member
Re: C/C++ - Hello World (very simple)
 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 .
-
Nov 17th, 2005, 07:20 PM
#5
Re: C/C++ - Hello World (very simple)
 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.
-
Nov 17th, 2005, 07:54 PM
#6
Fanatic Member
Re: C/C++ - Hello World (very simple)
 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.
-
Nov 17th, 2005, 08:44 PM
#7
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.
-
Dec 4th, 2005, 05:00 PM
#8
Addicted Member
Re: C/C++ - Hello World (very simple)
The cout won't work in C. This is ONLY C++
-
Jan 3rd, 2006, 08:18 PM
#9
Re: C/C++ - Hello World (very simple)
might want to add a
after the cout , so you can see what on the screen.
-
Jan 4th, 2006, 01:24 AM
#10
Addicted Member
Re: C/C++ - Hello World (very simple)
Or just run the .exe from command/terminal like its meant to be done
-
Mar 9th, 2006, 02:35 AM
#11
-
Apr 9th, 2006, 07:54 PM
#12
Fanatic Member
Re: C/C++ - Hello World (very simple)
 Originally Posted by Jmacp
might want to add a
after the cout , so you can see what on the screen. 
Or a:
If your running windows.
.
-
Jun 24th, 2006, 09:53 AM
#13
Hyperactive Member
Re: C/C++ - Hello World (very simple)
 Originally Posted by yitzle
The cout won't work in C. This is ONLY C++
ok for you then
Code:
printf("Hello World");
506C65617365205261746520506F7374732E2E2E
-
Jul 16th, 2006, 01:49 PM
#14
Re: C/C++ - Hello World (very simple)
 Originally Posted by k1ll3rdr4g0n
Or a:
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.
-
Jul 16th, 2006, 01:54 PM
#15
Addicted Member
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
-
Jul 16th, 2006, 03:35 PM
#16
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.
-
Jul 16th, 2006, 04:53 PM
#17
Addicted Member
Re: C/C++ - Hello World (very simple)
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 
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?)
-
Jul 16th, 2006, 04:58 PM
#18
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.
-
Jul 16th, 2006, 05:19 PM
#19
Addicted Member
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...
-
Jul 16th, 2006, 05:22 PM
#20
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.
-
Jul 16th, 2006, 05:26 PM
#21
Addicted Member
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.
-
Jul 16th, 2006, 05:30 PM
#22
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.
-
Jul 16th, 2006, 05:35 PM
#23
Addicted Member
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...
-
Jul 16th, 2006, 05:45 PM
#24
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.
-
Jul 16th, 2006, 09:46 PM
#25
Addicted Member
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...
-
Aug 29th, 2006, 02:26 PM
#26
New Member
Re: C/C++ - Hello World (very simple)
Mine keeps flickering command prompt.
-
Nov 9th, 2006, 10:46 AM
#27
Fanatic Member
Re: C/C++ - Hello World (very simple)
 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;
}
-
Nov 23rd, 2008, 07:41 AM
#28
Lively Member
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.
-
Nov 23rd, 2008, 10:03 AM
#29
Addicted Member
Re: C/C++ - Hello World (very simple)
 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"
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.
-
Nov 23rd, 2008, 02:26 PM
#30
Lively Member
Re: C/C++ - Hello World (very simple)
-
Jan 31st, 2009, 01:31 PM
#31
Hyperactive Member
Re: C/C++ - Hello World (very simple)
 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
-
Jan 31st, 2009, 08:18 PM
#32
Addicted Member
Re: C/C++ - Hello World (very simple)
The \n also causes the buffer to be flushed.
-
Feb 3rd, 2009, 07:01 PM
#33
Hyperactive Member
Re: C/C++ - Hello World (very simple)
 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
-
Feb 3rd, 2009, 07:18 PM
#34
Hyperactive Member
Re: C/C++ - Hello World (very simple)
 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
-
Feb 3rd, 2009, 10:53 PM
#35
Addicted Member
Re: C/C++ - Hello World (very simple)
 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:
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...
 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.
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.
-
Feb 5th, 2009, 09:45 AM
#36
Hyperactive Member
Re: C/C++ - Hello World (very simple)
 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.
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
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
-
Feb 5th, 2009, 10:15 AM
#37
Hyperactive Member
Re: C/C++ - Hello World (very simple)
 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
-
Feb 5th, 2009, 10:19 AM
#38
Addicted Member
Re: C/C++ - Hello World (very simple)
 Originally Posted by Maven
Code:
void main ()
{
return;
}
replace with
The latter gives me:
t.cpp:1: error: ‘::main’ must return ‘int’
-
Feb 5th, 2009, 10:29 AM
#39
Hyperactive Member
Re: C/C++ - Hello World (very simple)
 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 10: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
-
Feb 5th, 2009, 11:09 AM
#40
Hyperactive Member
Re: C/C++ - Hello World (very simple)
 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|