Nightlock32
Nightlock32

Reputation: 479

How do I add a newline using printf?

How do I add a new line to a print command using printf?

printf "I want this on a new line!"

I thought it would be something like this but it didn't work

printf "/n I want this on a new line!/n"

Upvotes: 42

Views: 274111

Answers (3)

rulshid sm
rulshid sm

Reputation: 11

Sample program to understand it better:

#include <iostream>

using namespace std;

char ch;
long lo;
int in_;
double dou;
float fl;

int main() {
   
scanf("%d %ld %c %f %lf",&in_,&lo,&ch,&fl,&dou);
printf("%d\n%ld\n%c\n%f\n%lf\n",in_,lo,ch,fl,dou);
    
}


sample input :3 12345678912345 a 334.23 14049.30493
sample output:
3
12345678912345
a
334.230
14049.304930000

Upvotes: 1

Dennis Williamson
Dennis Williamson

Reputation: 360035

Try this:

printf '\n%s\n' 'I want this on a new line!'

That allows you to separate the formatting from the actual text. You can use multiple placeholders and multiple arguments.

quantity=38; price=142.15; description='advanced widget'
$ printf '%8d%10.2f  %s\n' "$quantity" "$price" "$description"
      38    142.15  advanced widget

Upvotes: 35

petschekr
petschekr

Reputation: 1311

To write a newline use \n not /n the latter is just a slash and a n

Upvotes: 50

Related Questions