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

Source: Internet
Author: User
Tags integer division

19. Basic data types: plural and imaginary Data Types

C99 added Plural type( _ Complex) And Virtual Data Type( _ Imaginary). In short, C99 provides three types of plural: Float _ Complex , Double _ Complex , And Long double _ Complex. For Float _ ComplexFor a type variable, it contains two FloatType value, a value used to represent the plural Real-time( Real part), And the other is used to represent Virtual part( Imaginary part). Similarly,
Double _ ComplexContains two DoubleType value. And so on. C99 also provides three types of virtual numbers: Float _ Imaginary , Double _ Imaginary , And Long double _ Imaginary. The virtual data type has only the virtual data and has no real data. Contains standard header files Complex. hThen we can use ComplexTo represent _ Complex, Use
ImaginaryTo represent _ Imaginary, And use ITo represent the number of virtual units I, That is -1Square root. Example: # include <Complex. h >
Double _ Complex x = 5.2;/* the real part equals 5.2, and the virtual part is 0 */
Double complex y = 5.0 * I;/* the real part is 0, and the virtual part is 5.0 */
Double complex z = 5.2-5.0 * I;/* the real part is 5.2, and the virtual part is 5.0 */ Note:: _ ComplexFor Independent Environment( Freestanding environment) Is Optional. This type is not mandatory. The so-called Independent EnvironmentC Programs can run without an operating system. _ ImaginaryType is optional in any environment. The current compiler does not support both types well, so I will not discuss them further here. Now, the basic data types are all discussed.


20. Examples of escape characters

The following example demonstrates\,\ B,\ T,\ R, And\ N. Where\ B,\ TAnd\ RThe concept can be traced back to computers still using typewriter as output devices, so they may not work for modern computers. On Some Macintosh machines, their functions are different from what we call them below. First, take a closer look at the following program, compile and run it, and check whether the running result is the same as you think. Then, let's look at the detailed explanation later. /* Esc_sq.c -- Use escape characters */# include<Stdio. h>Int main (void) {float height; printf ("\ aPlease enter your height: _____ cm \ B "); scanf ("% f", & height); printf ("\ tYour height is %. 2f. \ rOh! \ N ", height); return 0;} The first printf will usually cause a soundBEEP(\), And the following prompt is displayed: Please enter your height: _____ cm where the active position (cursor) is at the beginning of the underline. The reason why the active location is at the beginning of the underline is that the eight\ BThe active location is backed up by eight locations. Followed by an underscore_____By five_.Note:: In general, \ B does not erase the characters output earlier, but in some systems, \ B erases the characters output earlier, causing the result to bePlease enter your height:. \ A resultsDepends on hardware. In general, output \ a will generateBEEP. However, in some systems, output \ a does not produce any effect, or only displays a special character. After you enter a number (for example, enter 180.5), the screen will be displayed as Please enter your height: 180.5
The number we entered replaces the original underline. Then we need to pressEnterConfirm our input. After you press the Enter key,
The active location is moved to the beginning of the next row.
After the second printf runs, the screen displays the following: Please enter your height: 180.5 Oh! Your height is 180.50.This is because\ TMove the active position backward to several positions (usually eight), and then output
Your height is 180.50.. Next\ RMake the active position return the beginning of the current row, and then outputOh!,
Last\ NLine feed.
 
 Addition OperatorAdd the values of the left and right sides. For example: printf ("% d", 9+11); the output result is 20. The operands of addition operators can be constants or variables. For example, var = var_1 + var_2; this statement first adds the value of var_1 and the value of var_2, and then assigns the sum result to var.  2.Subtraction Operator ):- Subtraction OperatorThis causes the left operand to be subtracted from the right operand. For example, var = 100-40; the value of 100-40 is 60, and the value of this 60 is assigned to var.Addition OperatorAndSubtraction OperatorCalledBinary Operator(Binary operator), Because they need two operands.3.Sign Operator:-And +For example, var_1 =-5; var_2 =-var_1; var = + 5 var_1 has a value of-5, var_2 has a value of 5, and var has a value of 5. Positive and Negative operators are calledSingle Object Operator(Unary operator), Because they only need one operand.

 

23. multiplication and division Operators

1. Multiplication Operator (Multiplication Operator ):** Is a multiplication operator. The following statement: meter = 100 * cm; constant 100 is multiplied by the variable cm, and the product is assigned to the variable meter.2. Division Operator (Division Operator ):/C usage/As the Division operator./The left operand is divided by the right operand. That is to say,/On the left is the divisor, and on the right is the divisor. For example, var = 6/2; 6 is divided by 2 to 3, and 3 is assigned to the variable var. The integer division is different from the floating point division. If a floating point is used to get a floating point, an integer is used to get an integer. Language C stipulates that all fractional parts generated by Division of integers are discarded, which is called truncation ). For example, the result of 7/4 is 1, not 1.75, or 2. Let's look at a small program. /* Divide. c */
# Include<Stdio. h>Int main (void)
{
Printf ("integer division: 7/4 is % d \ n", 7/4 );
Printf ("floating division: 7./4. is % f \ n", 7./4 .);
Printf ("mixed division: 7./4 is % f \ n", 7./4); return 0;
} The output is integer division: 7/4 is 1.
Floating division: 7./4. is 1.750000
Mixed division: 7./4 is 1.750000 In the last printf, we use a floating point number divided by an integer to get the same result as the second printf. This is because C automatically converts the two operands to the same type. In this example, integer 4 is converted to the floating point type and then divided with the floating point 7. Before C99, a positive integer and a negative integer are mutually exclusive. If the fractional part is generated, the result of division is uncertain. For example, the result of 7/-4 may be-2 or-1. C99 stipulates that the division of positive integers and negative integers will discard all generated decimal parts. That is to say, in C99, the result of 7/-4 is definite and must be-1.

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.