CS157: Intro to C, Part II

Spring 2018

Printf

printf formats and examples

Data typeFormatDescriptionFormatDescriptionFormatDescription
short%hddecimal%hooctal%hxhexadecimal
int%ddecimal%ooctal%xhexadecimal
long%lddecimal%looctal%lxhexadecimal
long long%llddecimal%llooctal%llxhexadecimal
unsigned short%hudecimal%hooctal%hxhexadecimal
unsigned%udecimal%ooctal%xhexadecimal
unsigned long%ludecimal%looctal%lxhexadecimal
unsigned long long%lludecimal%llooctal%llxhexadecimal
float%ffixed-point%escientific notation%g%e or %f
double%lffixed-point%lescientific notation%lg%le or %lf
long double%Lffixed-point%Lescientific notation%Lg%Le or %Lf
char%ccharacter
array of char%sstring
CodeOutput (“¤” means a space)
printf("%5d", 12)¤¤¤12
printf("%2d\n", 8675309)8675309
printf("%-5d", 12)12¤¤¤
printf("%05d", 12)00012
printf("%f", 12.345)12.345000
printf("%.1f", 12.345)12.3
printf("%6.1f", 12.345)¤¤12.3
printf("%-6.1f", 12.345)12.3¤¤
printf("%5o", 20)¤¤¤24
printf("%05o", 20)00024
printf("%3c", 'X')¤¤X
printf("%-3c", 'X')X¤¤
printf("%s", "abc")abc
printf("%5s", "abc")¤¤abc
printf("%-5s", "abc")abc¤¤
printf("%.2s", "abc")ab
printf("%-5.2s", "abc")ab¤¤¤

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2013-12-22T17:08

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building