(2.1) Grammar printf
Print F:format format
printf prints in format, output to console
printf: Displaying an integer
With%d (d:decimal)
Decimal English [DESML]
Adj. 10 rounded, fractional
printf ("A:%d\n,") ———— > A:33
printf ("A:%d,b:%d \ n", 32,33)
———— >a:32 b:33
Specify the number of integers:
Specify the number of digits to display, the alignment display:%3d or%4d
int a = 3;
int B = 33;
int c = 333;
printf ("Number:%d \ n", a);
printf ("Number:%d \ n", b);
printf ("Number:%d \ n", c);
printf ("Number:%03d \ n", a);
printf ("Number:%03d \ n", b);
printf ("Number:%03d \ n", c);
"03d" is used to align up and down, the width is the same
Int--integer integer variable
printf: Displaying a decimal
With%f (f = float)
printf ("x =%f \ n", 12.35);
printf ("x=%f, y=%f \ n", 12.35, 90.01)
Use double to denote decimals
Double x=123.456
Double y=99.87
printf ("X=%f,y=%f \ n", x, y)
Double: Variable type
X: Variable Name
123.456: Initial value
#include <stdio.h>
int main ()
{
int a =10
Double x=123.456; Double Float-point
printf ("x:%f\n", X);
return 0;
}
——— >x:123.456
Specify the number of digits after the decimal point
Double x =12.45678
Specifies 2 digits after the decimal point, rounded,
Using%.2f
printf ("X is%.2f\n", x);
Eg: the product of 123 and 456
printf ("Result:%d \ n", 123*456);
Or
int a = 123;
printf ("Result:%d \ n", a*456);
Summary:
1, learn the use of printf, note that the parameters are separated by commas in half-width
2. Use%d to print integers, use%04d to control width
3,%f to print decimals, using%.4f to control the number of digits after the decimal point
4, learn to define the int type variable, represent the integer
5, learn to define a double type variable, representing decimals
6, preliminary study simple +-*/the expression
2.3 scanf
Scan Scanning F format
Used to receive user input from the console
Enter an integer:
int n;
scanf ("%d", &n);
Enter decimal
Double X;
scanf ("%lf", &x); Lf--long float
Wait for user input
Input: 123
RESRLT:
int n;
printf ("Please input:"
scanf ("%d", &n);
printf ("Result:%d \ nthe", N)
scanf: One-time input multiple number
int A;
Double X;
printf ("Please input");
scanf ("%d,%lf", &a, &x);
printf ("Result:%D,%LF \ n", a,x)
scanf ("%d,%lf", &a, &x);
scanf ("%d-%lf", &a, &x);
scanf ("%d*%lf", &a, &x);
Note: Enter the time strictly according to the format to
To compare a complex format:
scanf ("AAA%DBBB%LFCCC", &a, &x);
Eg:1\ lets the user enter the month and year date in the format "2015-3-23".
Extract the year, month, day, replace as "2015/3/23" slash format output
int year;
int manth;
int day;
scanf ("%d-%d-%d", &year,&month,&day);
printf (%d/%d/%d ", year,month,day);
Eg2\ implements a program that calculates the product of any two decimals, prompting the user to first enter 2 numbers in the console and then print the product
To use this program for others
Double X;
Double y;
printf ("Please input:");
scanf ("%lf,%lf", &x, &y);
printf ("Result:%.3LF \ n", x*y);
Summary:
scanf How to enter data:
1. Define variables first, integers using int, decimal with double XI type
2. Using%d or%LF
3. Note the use of the & number before the parameter
Note printf scanf