当前位置:网站首页>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 ).
边栏推荐
- TCP handshake three times and wave four times. Why does TCP need handshake three times and wave four times? TCP connection establishes a failure processing mechanism
- I2C 子系统(一):I2C spec
- Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!
- [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
- I2C subsystem (III): I2C driver
- C语言初阶-指针详解-庖丁解牛篇
- Model transformation onnx2engine
- Segmentation fault occurs during VFORK execution
- MySql实战45讲【行锁】
- 基于Qt的yolov5工程
猜你喜欢

Nasvit: neural architecture search of efficient visual converter with gradient conflict perception hypernetwork training

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

MySql实战45讲【事务隔离】

VS 2019安装及配置opencv

力扣------网格中的最小路径代价

Vs Code configure virtual environment

Pytoch configuration

Sous - système I2C (IV): débogage I2C

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

Pat class B "1104 forever" DFS optimization idea
随机推荐
Docker install MySQL
Basic information of Promethus (I)
How to limit the size of the dictionary- How to limit the size of a dictionary?
基于QT的tensorRT加速的yolov5
Why does thread crash not cause JVM crash
Idea format code idea set shortcut key format code
MySQL practice 45 lecture [transaction isolation]
基于Qt的yolov5工程
Change cell color in Excel using C - cell color changing in Excel using C
Spark on yarn资源优化思路笔记
MySql实战45讲【SQL查询和更新执行流程】
Three. JS local environment setup
Agile certification (professional scrum Master) simulation exercises
二进制流转换成字节数组
Pat class B common function Usage Summary
VS 2019安装及配置opencv
MySQL Real combat 45 [SQL query and Update Execution Process]
I2C 子系統(四):I2C debug
Réglez la hauteur et lancez le système. Currenttimemillis catton
左连接,内连接