Formated printf

View previous topic View next topic Go down

Formated printf

Post by BIT0112-Rokon on Wed Jun 23, 2010 3:39 am

Sometime we need to have formatted output while using prinf in C. It is prety simple. In this text, I'll try to
show you the way of formating your output. So first we need to get instroduced with formatting identifier which are given below:

d int, signed base 10
i int, signed base 10
o int, unsigned base 8, no leading 0
x int, unsigned base 16, abcdef with no leading 0x
X int, unsigned base 16, ABCDEF with no leading 0X
u int, unsigned base 10
s char *, nul terminated sequence of characters
c int, single character
f double, notation [-]mm.dd
e double, notation [-]m.dde[+/-]xx
E double, notation [-]m.ddE[+/-]xx
g double, %e if exponent is less than -4 or >= the precision, %f otherwise
G double, %E if exponent is less than -4 or >= the precision, %f otherwise
p void *, implementation dependent representation
n int *, the number of characters written so far. Assigned to the argument
% print a % character

And all identifiers have upto 6 parts which are : %, Flags, Mininmum Field width, Precision, Maximum field width and argument type.
And they must be used in the order shown.

Now lets get introduced with flags
-, used for left adjustment
+, used to print the number with a sign.
0, used to print the number leading with zero
#, there are various use of it-
%#o (Octal) used to print with prefix 0.
%#x (Hex) used to print with prefix 0x
%#X (Hex) used to print with prefix 0X
%#e Always show the decimal point like 1.000000e+00.
%#E Always show the decimal point like 1.000000E+00.
%#f Always show the decimal point like 1.000000.
%#g Always show the decimal point trailing zeros not removed.
%#G Always show the decimal point trailing zeros not removed.

At the same time more than one flag can be used like
main(){
int number = 10;
printf(" %-#10x \n", number);
}

Now lets get introduced with Minimum field width. By default the width of a field is minimum to hold the data, but if we want, we can increase the width of a field. here is an example -
int number = 5;
printf("%4d\n", number);

the data 5 will print after 4 space.

now lets go with precision. The Precision takes different meanings for the different format types.
Float precision:
for say
%8.2f
This says you require a total field of 8 characters, within the 8 characters the last 2 will hold the decimal part.
Maximum field width:
%4.8s
Specifies a minimum width of 4 and a maximum width of 8 characters. If the string is greater than 8 characters, it will be cropped down to size.

Now lets see a example-
Code:

#include <stdio.h>

int main ( void )
{
        printf ( "%5d\n", 123 );   
        printf ( "%*d\n", 5, 123 ); 
        printf ( "%+05d\n", 123 );   
        printf ( "%x\n", 123U );   
        printf ( "%#x\n", 123U );   
        printf ( "%#X\n", 123U );   
        printf ( "%-10.2f\n", 12.3 );
        printf ( "%10.2f\n", 12.3 ); 
        printf ( "%lu\n", 123UL );   
        printf ( "%s\n", "Hello world" );
        printf ( "%c\n", 'R' );     

  return 0;
}


Reference :
http://www.cprogramming.com/
http://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/FUNCTIONS/format.html
http://www.codingunit.com/printf-format-specifiers-format-conversions-and-formatted-output
http://www.cppreference.com/wiki/c/io/printf

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

Re: Formated printf

Post by BIT0103-Ovid on Wed Jun 23, 2010 4:35 pm

nice post. will be really helpful for the beginners. keep up the good work
avatar
BIT0103-Ovid
Release Candidate
Release Candidate

Course(s) :
  • BIT

Blood Group : O-
Posts : 150
Points : 261

View user profile

Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum