Detailed description of C language sequence programming (functions such as scanf, printf, getchar, and putchar)

Source: Internet
Author: User

I. C language statement Overview:

C statements are classified into the following five types:
<1>: expression statement-> an expression statement is composed of a semicolon (;) added to the expression.
<2>: The function call statement is composed of the function name, actual parameter, and semicolon.
<3>: control statement->
Conditional judgment statement: if statement and switch statement;
Loop statement: do while statement, while statement, for statement;
Conversion statement: break statement, goto statement, continue statement, and return statement.
<4>: Compound statement: A statement consisting of multiple statements in parentheses {} is called a compound statement.
Example: {x = y + z;
A = B + c;
Printf ("% d", x, );
}
<5>: empty statement: A statement consisting of only semicolons (;) is called an empty statement. A null statement is a statement that does not run. The program hollow statement can be used as an empty loop body. Example: while (getchar ()! = '\ N ');

1. Explanation of C language assignment statements:
Variable = (variable = expression)
[Cpp]
A = B = c = d = e = 10

According to the right combination of the value assignment operator, it is actually equivalent:
E = 5;
D = e;
C = d;
B = c;
A = B;
2. putchar function (character output function)
Putchar is a character output function that outputs a single character on the display.
The general form is:
Putchar (character variable)
3. getchar function (Keyboard Input Function)
The getchar function is used to input a character from the keyboard.
The general form is:
Getchar ();
The input character is usually assigned to a character variable to form a value assignment statement, such:
Char c;
C = getchar ();
4. Explanation of the C language output function printf ()
The printf function is a standard library function. Its function prototype is in the header file "stdio. h. However, as a special case, you do not need to include stdio. H files before using the printf function. The printf function is generally called in the following format:
Printf ("format control string", output table column)

Format String
In Turbo C, the format string is generally of the [flag] [minimum output width] [. Precision] [length] type. The items in square brackets [] are optional.
The meanings of each item are described as follows:
Type: A type character is used to indicate the type of output data. Its format characters and meanings are shown in the following table:
Meaning of Characters in the format
D. Output signed integers in decimal form (positive numbers do not output symbols)
O outputs unsigned integers in octal format (no prefix 0 is output)
X and X output unsigned integers in hexadecimal format (no output prefix Ox)
U outputs an unsigned integer in decimal format
F outputs Single and Double Precision Real Numbers in decimal form
E, E outputs Single and Double Precision Real Numbers in exponential form
G and G are single output and double-precision real numbers with a shorter output width in % f or % e.
C Outputs a single character
S output string
 
Flag: it contains four types of characters:-, +, #, and space. The following table lists their meanings:
Biao Zhiyi
-The result is left aligned with a space on the right.
+ Output symbol (positive or negative)
If the output value of a space parameter is positive, it is preceded by a space.
# It has no effect on Class c, s, d, and u; it has a prefix of Class o in the output; it has a prefix of Class x in the output; it has a prefix of 0x in the output; and has a prefix of Class e, g, class f indicates the decimal point only when the result has decimal places.
5. Explanation of Input Function scanf () in C language format
Scanf ("format control string", address table column );
The format control string serves the same purpose as the printf function, but cannot display non-format strings, that is, it cannot display the prompt string. The address column lists the addresses of each variable. The address is composed of the address operator "&" and the variable name.

[Cpp]
Scanf ("% d", & a, & B, & c );
The format string is generally in the format of % [*] [input data width] [length] type.
The item with square brackets [] is any option. The meaning of each item is as follows:
Type: indicates the type of the input data. The format characters and meanings are shown in the following table.
Meaning of Characters in the format
D. Input a decimal integer.
O input an octal integer
X Input hexadecimal integer
U input unsigned decimal integer
F or e input real number (in decimal or exponential form)
C. Enter a single character.
S input string
 
"*": Indicates the input item. If the input item is read, the corresponding variable is not assigned, that is, the input value is skipped. For example, scanf ("% d % * d % d", & a, & B );. When the input is: 1 2 3, 1 is assigned to a, 2 is skipped, and 3 is assigned to B.
Width: Specify the input width (number of characters) with a decimal integer ).
For example:
Scanf ("% 5d", & );
Input 12345678 to assign only 12345 to variable a, and the rest is truncated.

Another example:
Scanf ("% 4d % 4d", & a, & B );
Input 12345678 will assign 1234 to a and 5678 to B.
Length: The length format is l and h, and l indicates the input of long integer data (such as % ld) and Double Precision Floating Point Number (such as % lf ). H indicates the input of short integer data.
Note the following when using the scanf function:
The scanf function does not have precision control. For example, scanf ("% 5.2f", & a); is invalid. You cannot use this statement to enter a real number with two decimal places.
The variables must be given in scanf. If the variable name is given, an error occurs. For example, scanf ("% d", a); is invalid. It should be changed to scnaf ("% d", &.
When multiple numeric values are input, if there are no non-formatted characters in the format control string for the interval between the input data, spaces can be used, TAB or press enter for the interval. When C is compiled with spaces, tabs, carriage return, or illegal data (for example, when "% d" is input, "12A" is invalid data), the data ends.
When inputting character data, if the format control string contains no more than the format character, it is considered that all input characters are valid characters.
For example:
Scanf ("% c", & a, & B, & c );
Input d e f, then assign 'D' to a, ''to B, and 'e' to c. Only when the input is def can 'D' be assigned to a, 'E' to B, and 'F' to c.

If spaces are added to the format control, for example:
Scanf ("% c", & a, & B, & c );
A space can be added between the input data.


Author: grassroots2011

Related Article

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.