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)的更多相关文章

  1. XSS (Cross Site Scripting) Prevention Cheat Sheet(XSS防护检查单)

    本文是 XSS防御检查单的翻译版本 https://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sh ...

  2. IOS Application Security Testing Cheat Sheet

    IOS Application Security Testing Cheat Sheet    [hide]  1 DRAFT CHEAT SHEET - WORK IN PROGRESS 2 Int ...

  3. XSS Filter Evasion Cheat Sheet 中文版

    前言 译者注: 翻译本文的最初原因是当我自己看到这篇文章后,觉得它是非常有价值.但是这么著名的一个备忘录却一直没有人把它翻译成中文版.很多人仅仅是简单的把文中的 各种代码复制下来,然后看起来很刁的发在 ...

  4. XSS Cheat Sheet

    Basic and advanced exploits for XSS proofs and attacks. Work in progress, bookmark it. Technique Vec ...

  5. Git Cheat Sheet

    Merge Undo git merge with conflicts $ git merge --abort Archive $ git archive --format zip --output ...

  6. [转]Swift Cheat Sheet

    原文:http://kpbp.github.io/swiftcheatsheet/ A quick cheat sheet and reference guide for Apple's Swift ...

  7. [转]Blue Prism VBO Cheat Sheet

    本文转自:https://www.cheatography.com/ethanium/cheat-sheets/blue-prism-vbo/ Blue Prism MAPIEx Configure ...

  8. Racket Cheat Sheet

    Racket Cheat Sheet 来源  http://docs.racket-lang.org/racket-cheat/index.html?q=Racket%20Cheat%20Sheet ...

  9. numpy, pandas, scikit-learn cheat sheet (速查表)

    1. scikit-learn cheat sheet 官方链接如下:http://scikit-learn.org/stable/tutorial/machine_learning_map/ Oft ...

随机推荐

  1. AlphaPose ubuntu16 python2安装

    #https://www.tensorflow.org/install/install_linux#ValidateYourInstallation #https://github.com/MVIG- ...

  2. 11-内涵段子-爬虫(python+正则)

    爬取内涵段子,使用正则进行简单处理: #_*_ coding: utf-8 _*_ ''' Created on 2018年7月14日 @author: sss function:爬去内涵段子(静态网 ...

  3. 面试题:四种Java线程池用法解析 !=!=未看

    1.new Thread的弊端 执行一个异步任务你还只是如下new Thread吗? 1 2 3 4 5 6 7 8 new Thread(new Runnable() {     @Override ...

  4. while 循环和do while循环

    while循环是先检测条件符合不符合,符合才执行循环体内容,不符合就跳过while循环. 就和一个房间有两个门,一个前门,一个后门,while循环是当你进入前门的时候有人会检查你的身份,只有身份符合条 ...

  5. cakephp中sql查询in

    $list = $this->Capital->find('all', array('conditions'=>array('remark in '=>array('银联支付' ...

  6. Oracle数据库管理

    一.Oracle 的(资源限制)概要文件 为了控制系统资源的使用, 可以利用资源限制概要文件. 资源限制概要文件是 Oracle 安全策略的重要组成部分, 利用资源限制概要文件可以对数据库用户进行基本 ...

  7. threading学习

    多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理. 用户界面可以更加吸引人,这样比如用户点击了一个按钮去触发某些事件的处理,可以弹出一个进 ...

  8. [转]TimeQuest之delay_fall clock_fall傻傻分不清楚

    这篇我想分享一个之前在用TimeQuest约束双边沿模块的input delay时犯得一个错误,有人看了可能会觉得傻傻的,什么眼神,falling delay和 falling clk怎么会分不清呢, ...

  9. rsync服务搭建--2018.5.8 [优化后最终版]

    2018年5月8日 22:09:38 第一步配置基础环境(按照自己的规划配置并非每人的环境都一致) 第一台服务器(RSYNC服务器): rsync外网地址:10.0.0.41  rsync内网地址:1 ...

  10. 3人从小公寓创业,到世界最大引擎公司,Unity创始人谈14年...

    Unity创始人David Helgason出席了5月11 - 13日在上海举办的Unite 2017 Shanghai,并在大会期间接受了游戏陀螺的专访,动情地讲述了这14年来从3人在公寓创业,到成 ...