当前位置:网站首页>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 ).
边栏推荐
- Use of El tree search method
- I2C subsystem (III): I2C driver
- float与0比较
- MySql实战45讲【全局锁和表锁】
- 你真的懂继电器吗?
- 基于QT的tensorRT加速的yolov5
- Are there any recommended term life insurance products? I want to buy a term life insurance.
- Creation and destruction of function stack frame
- 函数栈帧的创建与销毁
- Segmentation fault occurs during VFORK execution
猜你喜欢
[shutter] monitor the transparency gradient of the scrolling action control component (remove the blank of the top status bar | frame layout component | transparency component | monitor the scrolling
vfork执行时出现Segmentation fault
Agile certification (professional scrum Master) simulation exercise-2
【PyG】理解MessagePassing过程,GCN demo详解
I2C 子系统(四):I2C debug
MySql实战45讲【SQL查询和更新执行流程】
Opengauss database development and debugging tool guide
Vs 2019 configure tensorrt to generate engine
I2C 子系统(三):I2C Driver
Hi3536C V100R001C02SPC040 交叉编译器安装
随机推荐
I2C subsystem (I): I2C spec
内存泄漏工具VLD安装及使用
MySql实战45讲【索引】
Agile certification (professional scrum Master) simulation exercise-2
模糊查询时报错Parameter index out of range (1 > number of parameters, which is 0)
[pyg] understand the messagepassing process, GCN demo details
二维数组中的元素求其存储地址
监听对象中值变化及访问
VS 2019 配置tensorRT生成engine
C# WebRequest POST模式 ,基于“Basic Auth”口令认证模式,使用multipart/form-data方式上传文件及提交其他数据
为什么线程崩溃不会导致 JVM 崩溃
C#通用接口调用
将时间戳转为指定格式的时间
Le processus de connexion mysql avec docker
How to limit the size of the dictionary- How to limit the size of a dictionary?
Last update time of all sqlserver tables
Spark on yarn resource optimization ideas notes
What happens between entering the URL and displaying the page?
VS 2019配置tensorRT
[error record] the parameter 'can't have a value of' null 'because of its type, but the im