A printf format reference page (cheat sheet)
Summary: This page is a printf formatting cheat sheet. I originally created this cheat sheet for my own purposes, and then thought I would share it here.
A cool thing about the printf
formatting syntax is that the specifiers you can use are very similar, if not identical, between different languages, including C, C++, Java, Perl, Ruby, Scala, and others. So your printf knowledge is reusable, which is a good thing.
printf formatting with Perl and Java
In this cheat sheet I’ll show all the examples using Perl, but at first it might help to see one example using both Perl and Java. So, here’s a simple Perl printf example to get us started:
printf("the %s jumped over the %s, %d times", "cow", "moon", 2);
And here are three different Java printf examples, using different methods that are available to you in the Java programming language:
System.out.format("the %s jumped over the %s, %d times", "cow", "moon", 2);
System.err.format("the %s jumped over the %s, %d times", "cow", "moon", 2);
String result = String.format("the %s jumped over the %s, %d times", "cow", "moon", 2);
As you can see in that last String.format
example, that line of code doesn’t print any output, while the first line prints to standard output, and the second line prints to standard error.
In the remainder of this document I’ll use Perl examples, but again, the actual format specifier strings can be used in many different languages.
printf format specifiers - summary
Here’s a quick summary of the available printf format specifiers:
%c | character |
%d | decimal (integer) number (base 10) |
%e | exponential floating-point number |
%f | floating-point number |
%i | integer (base 10) |
%o | octal number (base 8) |
%s | a string of characters |
%u | unsigned decimal (integer) number |
%x | number in hexadecimal (base 16) |
%% | print a percent sign |
\% | print a percent sign |
Controlling integer width with printf
The %3d
specifier means a minimum width of three spaces, which, by default, will be right-justified:
printf("%3d", 0); | 0 |
printf("%3d", 123456789); | 123456789 |
printf("%3d", -10); | -10 |
printf("%3d", -123456789); | -123456789 |
Left-justifying printf integer output
To left-justify integer output with printf, just add a minus sign (-
) after the %
symbol, like this:
printf("%-3d", 0); | 0 |
printf("%-3d", 123456789); | 123456789 |
printf("%-3d", -10); | -10 |
printf("%-3d", -123456789); | -123456789 |
The printf zero-fill option
To zero-fill your printf integer output, just add a zero (0
) after the %
symbol, like this:
printf("%03d", 0); | 000 |
printf("%03d", 1); | 001 |
printf("%03d", 123456789); | 123456789 |
printf("%03d", -10); | -10 |
printf("%03d", -123456789); | -123456789 |
printf integer formatting
As a summary of printf integer formatting, here’s a little collection of integer formatting examples. Several different options are shown, including a minimum width specification, left-justified, zero-filled, and also a plus sign for positive numbers.
Description | Code | Result |
---|---|---|
At least five wide | printf("'%5d'", 10); | ' 10' |
At least five-wide, left-justified | printf("'%-5d'", 10); | '10 ' |
At least five-wide, zero-filled | printf("'%05d'", 10); | '00010' |
At least five-wide, with a plus sign | printf("'%+5d'", 10); | ' +10' |
Five-wide, plus sign, left-justified | printf("'%-+5d'", 10); | '+10 ' |
printf - floating point numbers
Here are several examples showing how to format floating-point numbers with printf:
Description | Code | Result |
---|---|---|
Print one position after the decimal | printf("'%.1f'", 10.3456); | '10.3' |
Two positions after the decimal | printf("'%.2f'", 10.3456); | '10.35' |
Eight-wide, two positions after the decimal | printf("'%8.2f'", 10.3456); | ' 10.35' |
Eight-wide, four positions after the decimal | printf("'%8.4f'", 10.3456); | ' 10.3456' |
Eight-wide, two positions after the decimal, zero-filled | printf("'%08.2f'", 10.3456); | '00010.35' |
Eight-wide, two positions after the decimal, left-justified | printf("'%-8.2f'", 10.3456); | '10.35 ' |
Printing a much larger number with that same format | printf("'%-8.2f'", 101234567.3456); | '101234567.35' |
printf string formatting
Here are several examples that show how to format string output with printf:
Description | Code | Result |
---|---|---|
A simple string | printf("'%s'", "Hello"); | 'Hello' |
A string with a minimum length | printf("'%10s'", "Hello"); | ' Hello' |
Minimum length, left-justified | printf("'%-10s'", "Hello"); | 'Hello ' |
Summary of special printf characters
The following character sequences have a special meaning when used as printf format specifiers:
\a | audible alert |
\b | backspace |
\f | form feed |
\n | newline, or linefeed |
\r | carriage return |
\t | tab |
\v | vertical tab |
\\ | backslash |
As you can see from that last example, because the backslash character itself is treated specially, you have to print two backslash characters in a row to get one backslash character to appear in your output.
Here are a few examples of how to use these special characters:
Description | Code | Result |
---|---|---|
Insert a tab character in a string | printf("Hello\tworld"); | Hello world |
Insert a newline character in a string | printf("Hello\nworld"); | Hello world |
Typical use of the newline character | printf("Hello world\n"); | Hello world |
A DOS/Windows path with backslash characters | printf("C:\\Windows\\System32\\"); | C:\Windows\System32\ |
A printf format reference page (cheat sheet)的更多相关文章
- XSS (Cross Site Scripting) Prevention Cheat Sheet(XSS防护检查单)
本文是 XSS防御检查单的翻译版本 https://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sh ...
- IOS Application Security Testing Cheat Sheet
IOS Application Security Testing Cheat Sheet [hide] 1 DRAFT CHEAT SHEET - WORK IN PROGRESS 2 Int ...
- XSS Filter Evasion Cheat Sheet 中文版
前言 译者注: 翻译本文的最初原因是当我自己看到这篇文章后,觉得它是非常有价值.但是这么著名的一个备忘录却一直没有人把它翻译成中文版.很多人仅仅是简单的把文中的 各种代码复制下来,然后看起来很刁的发在 ...
- XSS Cheat Sheet
Basic and advanced exploits for XSS proofs and attacks. Work in progress, bookmark it. Technique Vec ...
- Git Cheat Sheet
Merge Undo git merge with conflicts $ git merge --abort Archive $ git archive --format zip --output ...
- [转]Swift Cheat Sheet
原文:http://kpbp.github.io/swiftcheatsheet/ A quick cheat sheet and reference guide for Apple's Swift ...
- [转]Blue Prism VBO Cheat Sheet
本文转自:https://www.cheatography.com/ethanium/cheat-sheets/blue-prism-vbo/ Blue Prism MAPIEx Configure ...
- Racket Cheat Sheet
Racket Cheat Sheet 来源 http://docs.racket-lang.org/racket-cheat/index.html?q=Racket%20Cheat%20Sheet ...
- numpy, pandas, scikit-learn cheat sheet (速查表)
1. scikit-learn cheat sheet 官方链接如下:http://scikit-learn.org/stable/tutorial/machine_learning_map/ Oft ...
随机推荐
- 【转发】徐汉彬:Web系统大规模并发——电商秒杀与抢购
徐汉彬:Web系统大规模并发——电商秒杀与抢购 发表于2014-12-02 09:30| 73110次阅读| 来源CSDN| 114 条评论| 作者徐汉彬 问底徐汉彬大数据 摘要:电商的秒杀和抢购,从 ...
- 1-2 开发环境搭建-Windows平台
C:\Program Files\nodejs\node_modules\npm\npmrc C:\Users\ZHONGZHENHUA\.android\avd H:\heimaandroidadt ...
- 3-1 Git下载与安装
https://desktop.github.com/
- Luogu 4198 楼房重建
BZOJ 2957 挺妙的题. 先把题目中的要求转化为斜率,一个点$(x, y)$可以看成$\frac{y}{x}$,这样子我们要求的就变成了一个区间内一定包含第一个值的最长上升序列. 然后把这个序列 ...
- Vue.js 安装及其环境搭建
For me or other first studying vue.js. For Windows PC: 1.先安装node.js 安装官网最新的即可 版本应该要大于6.0版本 nodejs的官网 ...
- 【linux命令】setterm控制终端属性命令(中英文)
[linux命令]setterm控制终端属性命令(中英文) 2018年03月23日 17:13:44 阅读数:489 标签: linux 更多 个人分类: linux 摘自:https://blog. ...
- hdu 2206 IP的计算(最全的注意事项)
注意: 1.非法字符(包括空格) 如下都是错的 A.145.124.4 192.168.18 123(用scanf输入的话,则接收的是:192.168.18) 2.'.'有且仅有3个,且不能连续出现, ...
- Shell内置命令
主要Shell内置命令 Shell有很多内置在其源代码中的命令.这些命令是内置的,所以Shell不必到磁盘上搜索它们,执行速度因此加快.不同的Shell内置命令有所不同. A.2.1 bash内置命 ...
- PHP中循环结构之foreach循环语句
在PHP中foreach循环语句,常用于遍历数组,一般有两种使用方式:不取下标.取下标. (1)只取值,不取下标 <?php foreach (数组 as 值){ //执行的任务 } ?> ...
- 20169219《移动平台开发实践》移动APP设计应该考虑到的问题
1.开发流程包括: (1)用户需求分析 (2)产品原型设计 (3)UI视觉设计 (4)APP开发 (5)项目测试 (6)发布 App开发经过UI设计完成之后,便会进入开发阶段. (1)服务器端:编写接 ...