avm
avm

Reputation: 399

Is there any drawback to using '\n' at the start instead of end?

Mostly I see people using \n at the end of string but putting \n at the beginning makes more sense to me since now I don't have to keep track of what will be printed next. For example-

std::cout<<"Some string\n";  //syntax 1

Suppose after this the control goes to some other function where I don't need a new line but using this syntax is enforcing that newline to be inserted unless I can think ahead and keep track of whether next line needs to be printed in newline or not.

std::cout<<"\nSome string";  //syntax 2

But by using the second syntax I can avoid such things and I only have to worry about the current statement.

Question- Is it only a personal preference of using either of the 2 syntaxes or is there any drawback to second one over the first one?

Upvotes: 3

Views: 549

Answers (4)

ccdMuro
ccdMuro

Reputation: 229

It depends on the context you are writing your output in.

Syntax 1

If you want to make sure that your output begins with a new line and you can't tell if the cursor is at the beginning of a line you would favor syntax 1.

Example without '\n':

Output you don't control
----------------------------
Value 1 | Value 2 | Value 3Some string of your function.

Example with '\n':

Output you don't control
----------------------------
Value 1 | Value 2 | Value 3 
Some string of your function.

Syntax 2

On the other hand, if you are done with your output it is good practice to finish with a new line so the upcoming output doesn't have to be concerned in what state you left the cursor in.

Example without '\n':

Some string of your function.Output you don't control
----------------------------
Value 1 | Value 2 | Value 3 

Example with '\n':

Some string of your function.
Output you don't control
----------------------------
Value 1 | Value 2 | Value 3 

Upvotes: 0

ljleb
ljleb

Reputation: 312

In unix systems, when the content of standard output is intended to be the input of another program, printing a \n last generally means being able to interact with a larger set of command line utilities.

In the end it depends on what you want to do with standard output. That's why it's possible to write both ways.

Upvotes: 0

Clifford
Clifford

Reputation: 93466

It is not at all "personal preference" - the two solutions are semantically different. You would use one over the other when the requirements of your application demand it.

One critical point though is on many platforms \n causes any buffered text to be flushed and the text to be output. If you delay the \n you may not see the output immediately until the next \n which may not be deterministic or timely.

Upvotes: 7

Shreya
Shreya

Reputation: 1

You can use either of these syntaxes according to your needs. A choice will matter in case of recurring statements such as loops and recursive functions where a string needs to be printed.

The first syntax std::cout<<"\n String"; will however always begin printing after it has inserted a new line, which might not suit your purpose if it's the first line that's being printed.

But I believe it's just a matter of personal preference.

Upvotes: 0

Related Questions