May 17, 2010 at 9:27pm UTC
I cannot figure out what is the purpose of /n or newline in this example I found in this site:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// defined constants: calculate circumference
#include <iostream>
using namespace std;
#define PI 3.14159
#define NEWLINE '\n'
int main ()
{
double r=5.0; // radius
double circle;
circle = 2 * PI * r;
cout << circle;
cout << NEWLINE;
return 0;
}
The above code has no difference if I remove:
#define NEWLINE '\n' and
cout << NEWLINE;
The result is the same.
Last edited on May 20, 2010 at 12:26am UTC
May 17, 2010 at 9:41pm UTC
The new line character (\n) simply moves the cursor to a new line. Your program above will not demonstrate that. Try this:
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
using namespace std;
int main()
{
cout << "HELLO" ;
cout << "\n" ;
cout << "I AM ON A NEWLINE!!!" ;
cin.get();
return 0;
}
Also, it makes the code much more readable if you use the code tags. You might have done this by accident, but please don't double post.
Last edited on May 17, 2010 at 9:42pm UTC
May 17, 2010 at 9:48pm UTC
Actually, there is a purpose.
If you run this from a *nix shell, then when the program terminates, the cursor will write out the % or $ or whatever the shell uses to indicate that it's listening in the same line. It doesn't look good...
-Albatross
EDIT: i686 - 80,000 posts, and counting.
Last edited on May 17, 2010 at 11:00pm UTC
May 17, 2010 at 10:47pm UTC
The Windows console tends to make sure that programs terminate with a newline written to the screen. You'll get different behavior on *nix shells.
May 19, 2010 at 7:56pm UTC
Thanks for the infos.
Vexer: Yea I think I accidentally made 2 similar post which I have to delete.
Albatross and Douas: Im not familiar to *nix shell but it gave me an idea on how (/n) works.