C Language Learning Tutorial Chapter III-C language programming preliminary (2)

Source: Internet
Author: User
Tags control characters expression function prototype printf requires truncated

Second, format string

The general form of a format string in Turbo C is: [flag] [Output minimum width] [. precision] [length] type where the items in brackets [] are optional. The meaning of the items is described below:
1. The type type character is used to represent the type of output data, and its format character and meaning are shown in the following table:
Format character format character meaning of output type
D output signed integers in decimal form (positive not output symbols)
o Output unsigned integers in eight (no output prefix O)
x output unsigned integer in 16 (do not output prefix ox)
U output unsigned integers in decimal form
F output single, double real numbers in decimal form
E output single and double precision real numbers in exponential form
g to output single and double precision real numbers in shorter output widths of%f%e
C Output Single character
S output string
2. Logo
The symbol character is-, +, #, the space four kinds, its meaning is shown in the following table:
Flag format character flag meaning
-the result is left-aligned, the right blank
+ Output symbol (plus or minus) space output value is positive with a space, negative with minus sign
# has no effect on the C,s,d,u class; for O class, before the output is added
Suffix o to x class, prefix output with 0x; give a decimal point to the E,g,f class when the result has a decimal number
3. Output minimum width
A decimal integer to represent the minimum number of digits for the output. If the actual number is more than the defined width, the actual number of digits is output, if the actual number is less than the defined width, a space or 0 is filled.
4. Precision
The precision format character is "." The beginning followed by a decimal integer. The meaning of this item is: If the number is output, the number of digits, if the output is a character, the number of output characters, if the actual number of digits is greater than the defined number of precision, then truncate the more parts.
5. Length
The length format character is h,l Two, h means output by a short integer, L represents a long integer output.
void Main () {
int a=15;
float b=138.3576278;
Double c=35648256.3645687;
Char d= ' P ';
printf ("a=%d,%5d,%o,%x\n", a,a,a,a);
printf ("b=%f,%lf,%5.4lf,%e\n", b,b,b,b);
printf ("c=%lf,%f,%8.4lf\n", c,c,c);
printf ("d=%c,%8c\n", d,d);
} a<--15
b<--138.3576278
c<--35648256.3645687
d<--' P '

Main ()
{
int a=29;
float b=1243.2341;
Double c=24212345.24232;
Char d= ' h ';
printf ("a=%d,%5d,%o,%x\n", a,a,a,a);
printf ("b=%f,%lf,%5.4lf,%e\n", b,b,b,b);
printf ("c=%lf,%f,%8.4lf\n", c,c,c);
printf ("d=%c,%8c\n", d,d);
}
In this example, the value of integer variable A is output in four formats in line seventh, where "%5d" requires an output width of 5, while a value of 15 has two digits to fill three spaces. The value of the real type B is output in the eighth line in four different formats. The output of the "%f" and "%LF" formats is the same, stating that the "L" character has no effect on the type "F". "%5.4LF" Specifies the output width of 5, the precision is 4, because the actual length of more than 5 should be based on the actual number of digits, the number of decimal places more than 4 bits were truncated. The Nineth line outputs double real numbers, and "%8.4LF" has truncated more than 4 bits due to the specified precision of 4 digits. Line tenth outputs a character D, where "%BC" specifies an output width of 8 so that 7 spaces are added before the output character p.

Another issue to note when using the printf function is the order of evaluation in the output table column. Different compilation systems may not be the same, from left to right, or from right to left. Turbo c is pressed from right to left. If the example 2.13 is rewritten as follows:
void Main () {
int i=8;
printf ("%d\n%d\n%d\n%d\n%d\n%d\n", ++i,--i,i--, i++,-i--);
} i<--8

This program, compared with example 2.13, simply changes multiple printf statements to a printf statement output. But the results can be seen to be different. Why is the result different? It's because the printf function evaluates the values in the output table from right to left. In the formula, first evaluates to the last "-i--", the result is-8, then I 1 is 7. Then the "-i++" item is worth 7, then I will increase 1 after 8. Again, the "i--" item is worth 8, then I subtract 1 from the 7. Then the "i++" item is 7, then I will increase by 1 and then 8. Then the "I" Item, I first from minus 1 output, output value of 7. Finally, the output table column of the first "++i", at this time I since the 1 output 8. However, it must be noted that the order of evaluation is from right to left, but the output order is left to right, so the result is the output.

Character output function

Putchar function

The Putchar function is a character output function that outputs a single character on the monitor. The general form is: Putchar (character variable) for example:
Putchar (' A '); Output Capital Letter A
Putchar (x); The value of the output character variable X
Putchar (' \ n '); The newline pair control characters perform control functions and are not displayed on the screen. You must use the file to include the command before using this function:
#include <stdio.h>
void Main () {
Char a= ' B ', b= ' o ', c= ' k ';
Putchar (a);p Utchar (b);p Utchar (b);p Utchar (c);p Utchar (' t ');
Putchar (a);p Utchar (b);
Putchar (' \ n ');
Putchar (b);p Utchar (c);
}

Data Entry Statement

The C language data input is also completed by the function statement. This section describes the functions scanf and getchar that enter data from a standard input device-the keyboard. The scanf function scanf function is called the format input function, which is to enter data into a specified variable from the keyboard in a user-specified format.

The general form of scanf function

The scanf function is a standard library function whose function prototype is the same as the printf function in the header file "Stdio.h", and the C language also allows you to not include stdio.h files before using the scanf function. The general form of the scanf function is: scanf ("Format control string", Address table column); The format control string acts the same as the printf function, but cannot display the unformatted string, which means the prompt string cannot be displayed. Address Table column gives the address of each variable. The address is made up of the address operator "&" followed by the variable name. For example, &a,&b represents the address of variable A and variable B, respectively. This address is the address that compiles the system to assign a,b variables in memory. In the C language, the concept of address is used, which is different from other languages. The value of the variable and the address of the variable should be distinguished from the two different concepts. The address of the variable is assigned by the C compilation system, and the user does not have to care about the specific address. The relationship between the address of the variable and the value of the variable is as follows: &a--->a567 A is the variable name, 567 is the value of the variable, and &a is the address of the variable A. Assign a value to a variable in an assignment expression, such as: a=567 is the variable name on the left of the assignment number, the address cannot be written, and the scanf function is essentially assigning the variable, but requires the address of the variable to be written, such as &a. The two are different in form. & is an address operator, &a is an expression whose function is to evaluate the address of a variable.
void Main () {
int a,b,c;
printf ("Input a,b,c\n");
scanf ("%d%d%d", &a,&b,&c);
printf ("a=%d,b=%d,c=%d", a,b,c);
}
Pay attention to & usage!
In this example, because the SCANF function itself cannot display a hint string, it first prints a prompt on the screen with a printf statement, asking the user to enter a value of a, B, and C. Execute the scanf statement, exit the TC screen and wait for the user to enter the user screen. When the user enters 7, 8, 9 and presses the ENTER key, the system returns the TC screen. In the format string of a scanf statement, because there is no interval between "%d%d%d" as input for unformatted characters, use more than one space or enter key as the interval between each two input.
such as: 7 8 9
Or
7
8
9

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.