Home > Article > Backend Development > What is the difference between scanf and printf in C language
Difference: scanf is an input function, used to input data from a standard input device (usually a keyboard); printf is an output function, used to output data to a standard output device (usually a monitor).
The operating environment of this tutorial: windows7 system, c99 version, Dell G3 computer.
scanf() and printf() are commonly used functions in the C standard library. And both functions can accept certain formats for input and output.
When calling printf and scanf, you must include #include
##The usage of printf is:
printf(“格式控制字符串”,输出参数一,输出参数二);The format control string includes: format control instructions, ordinary characters format control instructions mainly output data according to the specified format, including format control characters starting with
%, different types of data use Different format control characters (use
%d for int type,
%f for float and double)
fahr=,
celsius= in "
fahr=%d,celsius=%d\n" usage of
scanf(“格式控制字符串”,输入参数一,输入参数二);The format control string contains: format control description, ordinary characters format control string represents the input format, (int type uses
%d, float type uses
%f, double type uses
%lf)
%u Decimal unsigned integer
%f Floating point number
%s String
%c Single character
%p Pointer value
%e Floating point number in exponential form
%x , %X Unsigned integer expressed in hexadecimal
%0 Unsigned integer expressed in octal
%g Automatically select the appropriate representation
\f Clear screen and page feed
\r Enter
\t Tab character
\xhh means An ASCII code is expressed in hexadecimal,
where hh is 1 to 2 hexadecimal numbers
printf(“fahr=%d,celsius=%d\n ” ,fahr,celsius);
##2、
printf(“enter x(x>=0):\n”);
3、
printf(“y=f(%f)=%.2f\n”,x,y);%f specifies to output floating-point data in decimal form, retaining 6 decimal places, while %.2f specifies to retain 2 decimal places when output
4 ,
printf(", d",D[i]); d means that when outputting a value less than 4 digits, 0 will be added in front to make the total width 4 bit.
5,
scanf("%lf",&x);//Read inputCall the scanf() function to input data, and add & in front of the variable name x , l in %lf is the first letter of long. The input parameters of the scanf function must correspond to the format control description in the format control string,
and their type, number and position must correspond one to one. .
scanf("%d%d%lf",&x,&y,&z) means that the input x is of type int, y is of type int, and z is of type double. This is the one-to-one correspondenceRelated recommendations: "
The above is the detailed content of What is the difference between scanf and printf in C language. For more information, please follow other related articles on the PHP Chinese website!