-
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 !
-
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.
-
Re: C/C++ - Hello World (very simple)
cout<<"Hello World\n";
Is better it uses less memory ;)
-
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 :p.
-
Re: C/C++ - Hello World (very simple)
Quote:
Originally Posted by k1ll3rdr4g0n
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:
Quote:
main.cpp fatal error C1083: Cannot open include file: 'iostream.h': No such file or directory
g++ reports:
Quote:
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>.
-
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. :)
-
Re: C/C++ - Hello World (very simple)
Well, in that case I agree with you ;).
-
Re: C/C++ - Hello World (very simple)
The cout won't work in C. This is ONLY C++
-
Re: C/C++ - Hello World (very simple)
might want to add a
after the cout , so you can see what on the screen. :bigyello:
-
Re: C/C++ - Hello World (very simple)
Or just run the .exe from command/terminal like its meant to be done :)
-
Re: C/C++ - Hello World (very simple)
The helloworld program is basically found in most C++ books for beginners...
:) :) :)
-
Re: C/C++ - Hello World (very simple)
Quote:
Originally Posted by Jmacp
might want to add a
after the cout , so you can see what on the screen. :bigyello:
Or a:
If your running windows.
;).
-
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");
-
Re: C/C++ - Hello World (very simple)
Quote:
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.
-
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
-
Re: C/C++ - Hello World (very simple)
No such thing in Linux. Not that it would be terribly hard to write.
-
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?)
-
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.
-
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...
-
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().
-
Re: C/C++ - Hello World (very simple)
Quote:
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.
-
Re: C/C++ - Hello World (very simple)
Well, those are curses functions. Curses is a special console handling library.
-
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...
-
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;
}
-
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...
-
Re: C/C++ - Hello World (very simple)
Mine keeps flickering command prompt.
-
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;
}
-
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.
-
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"
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.
-
Re: C/C++ - Hello World (very simple)
-
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.
-
Re: C/C++ - Hello World (very simple)
The \n also causes the buffer to be flushed.
-
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.
-
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.
-
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:
Quote:
Hello
Segmentation fault
Code:
#include <iostream>
using namespace std;
int main ()
{
int *j; j = 0;
cout << "Hello";
*j = 15;
return 0;
}
Output:
Quote:
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...
Quote:
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:
Quote:
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.
-
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.
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
-
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.
-
Re: C/C++ - Hello World (very simple)
Quote:
Originally Posted by Maven
Code:
void main ()
{
return;
}
replace with
The latter gives me:
Quote:
t.cpp:1: error: ‘::main’ must return ‘int’
-
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.
-
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
-
Re: C/C++ - Hello World (very simple)
Quote:
Originally Posted by Maven
Interesting. Thanks for the link.
-
Re: C/C++ - Hello World (very simple)
Quote:
Originally Posted by yitzle
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.
-
Re: C/C++ - Hello World (very simple)
Quote:
Originally Posted by BeholderOf
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 !
This is the basic of c++. Every one know about it...