"C language" 05-printf and scanf functions

Source: Internet
Author: User

First, the printf function

This is a function declared in stdio.h, so it is necessary to include # include <STDIO.H> before use, which can be used to output data to a standard output device (such as a screen)

1. Usage

1> printf (String)

printf ("Hello, world!");

The output is:

2> printf (string, format character parameter)

1//Use constants for parameter 2 printf ("My Age is%d\n", 26); 3 4//You can also use the variable 5 int age = 17;6 printf ("My was%d", age);

* Format symbol%d means output an integral type in signed decimal form, 26 in the format character parameter and age in place of%d.

* \ n is an escape character in the 2nd line of code, which means line wrapping, so the first sentence "My age is 26" will be wrapped first, then output "my age is 27"

Output Result:

* If you remove the \ n in line 2nd, this will be the effect

Output Result:

Summary: The number of formatting characters in the left string must be the same as the number of the right-hand format characters, and the type of the format character determines the type of the format character parameter, for example,%d, which indicates that the corresponding format character parameter must be an integral type.

Let me give you an example:

printf ("My age was%d and no is%d", 27, 1);

Output Result:

2. Commonly used format characters and their meanings

3. Format characters can also add some fine-grained formatting controls1> Output Width

* Let's look at the default integer output first

printf ("The price is%d.", 14);

Output (note that there is a point later):

* If I change%d to%4d:

printf ("The Price is%4d.", 14);

Output: You will find that the distance between "is" and "14" has been pulled.

%4D means that the output width is 4, and the width of "14" is 2, so more than 2 width, the extra width will be filled on the left with a space, so you will see "14" left more than 2 spaces, if the actual value width is larger, such as with%4d output width of 6 "142434", That will 6来 the output according to the actual numeric width.

printf ("The Price is%4d.", 142434);

Output: The output width of "142434" is 6

* If you switch to%-4D

printf ("The Price is%-4d.", 14);

Output: You will find "14" followed by "." The distance was pulled.

%-4D indicates that the output width is 4, if the width is larger than the actual value, the extra width is filled with a space on the right, and if 4 is smaller than the actual value, it is output according to the actual value width.

2> number of decimal digits for floating-point numbers

* Let's first look at the default floating-point output

printf ("My height is%f", 179.95f);

Output: The default is 6 decimal places

* If you only want to output 2 decimal places, replace the%f with%.2f

printf ("My height is%.2f", 179.95f);

Output Result:

* Of course, you can set both the output width and the number of decimal digits

printf ("My height is%8.1f", 179.95f);

Output: Output width is 8, 1 decimal places reserved

back to topSecond, scanf function

This is also a function declared in stdio.h, so you must include # include <stdio.h> before use. When you call the scanf function, you need to pass in the address of the variable as a parameter, and the scanf function waits for the standard input device (such as the keyboard) to input data and assigns the input data to the variable that corresponds to the address

1. Simple usage
1 printf ("Please input your-age:"); 2 3 int age;4 scanf ("%d", &age); 5 6 printf ("Your-age is%d.", age);

* Run the program, execute the 1th line of code, the console will output a sentence message:

* When the scanf function is executed to line 4th, it waits for the user's keyboard input and does not execute the code backwards. The 1th parameter of scanf is "%d", stating that the user is required to enter an integer in 10 binary form. Note here that the 2nd argument of scanf is not the age variable, but the address of the age variable &age,& is an address operator in the C language that can be used to get the address of the variable.

* Then we can enter a 8 after the prompt message:

(due to Xcode's own problem, we can only enter the width of 1 data in the console, if you want to enter a width greater than 1, such as input 27, you can copy 27 from somewhere else and then paste to the console)

* After the input is complete, hit the ENTER key to tell the scanf function that we have already entered, the SCANF function will assign the input 8 to the age variable

* After the scanf function is assigned, the code is executed backwards, and when the 6th line is executed, the controller outputs:

2. Other uses1> uses the scanf function to receive 3 values, where each value is separated by an underscore-
1 int A, B, c;2 scanf ("%d-%d-%d", &a, &b, &c); 3 4 printf ("a=%d, b=%d, c=%d", A, B, c);

* Note that line 2nd, 3%d, is separated by an underscore-so we have to underline each integer input--such as input, or when assigning a value to a variable will be problematic

* After all the values are entered and hit enter, the SCANF function assigns the variables a, B, C sequentially, and then outputs

Note: The delimiter between the numbers is arbitrary, not necessarily with an underscore--it can be a comma, a space, an asterisk *, a pound sign #, and so on, or even an English letter

Comma, scanf ("%d,%d,%d", &a, &b, &c); Input format: 10,14,20//well number #scanf ("%d#%d#%d", &a, &b, &c); Input format: 10#14#20//letter xscanf ("%dx%dx%d", &a, &b, &c); Input format: 10x14x20

2> receives 3 values with the scanf function, separated by a space between each value
1 int A, B, c;2 scanf ("%d%d%d", &a, &b, &c); 3 4 printf ("a=%d, b=%d, c=%d", A, B, c);

* Note that line 2nd, 3%d is separated by a space, we must enter a delimiter after each input, delimiter can be a space, tab, carriage return

    • Use spaces to make separators

    • Use tab to make delimiters

    • Use a carriage return to do the delimiter

"C language" 05-printf and scanf functions

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.