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\

时间: 2024-10-14 09:23:51

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_Sheet 介绍 本文描述了一种恰当地使用输出转码或者转义(encoding or escaping)防御XSS攻击的简单积极模式. 尽管存在巨量XSS攻击方式,遵守一些简单的规则能够彻底防住这类严重的攻击. 本文不探讨XSS攻击的商业和技术影响. reflected and stored XSS 可以

[Z] 各种开发相关的CHEAT SHEET

你是否会经常忘记一些CSS中的函数名或是一些属性名,那个时候,你一定觉得,如果手边有一个“小抄”(Cheat Sheet)就好了.当然,这个“小抄”不是给你作弊用的,这个“小纸条”就是可以让你马上知道那个你最想知道的东西.这个“小抄”上也不需要有所有的东西,就需要那些经常用的就行了.现在,网上有很多这样的“小抄”,它们可能是PDF格式的,可能是PNG格式的,你可以很方便地把其打印出来(可以打印得很小),然后贴在你的电脑旁,一但需要,瞟一眼就可以了,这对于我们的工作是相当方便的. 之前,酷壳也有两

IOS Application Security Testing Cheat Sheet

IOS Application Security Testing Cheat Sheet [hide] 1 DRAFT CHEAT SHEET - WORK IN PROGRESS 2 Introduction 3 Information gathering 4 Application traffic analysis 5 Runtime analysis 6 Insecure data storage 7 Tools 8 Related Articles 9 Authors and Prima

XSS Filter Evasion Cheat Sheet 中文版【转】

译者注: 翻译本文的最初原因是当我自己看到这篇文章后,觉得它是非常的价值.但是这么著名的一个备忘录却一直没有人把它翻译成中文版.很多人仅仅是简单的把文中的各种代码复制下来,然后看起来很刁的发在各种论坛上,不过你要真去认真研读这些代码,就会完全不知所云了.原因是这篇文章最精华的部分是代码的解释而非代码本身. 一方面为了自己学习,一方面也想让更多国内的xss爱好者去更方便的阅读本文.所以虽然我本身英语很烂,xss技术也很烂,但还是去翻译了这篇文章.当然这也导致最后翻译出来的文章晦涩难懂.不知所云.这

XSS Filter Evasion Cheat Sheet 中文版

前言 译者注: 翻译本文的最初原因是当我自己看到这篇文章后,觉得它是非常有价值.但是这么著名的一个备忘录却一直没有人把它翻译成中文版.很多人仅仅是简单的把文中的 各种代码复制下来,然后看起来很刁的发在各种论坛上,不过你要真去认真研读这些代码,就会完全不知所云了.原因是这篇文章最精华的部分是代码的解释而非代 码本身. 一方面为了自己学习,一方面也想让更多国内的xss爱好者去更方便的阅读本文.所以虽然我本身英语很烂,xss技术也很烂,但还是去翻译了这篇文 章.当然这也导致最后翻译出来的文章晦涩难懂.

[转]Swift Cheat Sheet

原文:http://kpbp.github.io/swiftcheatsheet/ A quick cheat sheet and reference guide for Apple's Swift language. This guide intends to cover all the key features of Swift, including Strings, Arrays, Dictionaries and Flow Control. Swift is a new programm

MySQL SQL Injection Cheat Sheet

MySQL SQL Injection Cheat Sheet Some useful syntax reminders for SQL Injection into MySQL databases- This post is part of a series of SQL Injection Cheat Sheets.  In this series, I've endevoured to tabulate the data to make it easier to read and to u

Interactive Learning Document Format Reference

Interactive Learning Document Format Reference An interactive learning document is a kind of playground that lets the reader interact with code mixed with rich HTML content. Its file format is a package that contains Swift source code, HTML content,

转:PostgreSQL Cheat Sheet

PostgreSQL Cheat Sheet CREATE DATABASE CREATE DATABASE dbName; CREATE TABLE (with auto numbering integer id) CREATE TABLE tableName ( id serial PRIMARY KEY, name varchar(50) UNIQUE NOT NULL, dateCreated timestamp DEFAULT current_timestamp ); Add a pr