当前位置:网站首页>C programming learning notes [edited by Mr. Tan Haoqiang] (Chapter III sequence programming) 05 data input and output
C programming learning notes [edited by Mr. Tan Haoqiang] (Chapter III sequence programming) 05 data input and output
2022-07-03 03:14:00 【ihan001】
List of articles
One 、 Input and output of data
give an example 1:
seek ax2+bx+c=0 The root of the equation .a,b,c Input by keyboard , set up b2-4ac>0.
Their thinking : First of all, you need to know how to find the root of the equation . Known by mathematical knowledge : If b2-4ac≥0, Then the quadratic equation of one variable has two real roots :x1=−b+√b2−4ac/2a,x2=−b−√b2−4ac/2a, Divide the fraction into two terms :p=−b/2a,q=√b^2−4ac/2a, be x1=p+q,x2=p-q, With these formulas , As long as you know a,b,c Value , We can successfully find the two roots of the equation .
#include <stdio.h>
#include<math.h> // The program calls the square root function sqrt
int main()
{
double a,b,c,disc,x1,x2,p,q; //disc Used to store discriminant (bb-4ac) Value
scanf("%lf%lf%lf",&a,&b,&c); // The value of the input double precision variable should be declared in the format ″%lf″
disc=b*b-4*a*c;
p=-b/(2.0*a);
q=sqrt(disc)/(2.0*a);
x1=p+q;x2=p-q; // Find the two roots of the equation
printf("x1=%7.2f\nx2=%7.2f\n",x1,x2); // Two roots of the output equation
return 0;
}

scanf The function is used to input a,b,c Value .
Variables in parentheses in function a,b,c In front of , To use address characters &.&a Said variable a Address in memory .
Use %lf Format statement , Indicates that the input is a double precision real number .
The format declaration is “%lf%lf%lf”, Request input 3 Double precision real number . Program runtime , Input “1 3 2”, Separate the two numbers with a space . Although the input is an integer , However, due to the designated use %lf Format input , So the system will put this 3 Convert integers into real numbers 1.0,3.0,2.0, Then assign it to the variable a,b,c.
stay printf Function , In the formatter f The is preceded by “7.2”, Indicates that in the output x1 and x2 when , The specified data accounts for 7 Column , Among them, decimals account for 2 Column . advantage :
① The number of decimal places can be output according to actual needs ;
② If you output multiple data , It can make the output data neat and beautiful .
Use preprocessing instructions at the beginning of the program file #include Put relevant header files in this program
#include Instructions
Three forms :
#include “c:\cpp\include\myfile.h”
#include "myfile.h“
#include <myfile.h>
Find the file by the specified path
The directory where the source program files are located
C Compile system specified include Catalog
Two 、 Output function
printf function
Used to send messages to the terminal ( Or the system implicitly specifies the output device ) Output several arbitrary types of data .
printf( Format control , Output table columns )
(1) “ Format control ” Is a string enclosed in double quotation marks , Called format control string , Abbreviation format string . Include :
① Format statement . The format declaration is made by “%” And format characters . The function is to convert the output data into the specified format and then output .
② Ordinary character . Ordinary characters are characters that need to be output as they are .
(2) The output table column is some data that the program needs to output , It can be a constant 、 Variable or expression .


3、 ... and 、printf Example of function
use %f Output real number , Can only get 6 Decimal place .
#include <stdio.h>
int main()
{
double a=1.0;
printf("%f\n",a/3);
return 0;
}
although a It's double precision ,a/3 The result is also double precision , But with %f A format declaration can only output 6 Decimal place .
#include <stdio.h>
int main()
{
double a=1.0;
printf("%20.15f\n",a/3);
return 0;
}

A double precision number can only guarantee 15 Accuracy of significant digits , Even if the specified decimal places are 50( If used %55.50f), There is no guarantee that the output 50 Bits are valid numbers .
Four 、 Input function
scanf function
It's used to input data .
scanf( Format control , Address table column )
(1) “ Format control ” Is a string enclosed in double quotation marks , Meaning with printf function . Include :
① Format statement . With % Start , End with a format character , Additional characters can be inserted in the middle .
② Ordinary character .
(2) The address table column is a table column composed of several addresses , It can be the address of a variable , Or the first address of a string .
% Additional characters Format characters
(1) scanf Function format control should be followed by the variable address , Not variable names .
It should match the format description above , Otherwise, there will be a mistake .
(2) If there are other characters in the format control string in addition to the format declaration , When inputting data, the same characters as these characters should be input at the corresponding position .
(3) In use “%c” Format declaration input character , Space characters and “ Escape character ” All characters in are entered as valid characters .
(4) When inputting numerical data , If you enter a space 、 enter 、Tab Key or encountered illegal character ( Characters that are not numeric ), Think the data ends .

Four 、 Character functions
1. Input :getchar
getchar function
Enter a character into the computer .
getchar()
Function has no arguments .
The value of the function is the character from the input device .
Only one character can be received .
If you want to input multiple characters, you need to use multiple functions .
Not only can a displayable character be obtained from the input device , And you can get control characters .
use getchar The character obtained by the function can be assigned to a character variable or integer variable , It can also be part of an expression . Such as ,putchar(getchar()); Output the received characters .
2. Output :putchar
putchar function
Output a character from the computer to the display .
putchar (c)
use putchar Function can output displayable characters , You can also output control characters and escape characters .
putchar Medium c Can be character constants 、 integer constants 、 Character variable or integer variable ( Its value is in the... Of the character ASCII Within the scope of the code ).
边栏推荐
- com.fasterxml.jackson.databind.exc.InvalidFormatException问题
- Update and return document in mongodb - update and return document in mongodb
- 监听对象中值变化及访问
- Vs 2019 installation and configuration opencv
- Left connection, inner connection
- js根据树结构查找某个节点的下面的所有父节点或者子节点
- 基于QT的tensorRT加速的yolov5
- Use of El tree search method
- 使用InputFilter限制EditText时踩坑及解决方案
- Parameter index out of range (1 > number of parameters, which is 0)
猜你喜欢

Idea set method call ignore case
![[pyg] understand the messagepassing process, GCN demo details](/img/8b/8490aac98fd2753e661f74e284f43d.png)
[pyg] understand the messagepassing process, GCN demo details

Vs Code configure virtual environment

Unity3d RPG implementation (medium)

umi 路由拦截(简单粗暴)

Distributed transaction

MySql实战45讲【事务隔离】

Idea format code idea set shortcut key format code

Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!

3D drawing example
随机推荐
Idea set method call ignore case
Converts a timestamp to a time in the specified format
MySQL practice 45 [global lock and table lock]
为什么线程崩溃不会导致 JVM 崩溃
VS 2019 配置tensorRT生成engine
Find the storage address of the elements in the two-dimensional array
MySQL practice 45 lecture [row lock]
VS 2019 配置tensorRT生成engine
How to make backgroundworker return an object
Force deduction ----- the minimum path cost in the grid
Are there any recommended term life insurance products? I want to buy a term life insurance.
C语言初阶-指针详解-庖丁解牛篇
I2C 子系統(四):I2C debug
Parameter index out of range (1 > number of parameters, which is 0)
I2C 子系统(二):I3C spec
Réglez la hauteur et lancez le système. Currenttimemillis catton
The base value is too large (the error is marked as "08") [duplicate] - value too great for base (error token is'08') [duplicate]
JS finds all the parent nodes or child nodes under a node according to the tree structure
基于QT的tensorRT加速的yolov5
PAT乙级常用函数用法总结