0% found this document useful (0 votes)
38 views86 pages

Formatted Output

The document demonstrates the use of format specifiers in C printf statements to control the formatting of output. It shows how specifiers like %d, %s, and modifiers like numbers, dots, and minus signs can set minimum widths, maximum widths, left adjustment, zero padding and other formatting of integer and string output. Examples are provided of formatting integers of various widths and a string with different length controls.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views86 pages

Formatted Output

The document demonstrates the use of format specifiers in C printf statements to control the formatting of output. It shows how specifiers like %d, %s, and modifiers like numbers, dots, and minus signs can set minimum widths, maximum widths, left adjustment, zero padding and other formatting of integer and string output. Examples are provided of formatting integers of various widths and a string with different length controls.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 86

$

char *str = "hello, world";


printf(":%s:\n", str);

$
char *str = "hello, world";
printf(":%s:\n", str);

$ ./a.out ê
char *str = "hello, world";
printf(":%s:\n", str);

$ ./a.out ê
:hello, world:
char *str = "hello, world";
printf(":%s:\n", str);
printf(":%15s:\n", str);

$ ./a.out ê
:hello, world:
char *str = "hello, world";
printf(":%s:\n", str);
printf(":%15s:\n", str);

number – min. width

$ ./a.out ê
:hello, world:
char *str = "hello, world";
printf(":%s:\n", str);
printf(":%15s:\n", str);

number – min. width

$ ./a.out ê
:hello, world:
: hello, world:
char *str = "hello, world";
printf(":%s:\n", str);
printf(":%15s:\n", str);
printf(":%10s:\n", str);
number – min. width

$ ./a.out ê
:hello, world:
: hello, world:
char *str = "hello, world";
printf(":%s:\n", str);
printf(":%15s:\n", str);
printf(":%10s:\n", str);
number – min. width

$ ./a.out ê
:hello, world:
: hello, world:
:hello, world:
char *str = "hello, world";
printf(":%s:\n", str);
printf(":%15s:\n", str);
printf(":%10s:\n", str);
printf(":%-15s:\n", str); number – min. width

$ ./a.out ê
:hello, world:
: hello, world:
:hello, world:
char *str = "hello, world";
printf(":%s:\n", str);
printf(":%15s:\n", str);
number – min. width
printf(":%10s:\n", str);
printf(":%-15s:\n", str);
minus – adjust left

$ ./a.out ê
:hello, world:
: hello, world:
:hello, world:
char *str = "hello, world";
printf(":%s:\n", str);
printf(":%15s:\n", str);
number – min. width
printf(":%10s:\n", str);
printf(":%-15s:\n", str);
minus – adjust left

$ ./a.out ê
:hello, world:
: hello, world:
:hello, world:
:hello, world :
char *str = "hello, world";
printf(":%s:\n", str);
printf(":%15s:\n", str);
number – min. width
printf(":%10s:\n", str);
printf(":%-15s:\n", str);
minus – adjust left
printf(":%.10s:\n", str);

$ ./a.out ê
:hello, world:
: hello, world:
:hello, world:
:hello, world :
char *str = "hello, world";
printf(":%s:\n", str);
printf(":%15s:\n", str); number – min. width
printf(":%10s:\n", str);
printf(":%-15s:\n", str); minus – adjust left
printf(":%.10s:\n", str);
.number – max. characters

$ ./a.out ê
:hello, world:
: hello, world:
:hello, world:
:hello, world :
char *str = "hello, world";
printf(":%s:\n", str);
printf(":%15s:\n", str); number – min. width
printf(":%10s:\n", str);
printf(":%-15s:\n", str); minus – adjust left
printf(":%.10s:\n", str);
.number – max. characters

$ ./a.out ê
:hello, world:
: hello, world:
:hello, world:
:hello, world :
:hello, wor:
char *str = "hello, world";
printf(":%s:\n", str);
printf(":%15s:\n", str); number – min. width
printf(":%10s:\n", str);
printf(":%-15s:\n", str); minus – adjust left
printf(":%.10s:\n", str);
printf(":%15.10s:\n", str); .number – max. characters

$ ./a.out ê
:hello, world:
: hello, world:
:hello, world:
:hello, world :
:hello, wor:
char *str = "hello, world";
printf(":%s:\n", str);
printf(":%15s:\n", str); number – min. width
printf(":%10s:\n", str);
printf(":%-15s:\n", str); minus – adjust left
printf(":%.10s:\n", str);
printf(":%15.10s:\n", str); .number – max. characters

$ ./a.out ê
:hello, world:
: hello, world:
:hello, world:
:hello, world :
:hello, wor:
: hello, wor:
char *str = "hello, world";
printf(":%s:\n", str);
printf(":%15s:\n", str); number – min. width
printf(":%10s:\n", str);
printf(":%-15s:\n", str); minus – adjust left
printf(":%.10s:\n", str);
printf(":%15.10s:\n", str); .number – max. characters
printf(":%-15.10s:\n", str);

$ ./a.out ê
:hello, world:
: hello, world:
:hello, world:
:hello, world :
:hello, wor:
: hello, wor:
char *str = "hello, world";
printf(":%s:\n", str);
printf(":%15s:\n", str); number – min. width
printf(":%10s:\n", str);
printf(":%-15s:\n", str); minus – adjust left
printf(":%.10s:\n", str);
printf(":%15.10s:\n", str); .number – max. characters
printf(":%-15.10s:\n", str);

$ ./a.out ê
:hello, world:
: hello, world:
:hello, world:
:hello, world :
:hello, wor:
: hello, wor:
:hello, wor :
$
int a = 12;
printf(":%d:\n", a);

$
int a = 12;
printf(":%d:\n", a);

$ ./a.out ê
int a = 12;
printf(":%d:\n", a);

$ ./a.out ê
:12:
int a = 12;
printf(":%d:\n", a);
printf(":%3d:\n", a);

$ ./a.out ê
:12:
int a = 12;
printf(":%d:\n", a);
printf(":%3d:\n", a);

number – min. width

$ ./a.out ê
:12:
int a = 12;
printf(":%d:\n", a);
printf(":%3d:\n", a);

number – min. width

$ ./a.out ê
:12:
: 12:
int a = 12;
printf(":%d:\n", a);
printf(":%3d:\n", a);
printf(":%1d:\n", a);
number – min. width

$ ./a.out ê
:12:
: 12:
int a = 12;
printf(":%d:\n", a);
printf(":%3d:\n", a);
printf(":%1d:\n", a);
number – min. width

$ ./a.out ê
:12:
: 12:
:12:
int a = 12;
printf(":%d:\n", a);
printf(":%3d:\n", a);
printf(":%1d:\n", a);
printf(":%-3d:\n", a);
number – min. width

$ ./a.out ê
:12:
: 12:
:12:
int a = 12;
printf(":%d:\n", a);
printf(":%3d:\n", a);
printf(":%1d:\n", a); number – min. width
printf(":%-3d:\n", a);
minus – adjust left

$ ./a.out ê
:12:
: 12:
:12:
int a = 12;
printf(":%d:\n", a);
printf(":%3d:\n", a);
printf(":%1d:\n", a); number – min. width
printf(":%-3d:\n", a);
minus – adjust left

$ ./a.out ê
:12:
: 12:
:12:
:12 :
int a = 12;
printf(":%d:\n", a);
printf(":%3d:\n", a);
printf(":%1d:\n", a); number – min. width
printf(":%-3d:\n", a);
printf(":%.3d:\n", a); minus – adjust left

$ ./a.out ê
:12:
: 12:
:12:
:12 :
int a = 12;
printf(":%d:\n", a);
printf(":%3d:\n", a);
number – min. width
printf(":%1d:\n", a);
printf(":%-3d:\n", a);
minus – adjust left
printf(":%.3d:\n", a);
.number – min. digits, pad 0

$ ./a.out ê
:12:
: 12:
:12:
:12 :
int a = 12;
printf(":%d:\n", a);
printf(":%3d:\n", a);
number – min. width
printf(":%1d:\n", a);
printf(":%-3d:\n", a);
minus – adjust left
printf(":%.3d:\n", a);
.number – min. digits, pad 0

$ ./a.out ê
:12:
: 12:
:12:
:12 :
:012:
int a = 12;
printf(":%d:\n", a);
printf(":%3d:\n", a);
number – min. width
printf(":%1d:\n", a);
printf(":%-3d:\n", a);
minus – adjust left
printf(":%.3d:\n", a);
printf(":%.4d:\n", a);
.number – min. digits, pad 0

$ ./a.out ê
:12:
: 12:
:12:
:12 :
:012:
int a = 12;
printf(":%d:\n", a);
printf(":%3d:\n", a);
number – min. width
printf(":%1d:\n", a);
printf(":%-3d:\n", a);
minus – adjust left
printf(":%.3d:\n", a);
printf(":%.4d:\n", a);
.number – min. digits, pad 0

$ ./a.out ê
:12:
: 12:
:12:
:12 :
:012:
:0012:
int a = 12;
printf(":%d:\n", a);
printf(":%3d:\n", a);
number – min. width
printf(":%1d:\n", a);
printf(":%-3d:\n", a);
minus – adjust left
printf(":%.3d:\n", a);
printf(":%.4d:\n", a);
.number – min. digits, pad 0
printf(":%-4d:\n", a);

$ ./a.out ê
:12:
: 12:
:12:
:12 :
:012:
:0012:
int a = 12;
printf(":%d:\n", a);
printf(":%3d:\n", a);
number – min. width
printf(":%1d:\n", a);
printf(":%-3d:\n", a);
minus – adjust left
printf(":%.3d:\n", a);
printf(":%.4d:\n", a);
.number – min. digits, pad 0
printf(":%-4d:\n", a);

$ ./a.out ê
:12:
: 12:
:12:
:12 :
:012:
:0012:
:12 :
int a = 12;
printf(":%d:\n", a);
printf(":%3d:\n", a);
number – min. width
printf(":%1d:\n", a);
printf(":%-3d:\n", a);
minus – adjust left
printf(":%.3d:\n", a);
printf(":%.4d:\n", a);
.number – min. digits, pad 0
printf(":%-4d:\n", a);
printf(":%4.3d:\n", a);

$ ./a.out ê
:12:
: 12:
:12:
:12 :
:012:
:0012:
:12 :
int a = 12;
printf(":%d:\n", a);
printf(":%3d:\n", a);
number – min. width
printf(":%1d:\n", a);
printf(":%-3d:\n", a);
minus – adjust left
printf(":%.3d:\n", a);
printf(":%.4d:\n", a);
.number – min. digits, pad 0
printf(":%-4d:\n", a);
printf(":%4.3d:\n", a);

$ ./a.out ê
:12:
: 12:
:12:
:12 :
:012:
:0012:
:12 :
: 012:
int a = 12;
printf(":%d:\n", a);
printf(":%3d:\n", a);
number – min. width
printf(":%1d:\n", a);
printf(":%-3d:\n", a);
minus – adjust left
printf(":%.3d:\n", a);
printf(":%.4d:\n", a);
.number – min. digits, pad 0
printf(":%-4d:\n", a);
printf(":%4.3d:\n", a);
printf(":%-4.3d:\n", a);
$ ./a.out ê
:12:
: 12:
:12:
:12 :
:012:
:0012:
:12 :
: 012:
int a = 12;
printf(":%d:\n", a);
printf(":%3d:\n", a);
number – min. width
printf(":%1d:\n", a);
printf(":%-3d:\n", a);
minus – adjust left
printf(":%.3d:\n", a);
printf(":%.4d:\n", a);
.number – min. digits, pad 0
printf(":%-4d:\n", a);
printf(":%4.3d:\n", a);
printf(":%-4.3d:\n", a);
$ ./a.out ê
:12:
: 12:
:12:
:12 :
:012:
:0012:
:12 :
: 012:
:012 :
$
float f = 12.4356;
printf(":%f:\n", f);

$
float f = 12.4356;
printf(":%f:\n", f);

$ ./a.out ê
float f = 12.4356;
printf(":%f:\n", f);

$ ./a.out ê
:12.435600:
float f = 12.4356;
printf(":%f:\n", f);
printf(":%10f:\n", f);

$ ./a.out ê
:12.435600:
float f = 12.4356;
printf(":%f:\n", f);
printf(":%10f:\n", f);

number – min. width

$ ./a.out ê
:12.435600:
float f = 12.4356;
printf(":%f:\n", f);
printf(":%10f:\n", f);

number – min. width

$ ./a.out ê
:12.435600:
: 12.435600:
float f = 12.4356;
printf(":%f:\n", f);
printf(":%10f:\n", f);
printf(":%5f:\n", f);
number – min. width

$ ./a.out ê
:12.435600:
: 12.435600:
float f = 12.4356;
printf(":%f:\n", f);
printf(":%10f:\n", f);
printf(":%5f:\n", f);
number – min. width

$ ./a.out ê
:12.435600:
: 12.435600:
:12.435600:
float f = 12.4356;
printf(":%f:\n", f);
printf(":%10f:\n", f);
printf(":%5f:\n", f);
printf(":%-10f:\n", f); number – min. width

$ ./a.out ê
:12.435600:
: 12.435600:
:12.435600:
float f = 12.4356;
printf(":%f:\n", f);
printf(":%10f:\n", f);
number – min. width
printf(":%5f:\n", f);
printf(":%-10f:\n", f);
minus – adjust left

$ ./a.out ê
:12.435600:
: 12.435600:
:12.435600:
float f = 12.4356;
printf(":%f:\n", f);
printf(":%10f:\n", f);
number – min. width
printf(":%5f:\n", f);
printf(":%-10f:\n", f);
minus – adjust left

$ ./a.out ê
:12.435600:
: 12.435600:
:12.435600:
:12.435600 :
float f = 12.4356;
printf(":%f:\n", f);
printf(":%10f:\n", f);
number – min. width
printf(":%5f:\n", f);
printf(":%-10f:\n", f);
minus – adjust left
printf(":%.3f:\n", f);

$ ./a.out ê
:12.435600:
: 12.435600:
:12.435600:
:12.435600 :
float f = 12.4356;
printf(":%f:\n", f);
printf(":%10f:\n", f); number – min. width
printf(":%5f:\n", f);
printf(":%-10f:\n", f); minus – adjust left
printf(":%.3f:\n", f);
.number – max. decimal digits

$ ./a.out ê
:12.435600:
: 12.435600:
:12.435600:
:12.435600 :
float f = 12.4356;
printf(":%f:\n", f);
printf(":%10f:\n", f); number – min. width
printf(":%5f:\n", f);
printf(":%-10f:\n", f); minus – adjust left
printf(":%.3f:\n", f);
.number – max. decimal digits

$ ./a.out ê
:12.435600:
: 12.435600:
:12.435600:
:12.435600 :
:12.436:
float f = 12.4356;
printf(":%f:\n", f);
printf(":%10f:\n", f); number – min. width
printf(":%5f:\n", f);
printf(":%-10f:\n", f); minus – adjust left
printf(":%.3f:\n", f);
printf(":%-4f:\n", f); .number – max. decimal digits

$ ./a.out ê
:12.435600:
: 12.435600:
:12.435600:
:12.435600 :
:12.436:
float f = 12.4356;
printf(":%f:\n", f);
printf(":%10f:\n", f); number – min. width
printf(":%5f:\n", f);
printf(":%-10f:\n", f); minus – adjust left
printf(":%.3f:\n", f);
printf(":%-4f:\n", f); .number – max. decimal digits

$ ./a.out ê
:12.435600:
: 12.435600:
:12.435600:
:12.435600 :
:12.436:
:12.4356:
float f = 12.4356;
printf(":%f:\n", f);
printf(":%10f:\n", f); number – min. width
printf(":%5f:\n", f);
printf(":%-10f:\n", f); minus – adjust left
printf(":%.3f:\n", f);
printf(":%-4f:\n", f); .number – max. decimal digits
printf(":%10.3f:\n", f);

$ ./a.out ê
:12.435600:
: 12.435600:
:12.435600:
:12.435600 :
:12.436:
:12.4356:
float f = 12.4356;
printf(":%f:\n", f);
printf(":%10f:\n", f); number – min. width
printf(":%5f:\n", f);
printf(":%-10f:\n", f); minus – adjust left
printf(":%.3f:\n", f);
printf(":%-4f:\n", f); .number – max. decimal digits
printf(":%10.3f:\n", f);

$ ./a.out ê
:12.435600:
: 12.435600:
:12.435600:
:12.435600 :
:12.436:
:12.4356:
: 12.436:
float f = 12.4356;
printf(":%f:\n", f);
printf(":%10f:\n", f); number – min. width
printf(":%5f:\n", f);
printf(":%-10f:\n", f); minus – adjust left
printf(":%.3f:\n", f);
printf(":%-4f:\n", f); .number – max. decimal digits
printf(":%10.3f:\n", f);
printf(":%-10.3f:\n", f);

$ ./a.out ê
:12.435600:
: 12.435600:
:12.435600:
:12.435600 :
:12.436:
:12.4356:
: 12.436:
float f = 12.4356;
printf(":%f:\n", f);
printf(":%10f:\n", f); number – min. width
printf(":%5f:\n", f);
printf(":%-10f:\n", f); minus – adjust left
printf(":%.3f:\n", f);
printf(":%-4f:\n", f); .number – max. decimal digits
printf(":%10.3f:\n", f);
printf(":%-10.3f:\n", f);

$ ./a.out ê
:12.435600:
: 12.435600:
:12.435600:
:12.435600 :
:12.436:
:12.4356:
: 12.436:
:12.436 :
char *str = "hello, world";

$
char *str = "hello, world";
printf(":%*.*s:\n", 10, 3, str);

$
char *str = "hello, world";
printf(":%*.*s:\n", 10, 3, str);

$
char *str = "hello, world";
printf(":%*.*s:\n", 10, 3, str);

$ ./a.out ê
char *str = "hello, world";
printf(":%*.*s:\n", 10, 3, str);

$ ./a.out ê
: hel:
char *str = "hello, world";
printf(":%*.*s:\n", 10, 3, str);
printf(":%-*.*s:\n", 10, 3, str);

$ ./a.out ê
: hel:
char *str = "hello, world";
printf(":%*.*s:\n", 10, 3, str);
printf(":%-*.*s:\n", 10, 3, str);

$ ./a.out ê
: hel:
:hel :
char *str = "hello, world";
printf(":%*.*s:\n", 10, 3, str);
printf(":%-*.*s:\n", 10, 3, str);

int a = 10, b = 3;

$ ./a.out ê
: hel:
:hel :
char *str = "hello, world";
printf(":%*.*s:\n", 10, 3, str);
printf(":%-*.*s:\n", 10, 3, str);

int a = 10, b = 3;
printf(":%*.*s:\n", a, b, str);

$ ./a.out ê
: hel:
:hel :
char *str = "hello, world";
printf(":%*.*s:\n", 10, 3, str);
printf(":%-*.*s:\n", 10, 3, str);

int a = 10, b = 3;
printf(":%*.*s:\n", a, b, str);

$ ./a.out ê
: hel:
:hel :
: hel:
char *str = "hello, world\n";

$
char *str = "hello, world\n";
printf(str);

$
char *str = "hello, world\n";
printf(str);

$ ./a.out ê
char *str = "hello, world\n";
printf(str);

$ ./a.out ê
hello, world
Format string
need not be
char *str = "hello, world\n";
fixed.
printf(str);

$ ./a.out ê
hello, world
Format string
need not be
char *str = "hello, world %d\n";
fixed.
printf(str);

$ ./a.out ê
hello, world
Format string
need not be
char *str = "hello, world %d\n";
fixed.
printf(str, 10);

$ ./a.out ê
hello, world
Format string
need not be
char *str = "hello, world %d\n";
fixed.
printf(str, 10);

$ ./a.out ê
hello, world 10
Format string
need not be
char *str = "hello, %s world\n";
fixed.
printf(str);
First argument is
always format
string.

$ ./a.out ê
Format string
need not be
char *str = "hello, %s world\n";
fixed.
printf(str, "\n");
First argument is
always format
string.

$ ./a.out ê
Format string
need not be
char *str = "hello, %s world\n";
fixed.
printf(str, "\n");
First argument is
always format
string.

$ ./a.out ê
hello,
world
Format string
need not be
char *str = "hello, %s world\n";
fixed.
printf(str, str);
First argument is
always format
string.

$ ./a.out ê
Format string
need not be
char *str = "hello, %s world\n";
fixed.
printf(str, str);
First argument is
always format
string.

$ ./a.out ê
hello, hello, %s world
world

You might also like