Basic C language tutorial (my C journey started) [3]

Source: Internet
Author: User
Tags value of pi

7. The third C program
First, take a look at the following small program and try to compile and run it by yourself. If you do not know how to compile, click the following hyperlink:
Compiler usage
Compiler Dev-C ++ download & tutorial

/* Circle. c -- calculate the area of the circle */

# Include<Stdio. h>

Int main (void)
{
Float radius;/* circle radius */

Printf ("Please enter the radius :");
/* Get user input */
Scanf ("% f", & radius );
/* Formula for calculating the circular area: circumference rate * radius square */
Printf ("The area of the circle is: %. 3f \ n", 3.14 * radius );

Printf ("Press ENTER to quit ...");
Getchar ();
Getchar ();
Return 0;
}

Enter the radius, and press Enter ). This program requires us to enter numbers, for example, 15, 31.6. Do not enter letters, such as abc and t156. Inputting letters will cause program errors! We will learn how to deal with such errors in the future, but we should enter numbers honestly now! Of course, you can also try to enter a letter to see what will happen.

Next we will explain the knowledge points of this program.

1.The first line of the program, we useFloatDeclaresVariable.FloatThe data type isFloating Point Type, That is, decimal number. In the past, we usedIntTo declare variables. Int standsInteger, That is, an integer. Variables declared with int can only store integers, while variables declared with float can store decimals.

2.To accept user input, we need to useScanfFunction. AndPrintfSimilarly, scanf is also a function defined in the standard library.Standard Functions. Like printf, scanf's function prototype is also located inStandard header fileStdio. h.Placeholder % FThe function reads floating-point numbers using the scanf command;& RadiusTells scanf to use the read floating point number to give the variable radiusAssignment. & Is required. Otherwise, an error occurs in the program.&Here isAddress fetch OperatorUsed to obtain the variable radiusMemory AddressTo tell scanf to store the read floating point number to the address indicatedMemory spaceTo assign a value to the radius. It doesn't matter if you don't understand the above content. Really, you just need to remember it. In the future, we will learn this knowledge in depth.

3.Radius is floatVariable, 3.14. The default value isDual-precision floating point(Double)ConstantTherefore, the result of the 3.14 * radius expression is a double-precision floating point number. It doesn't matter if you don't understand it here. We will learn it in detail later.

4.To display floating point data, we need to use placeholders% F.% DIs used to display integers. If we convert%. 3fChange to % d, and the output will be abnormal.. 3The function is to tell printf to output only three digits after the decimal point. We can also change it. 2Or. 0.. 0 indicates that the fractional part is not output.

5.The program uses two consecutiveGetcharFunction, which is used to wait for the user to enter the carriage return before exiting the program.GetcharYesStandard FunctionsIts function prototype is also located in the standard header file stdio. h. It is used to read a character entered by the user. The specific reason is that we need to use two getchar statements later.

8. Error and Warning)

So far, we have written several C Programs. Some people may say that the program hasError(Error), And does not compile the program into an executable file. If the compiler reports an error, it indicates that our program has an error! The compiler reports an error if the semicolon (;) or no braces are written.

Some people may write programs that can be compiled, but the compiler also provides someWarning(Warning). Compiler alerts indicate that the code we write conforms to the C language syntax, but the effects of those codes may be different from what we expect.

As programmers, we should carefully read error messages and warnings. From the information, we can understand the programError locationFangWhere to correct the error. When an error is corrected,If no error is found in the row in which the compiler says an error, you need to check it.Whether a row has an error.

9. Constant (constant) and variable

Some data values have been determined before the program runs, and cannot be changed during the program running. Such data is calledConstant, OrConstant. For example, 'A' isCharacter constant, "A" isString constant, 123 isInteger constant, 123.45 isDouble PrecisionFloat Constants. The above data values are determined before the program runs, and cannot be changed, so they are constant.

The data that can be changed during the program running is calledVariable. For example:
Double pi = 3.14;
In the preceding statement, pi is a variable, because we can continuously change the value of pi after this statement. For example:
Pi = 3.1415;
Scanf ("% lf", & pi );
The above two statements can change the pi value. In the above Code, both 3.14 and 3.1415 are double Floating Point constants.% LfYesLYesLowercase English letter LIn lower case. % Lf is used to tell scanf that & pi isDouble Precision Floating Point pointer(Double *). % Lf means that & pi isExtended 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.

ConstantAndVariableThe difference is that the constant value is constant, and the value of a variable can be changed by assigning values.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.