Home  >  Article  >  Backend Development  >  What is the difference between scanf and printf in C language

What is the difference between scanf and printf in C language

青灯夜游
青灯夜游Original
2021-03-24 14:14:3516825browse

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).

What is the difference between scanf and printf in C language

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)

Ordinary characters are the characters that are output as they are when outputting data. For example:

fahr=, celsius= in "fahr=%d,celsius=%d\n" usage of

scanf Yes:

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)

Ordinary characters: The usage of printf mentioned above is the same.

The input parameters are The address of the variable, so add &

--------Format Control Instructions---------------------- in front of the variable ----------------------------------

%d Decimal signed integer

%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

------Some special Specified characters------------------------------------------------ -----------

\n Line feed

\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


1,

printf(“fahr=%d,celsius=%d\n ” ,fahr,celsius);

What is the difference between scanf and printf in C language##2、

printf(“enter x(x>=0):\n”);

What is the difference between scanf and printf in C language3、

printf(“y=f(%f)=%.2f\n”,x,y);

What is the difference between scanf and printf in C language%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 input

Call 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: "

C Language Video Tutorial

"

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn