Learn C day 5th (printf function, scanf function)

Source: Internet
Author: User

First,format

1.%d format

%[-]d left Justified
%[m]d Output with the field width specified by M, the number of bits in the data is less than M, the left-fill space
%[0m]d Output with the field width specified by M and the number of bits in the data is less than M, then the left 0
%[l]d Output Long Integer
%[-m]d left-aligned, ignoring 0 or spacesintnum = +;
printf"%-d", num);// +
printf"\n%10d", num);// +
printf"\n%010d", num);//0000001000
printf"\n%ld", num);// +
printf"\n%-10d", num);// +

2.%c format%[m]c with the field width specified in M, the number of bits in the data is less than M, and the Left fill space
%[0m]c Output with the field width specified by M, the number of bits in the data is less than M, and the left is 0
%[-m]c left-aligned, ignoring 0 or spaces printf ("\n%-c",'a');//a
printf"\n%5c",'a');//a
printf"\n%05c",'a');//0000a
printf"\n%-05c",'a');//a

3.%s format

%[-m]s output string in M column, left justified
%[m]s output string in M-column, right-aligned, left-fill space
%[0m]s output string in M-column, right-aligned, left-side complement 0
%[m.n]s the first n characters of the output string, in M-column, right-justified
%[.n]s First n characters of the output string, left justified

Charstr[ -] ="Hello";
printf"\n%-s", str);//Hello
printf"\n%10s", str);//Hello
printf"\n%010s", str);//00000hello
printf"\n%-10s", str);//Hello
printf"\n%.3s", str);//Hel
printf"\n%10.3s", str);//Hel
printf"\n%010.3s", str);//0000000hel

4.%f format

%[m][.n]f output data for M-column, decimal for n-bit, right-justified
%[-][m][.n]f output data is a total of M columns, fractional n bits, left justified

floatnum =10.356;
printf"\n%-8.2f", num);//10.36
printf"\n%08.2f", num);//00010.36

5.EGXC can be capitalized

//e Capital X output letter uppercase for EG output
printf"%d",Ten);//Ten
printf"\n%d",Ten);//D
printf"\n%c", $);//A
printf"\n%c",'A');//A
printf"\n%s","ABC");//ABC
printf"\n%s","ABC");//Empty
printf"\n%f",100000.0);//100000.000000
printf"\n%f",100000.0);//Empty
printf"\n%e",100000.0);//1.000000e+005
printf"\n%e",100000.0);//1.000000E+005
//g How to streamline how to output
printf"\n%g",100000.0);//100000
printf"\n%g",100000.0);//100000
printf"\n%g",1000000000.5555);//1E+009
printf"\n%o",Ten);// A
printf"\n%o",Ten);//O
printf"\n%x",Ten);//a
printf"\n%x",Ten);//A

6.printf function Considerations

1> If you want to output ", Need \" escape

2> If output% is required,

3>: Extra output items are not output when the format character is less than the output item

4>: When the format character is more than the output item, the result is an indeterminate value

printf"%\"");//"
printf"\n%%%");//%
printf"\n%d,%d",Ten, -, -);//10,20
printf"\n%d,%d,%d", -, -);//20,30,0

7.scanf function Considerations

1> additional format characters

* The input value is not assigned to the corresponding variable

L for input of long-shaped (32-bit platform input effect and%d) and double-precision real data

m specifies the width of the data and automatically captures the required length of data.

2>: Cannot specify precision when entering data

ii. File redirection

Reads the instruction from the text where the instruction is written and saves the result in another text.

cmd--> path-->exe file < input file name with instruction .txt> output TXT file name. txt


intCounts//number of
Charinstruct[ -];//instruction
scanf"%d%s", &counts, instruct);
Charstr[ -];
sprintf (str,"for/l%%i in (1,1,%d) do%s", counts,instruct);
System (STR);
System"Pause");

1. Enter tasklist in Notepad and save as Input.txt

Enter the path and EXE file name in cmd <input.txt>output.txt

Learn C day 5th (printf function, scanf function)

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.