Printf () usage Summary

Source: Internet
Author: User
Tags value of pi

/* Use the conversion specifier */
# Include <stdio. h>
# Include <string. h>
# Define PI 3.141593 // The first method to create a symbolic constant
# Define pages 93179962
# Define blurb "authentic imtation! "
# Define words 65618
# Define pages2 336
Int main (void)
{
Printf ("\ n \ t/* use the conversion specifier */\ n ");
Int number = 5;
Int Pc = 2*6;
Float expresso = 13.5;
Int cost = 3100;
Printf ("\ n ***** \ tThe % d CEOs drank % F cups of expresso. \ t ******* \ n ",
Number, Expresso); // print the variable
Printf ("_____ the value of PI is % F. _______ \ n", Pi); // print a constant
Printf ("\ afarewell! Thou art too dear for my possessing. \ n "); // print the statement
Printf ("% C % d \ n \ t", '$', 2 * cost); // print character constants and penalty expressions
 
Printf ("Only % d % of Sally's gribbles were edible. \ n", PC); // print the % number. Note that it is two %
 
/* Field width */
Printf ("\ t/* field width */\ n ");
Printf ("\ t * % d * \ n", pages); // fields with the same integer width
Printf ("\ t * % 2D * \ n", pages);/* It indicates to generate a field with a width of 2
The integer has four digits, so it is automatically extended to adapt to the number length */
 
Printf ("\ t * % 10d * \ n", pages);/* this will generate a field as wide as 10 spaces,
Therefore, there are seven white spaces and three numbers between the asterisks,
And the number is on the right side of the entire field */
Printf ("\ t * % + 10d * \ n", pages);/* With the plus sign */
Printf ("\ t * %-10D * \ n", pages);/* this will generate a field that is as wide as 10 spaces,
Therefore, there are seven white spaces and three numbers between the asterisks,
And the number is on the left side of the entire field */
 
/* Combination of floating point numbers */
Printf ("\ n \ t/* combination of some floating point numbers */\ n ");

Const double rent = 3852.99; // The second method for creating a symbolic constant
 
Printf ("\ t * % F * \ n", rent);/* Note % F default format */
Printf ("\ t * % E * \ n", rent);/* Note % E default format */
Printf ("\ t * % 4.2f * \ n", rent );
Printf ("\ t * % 3.1f * \ n", rent); // rounding
Printf ("\ t * % 10.3f * \ n", rent );
Printf ("\ t * % 10.3e * \ n", rent); // four-color five-in-one
Printf ("\ t * % + 4.2f * \ n", rent );
Printf ("\ t * % 010.2f * \ n", rent);/* 0 indicates that the leading zero is generated to fill the entire field with the result
Note: The first 0 in 010 is a sign,
Specify the field width for the remaining 10 characters */
/* Examples of some format labels */
Printf ("\ n \ t/* examples of some format signs */\ n ");
 
Printf ("% x % # x \ n", 31,31, 31 );
Printf ("** % d ** \ n", 42 );
Printf ("** % 5d ** % 5.3d ** % 05d ** % 05.3d ** \ n", 6, 6 );
// If the 0 sign and the precision specifier sign appear at the same time, the 0 sign will be ignored.

/* Format the string */
Printf ("\ n \ t/* examples of some format signs */\ n ");
Printf ("/% 2 S/\ n", blurb );
Printf ("/% 24 S/\ n", blurb );
Printf ("/% 24.5 S/\ n", blurb );
Printf ("/%-24.5 S/\ n", blurb );

/* Application */
Printf ("\ n/* application */\ n ");

Char name [40];
Float cash;
Printf ("Please input the name and cash \ n ");
Scanf ("% S % F", name, & cash );
Printf ("the % s family just may be % C %. 2f richer! \ N ", name, '$', cash );

/* Some unmatched integer conversions */
Printf ("\ n \ t/* Some unmatched integer conversions */\ n ");

Short num = pages2;
Short mnum =-pages2;

Printf ("Num as short and unsigned short: % HD % Hu \ n", num, num );
Printf ("-num as short and unsigned short: % HD % Hu \ n", mnum, mnum );
Printf ("Num as short and char: % d % C \ n", num, num );
Printf ("words as int, short and char: % d % HD % C \ n ",
Words, words, words );


/* Unmatched floating point conversion */
Printf ("\ n \ t/* unmatched floating point conversion */\ n ");

Float n1 = 3.0;
Double n2 = 3.0;
Long N3 = 2000000000;
Long N4 = 1234567890;

Printf ("%. 1E %. 1E %. 1E %. 1E \ n", N1, N2, N3, N4 );
Printf ("% LD \ n", N3, N4 );
Printf ("% LD \ n", N1, N2, N3, N4 );
// Byte loss


/* -- Discovers the return value of printf */
Printf ("\ n \ t/* -- the return value of printf () */\ n ");
Int bph2o= 212;
Int RV;

Rv = printf ("% d F is water's boiling point. \ n", bph2o );
Printf ("the printf () function printed % d character. \ n ",
RV );

/* Three methods for printing long strings */

Printf ("\ n \ t/* three methods for printing long strings */\ n ");
Printf ("here's one way to print ");
Printf ("A long string. \ n ");
Printf ("Here's another way to print \
Long String \ n ");
Printf ("here's newest way to print"
"Long string. \ n ");

Return 0;
}
 

Printf () usage Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.