Format output function printf () __ function

Source: Internet
Author: User
Tags clear screen

The call format for the printf () function is:printf ("< format string >", < Parameter table >);

Note: the format specifier must match the type of the data. For example, output UINT32 type must use%u, and UINT64 must use%llu, otherwise easy to appear inexplicable print value.

1. Format specifier %d decimal signed integer

%u decimal unsigned integer

%x, int without symbol in hexadecimal notation

%f floating-point numbers

%s string

%c single character

Floating point numbers in the form of%e exponent

%0 An integer that is unsigned as octal

%g automatic selection of appropriate notation

The value of the%p pointer

1.1 You can insert a number between "%" and a letter to indicate the maximum field width.

For example:

The%3d represents an output 3-bit integer and is not 3-bit right-aligned.
%9.2F represents a floating-point number with an output field width of 9, where the decimal digit is 2, the integer digit is 6, the dot is one digit, and the 9-bit right alignment is not enough.
%8s represents a 8-character string that is not 8 words Fu Yi aligned.
Note:

(1) If the length of the string, or the number of integer digits exceeds the field width of the description, it will be output according to its actual length.

(2) for floating point numbers, if the integer partial digits exceed the indicated integer digit width, the actual integer digit output will be applied; If the fractional number of digits exceeds the indicated decimal width, the output is rounded by the width of the description.

(3) if you want to add some 0 before the output value, you should add a 0 to the presence width. For example,%04d indicates that when outputting a value less than 4 digits, the front complement of 0 will make the total width 4 bits.

(4) If you use floating-point numbers to represent the output format for characters or integers, the digits after the decimal point represent the maximum width, and the digits before the decimal point represent the minimum width. For example,%6.9s represents a string that is not less than 6 and not greater than 9. If greater than 9, the contents of the 9th character will be deleted.

1.2 You can add a lowercase letter L between "%" and a letter to indicate the length of the output.

For example:

%LD indicates output signed Long integer

%llu represents the output of an unsigned long long integer

%LF indicates output double floating-point numbers

1.3 You can control the output to the left or right alignment, that is, "%" and the letter between the addition of a "-" number can be Note that the output is left-aligned or right-aligned.

For example:

%-7D indicates output 7-bit integer left-aligned

%-10s indicates output 10 characters left-aligns alignment

2. Some special specified characters

\ n Line Change
\f Clear screen and change page
\ r Carriage Return
\ t Tab character
\XHH represents an ASCII code with a 16-in representation,
where HH is 1 to 2 16 in number

#include <stdio.h> #include <string.h> int main () {char C, s[20], *p; 
int a=1234, *i; 
float f=3.141592653589; 
Double x=0.12345678987654321; 
P= "How to Do"; 
strcpy (S, "Hello, comrade"); 
*i=12; 
c= ' \x41 '; printf ("a=%d\n", a); /* Results Output decimal integer a=1234*/printf ("a=%6d\n", a); /* Results output 6 decimal digits a= 1234*/printf ("a=%06d\n", a); /* Results output 6 decimal digits a=001234*/printf ("a=%2d\n", a); /*a more than 2 digits, output a=1234*/printf ("*i=%4d\n", *i) by actual value; /* Output 4-bit decimal integer *i= 12*/printf ("*i=%-4d\n", *i); /* Output left-aligned 4-bit decimal integer *i=12*/printf ("i=%p\n", I); /* Output Address i=06e4*/printf ("f=%f\n", f); /* Output floating-point number f=3.141593*/printf ("f=6.4f\n", f); /* Output 6 digits of which floating-point number 4 digits after the decimal f=3.1416*/printf ("x=%lf\n", X); /* Output long floating-point number x=0.123457*/printf ("x=%18.16lf\n", x);/* Output 18-bit long floating-point number 16 digits after the decimal point x=0.1234567898765432*/printf ("c=%c\n", c); /* Output character c=a*/printf ("c=%x\n", c); /* The ASCII code value of the output character c=41*/printf ("s[]=%s\n", s); /* Output array string S[]=hello, comrade*/printf ("s[]=%6.9s\n", s);/* Output up to 9 characters of string S[]=hello, co*/printf ("s=%p\n", s); /* Output array string first character address s=ffbe*/printf ("*p=%s\n ", p); /* Output pointer string p=how do you do*/printf ("p=%p\n", p); 
/* The value of the output pointer p=0194*/getch (); 
RETUNR 0;  }





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.