7. A third C procedure
First of all, please look at the following small program, try to compile their own run. If you do not know how to compile, please click the following hyperlink:
 Compiler use Method
compiler dev-c++ Download & Use tutorial
 
/ * circle.c-Calculate the area of the circle * *
 
#include <stdio.h>
 
int main (void)
{
float radius; / * radius of Circle * *
 
           printf ("Please enter the radius:"); 
           /* get user input */ 
            scanf ("%f", &radius); 
           /* Circle area Calculation formula: Pi * radius squared */ 
           printf ("The area of the ' Circle is:%.3f\n", 3.14 * radius * RA Dius); 
 
           printf ("Press ENTER to quit ..."); 
           GetChar (); 
           GetChar ();      
            return 0; 
      } 
 
"Enter the radius" means: Enter the radius of the circle, and then press ENTER (enter). This program requires us to enter numbers, for example: 15, 31.6. Do not enter letters, such as: ABC, t156. Entering letters will cause a program error! Later we will learn how to deal with this error, but now still honest to enter the number bar! Of course, you can also try typing letters to see what happens.
 
Let's take a closer look at the knowledge points of the program.
 
1. in the 7th line of the procedure, we declare a variable named radius using float . The data type represented by float is a floating-point type , which is a decimal number. In previous programs, we used int to declare variables. An int represents an integral type , which is an integer. A variable declared with int can store only integers, and a variable declared with float may store decimals.
 
       2.   to accept user input, we need to use  scanf   function. Like  printf , scanf is also a function defined in the standard library, and we call such a function   standard function  . Like printf, the scanf function prototype is also located in the   standard header file   stdio.h.  Placeholder  The function of the  %f   is to command the scanf function to read floating-point numbers;  &radius  Tell scanf Assigns a variable radius  assignment  with a read-to floating-point number. Where & is necessary, otherwise the program will be wrong.  &   here is   address operator  , Used to get the  memory address  of the variable radius, which tells scanf to store the floating-point number read to the  memory space  indicated by that address, and it achieves the purpose of assigning the radius. If you can't read these things, it doesn't matter, really, as long as you can remember. In the future, we will learn this knowledge in greater depth. 
 
3. radius is a floating-point variable , and the 3.14 default is a double-precision floating-point type (  double) constant, so the expression 3.14 * radius * radius The result is a double-precision floating-point number. It doesn't matter if you don't understand here, we will study in detail later.
 
4. to display floating-point data, we need to use placeholder %f. %d is used to display integers, and if we replace the %.3f in the second printf with%d, the output will be abnormal. . 3 's role is to tell printf to output only 3 digits after the decimal point. We can also change to . 2 or . 0 , etc.. 0 means not to output decimal parts.
 
5. The program last used two GetChar functions, which played a role in waiting for the user to enter a carriage return before exiting the program. GetChar is also a standard function , its function prototype is also in the standard header file stdio.h, its function is to read the user input a character. Specifically why use two getchar here we'll talk about it later.
 
 
 
  
   
   | 8. Errors (Error) and Warning (Warning) 
 
 | 
 
   
   | so far, we have written several C programs. Perhaps some people are compiling the program, the compiler says the program there is  error  ( error ), and does not compile the program into an executable file. The compiler error indicates that there is a bug in our program! Omitting a semicolon (;) or not writing an curly brace} and so on can cause the compiler to report an error.       Maybe some people write programs that can be compiled, but the compiler also gives some  warnings  ( warning ). The compiler alert indicates that the code we write conforms to the C language syntax, but the code may have a different effect than we expected.        As a programmer, we should read error messages and warnings carefully. From that information, we can find out where the program  goes wrong   Square , which corrects the error. To make a mistake,      .  | 
 
  
 
 
  
   
   | 9. Constants (constant) and variables 
 
 | 
 
   
   |  | 
 
   
   | The value of some data is determined before the program is run and cannot be changed while the program is running, such data is called a  constant, or constants . For example: ' A ' is a character constants , "a" is a string constant , 123 is an integer constant , and 123.45 is a double-precision floating-point constant . The values of the above data are determined before the program is run, and cannot be changed, so they are all constants. The data that can be changed while the program is running is called a variable . For example:Double pi = 3.14;
 In the above statement, PI is a variable, because we can constantly change the value of pi after this statement. For example:
 PI = 3.1415;
 scanf ("%lf", &PI);
 The two statements above can change the value of pi. In the code above, 3.14 and 3.1415 are double (double) floating-point constants. %lf is in the l is lowercase letter L, here must be lowercase. The function of the%LF is to tell the scanf function that &pi is a double-precision floating-point pointer (double *). The%LF means that the &PI is a lengthened double-precision floating-point pointer (long double *). Because Pi is a double-precision floating-point variable, &PI is a double-precision floating-point pointer, so we should use %lf.
 The difference between constants and variables is that the value of the constant is invariable, and the value of the variable can be changed by means of assignment. |