Learning notes 05-printf and scanf Functions

Source: Internet
Author: User
I. printf Functions

This is a function declared in stdio. H. Therefore, you must add # include <stdio. h> before using it to output data to standard output devices (such as screens ).

1. Usage
printf("Hello, World!");

The output result is:

2> printf (string, format character parameter)

1 // use the constant as the parameter 2 printf ("My age is % d \ n", 26); 3 4 // You can also use the variable 5 Int age = 17; 6 printf ("My age is % d", age );

* The format character % d indicates that an integer is output in a signed decimal format. The 26 and age in the format character parameter will replace the location of % d.

* \ N is an escape character in the 2nd line of code, indicating a line break. Therefore, the first line break is output after "My age is 26" and "my age is 27" is output"

Output result:

* If \ n in row 2nd is removed, this will be the result.

Output result:

Summary: The number of operators in the left string must be the same as the number of parameters in the right string. the type of the operators determines the type of the parameters, for example, % d, it indicates that the corresponding format character parameter must be an integer.

2. Common Format characters and their meanings

3. You can also add some fine-grained format controls to the format operator. 1> the output width.

* First, let's look at the default integer output.

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

Output results (note that there is a vertex following ):

* If I change % d to % 4d:

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

Output: you will find that the distance between "is" and "14" is opened.

% 4D indicates that the output width is 4, and the width of "14" is 2. Therefore, if there are two more widths, the extra widths will be filled with spaces on the left, therefore, you will see two spaces on the left of "14". If the actual value width is large, for example, use "142434" with % 4D output width of 6 ", the output is based on the actual value width of 6.

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

 

Output result: the output width of 142434 is 6.

* If you change to %-4d

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

Output result: you will find that the distance between "14" and "." is opened.

%-4D indicates that the output width is 4. If it is larger than the actual value width, the extra width will be filled with spaces on the right side. If 4 is smaller than the actual value width, the output is based on the width of the actual value.

2> decimal places of Floating Point Numbers

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

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

Output result: the default value is 6 decimal places.

* If you only want to output 2 decimal places, replace % 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 places.

Output result: the output width is 8 and the number of digits is retained.

Ii. scanf Functions

This is also a function declared in stdio. H. Therefore, you must add # include <stdio. h> before using it. When the scanf function is called, the address of the variable needs to be input as a parameter. The scanf function will wait for the standard input device (such as the keyboard) to input data and assign the input data to the corresponding variable of 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);

* After running the program and executing the 1st lines of code, the console will output a prompt message:

* When you execute the scanf function of Row 3, the system waits for the user's keyboard input and does not execute the code later. The first parameter of scanf is "% d", which indicates that the user must enter an integer in decimal format. Note that the 2nd parameters of scanf do not pass the age variable, but the address & age of the age variable, which is an address operator in C language and can be used to obtain the address of the variable.

* You can enter 8 after the prompt:

 

(Due to xcode's own problems, we can only enter data with a width of 1 on the console. If you want to input data with a width greater than 1, for example, input 27, you can copy 27 from somewhere else, paste it to the console)

 

* After the input is complete, press the Enter key to tell the scanf function that the input is complete. The scanf function assigns the input 8 value to the age variable.

* After the scanf function is assigned a value, the code will be executed later. When the code is executed to 6th rows, the Controller will output:

2. Other usage 1> use the scanf function to receive three values. Here, each value is separated by a hyphen (-).
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 lines 3 and 3% d are separated by a hyphen (-). Therefore, after each integer is entered, A hyphen (-) must be entered. For example, otherwise, problems may occur when assigning values to variables.

* After All values are input, press Enter. the scanf function assigns values to variables A, B, and C, and then outputs the values.

Note: The delimiter between values is arbitrary. It may be a comma, space, asterisk (*), pound number (#), or even English text.

 

1 // comma, 2 scanf ("% d, % d, % d", & A, & B, & C); // input format: 10, 14, 203 4 // Well #5 scanf ("% d # % d", & A, & B, & C); // input format: 10 #14 #206 7 // letter X8 scanf ("% DX % d", & A, & B, & C); // input format: 10x14x20
2> use the scanf function to receive three values, separated by spaces.
int a, b, c;scanf("%d %d %d", &a, &b, &c);printf("a=%d, b=%d, c=%d", a, b, c);

 

* Note that lines 3 and 3% d are separated by spaces. After each integer is entered, a separator must be entered. The separator can be space, tab, or press Enter.

  • Use space as Separator

 

  • Use tab as Separator

 

  • Use carriage return as the Separator

 

 

 

 

 

 

 

 

 

 

Learning notes 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.