A simple question about n/

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
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
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
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.
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.
Topic archived. No new replies allowed.