当前位置:网站首页>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 ).
边栏推荐
- MySQL Real combat 45 [SQL query and Update Execution Process]
- MySQL practice 45 lecture [transaction isolation]
- Réglez la hauteur et lancez le système. Currenttimemillis catton
- Le processus de connexion mysql avec docker
- Distributed transaction
- Docker install redis
- [mathematical logic] predicate logic (individual word | individual domain | predicate | full name quantifier | existence quantifier | predicate formula | exercise)
- C # general interface call
- Find the storage address of the elements in the two-dimensional array
- [Chongqing Guangdong education] cultural and natural heritage reference materials of China University of Geosciences (Wuhan)
猜你喜欢
![MySQL practice 45 lecture [transaction isolation]](/img/a5/5420651d6be51e892976f02be8c43c.png)
MySQL practice 45 lecture [transaction isolation]

Pytoch configuration

MySql實戰45講【SQL查詢和更新執行流程】

【PyG】理解MessagePassing过程,GCN demo详解

MySql实战45讲【事务隔离】

Vs 2019 configuration du moteur de génération de tensorrt

Hi3536C V100R001C02SPC040 交叉编译器安装

Installation and use of memory leak tool VLD

VS code配置虚拟环境
![[error record] the parameter 'can't have a value of' null 'because of its type, but the im](/img/1c/46d951e2d0193999f35f14d18a2de0.jpg)
[error record] the parameter 'can't have a value of' null 'because of its type, but the im
随机推荐
Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!
从C到Capable-----利用指针作为函数参数求字符串是否为回文字符
Vs 2019 configuration du moteur de génération de tensorrt
VS克隆时显示403错误
@Accessors注解作用指定前缀遵守驼峰命名
Solve high and send system Currenttimemillis Caton
New programmers use the isXXX form to define Boolean types in the morning, and are discouraged in the afternoon?
[mathematical logic] normal form (conjunctive normal form | disjunctive normal form | major item | minor item | maximal item | minor item | principal conjunctive normal form | principal disjunctive no
I2C 子系统(四):I2C debug
[combinatorics] number of solutions of indefinite equations (number of combinations of multiple sets R | number of non negative integer solutions of indefinite equations | number of integer solutions
@Accessors annotation function specifies that the prefix follows the hump naming
复选框的使用:全选,全不选,选一部分
float与0比较
BigVision代码
[algebraic structure] group (definition of group | basic properties of group | proof method of group | commutative group)
Pytorch配置
Model transformation onnx2engine
The process of connecting MySQL with docker
docker安装redis
How to use asp Net MVC identity 2 change password authentication- How To Change Password Validation in ASP. Net MVC Identity 2?