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 ...
随机推荐
- 刷题向》DP》关于基础DP(easy)
openjudge 8464 这道题其实很简单,算是DP的基础题,比较适合开拓DP思维. 题目比较有欺骗性,其实稍微想想就可以解决,因为题意说第一次卖出后才能买入,所以我们可以考虑枚举断点,所以题目 ...
- java用JDBC连接数据库的方式
//驱动位置String sDBDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";//连接数据库地址名字String ...
- SpringBoot15 sell01 项目创建、MySQL数据库连接、日志配置、开发热部署、商品信息模块
项目软件版本说明: jdk: 1.8 springboot: 2.0.1 mysql: 5.7 1 项目创建 创建一个SpringBoot项目即可,创建是勾选 web jpa mysql 这三个依赖就 ...
- 283. Move Zeroes把零放在最后面
[抄题]: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...
- maven构建非法字符解决办法
CI使用maven做版本构建时候碰到了一个问题,有个java源码始终编译报错,错误发生在文件第一行. 出错内容是: ***.java:[1,1] 非法字符: \65279 后面上网看了,原来是文件编码 ...
- python DDT读取excel测试数据
转自:http://www.cnblogs.com/nuonuozhou/p/8645129.html ddt 结合单元测试一起用 ddt(data.driven.test):数据驱动测试 由外部 ...
- JavaScript补充:BOM(浏览器对象模型)
一些公共对象.详细参考手册. 一.Window 对象 Window 对象表示浏览器中打开的窗口. 如果文档包含框架(<frame> 或 <iframe> 标签),浏览器会为 H ...
- Server Sql 多表查询、子查询和分页
一.多表查询:根据特定的连接条件从不同的表中获取所需的数据 多表查询语法: SELECT table1.column, table2.column FROM table1, table2 WHERE ...
- 21天学通C++学习笔记(一):入门
1. 基础概念 1.1 编程语言 旨在让人更容易得使用计算资源,充分利用电脑,不用人做一些重复劳动或持续参与. 1.2 可执行文件 是可运行的成品,应按程序员希望的那样做. 1.3 生成可执行文件的步 ...
- 十二、Nodejs 包与 NPM 第三方模块安装 package.json 以及 CNPM
1. 包 Nodejs 中除了它自己提供的核心模块外,我们可以自定义模块,也可以使用第三方的模块.Nodejs 中第三方模块由包组成,可以通过包来对一组具有相互依赖关系的模块进行统一管理. 在 Nod ...