C Language Basics Tutorial (my c tour started) [Eight]_c language

Source: Internet
Author: User
Tags constant integer division square root

19. Basic data type: complex number type and imaginary number type

C99Added a plural type_complex) and Imaginary Type_imaginary)。 In simple terms, C99 provides three complex numbers of types: float _complex, Double _complex, and long Double _complex。 For float _complexA variable of type, it contains two floatThe value of the type used to represent a complex number. Real Department Real Part), and another to indicate Imaginary PartImaginary part)。 Similarly,
Double _complexContains two DoubleThe value of the type. Analogy The C99 also offers three imaginary types: float _imaginary, Double _imaginary, as well as long Double _imaginary。 Imaginary type only imaginary part, no real part.
With standard header file complex.h , we can use complex to represent _complex,
imaginary to represent the _imaginary, and I to represent the imaginary unit I, which is the square root of 1 . For example:
#include <complex.h >
double _complex x = 5.2; / * real part equals 5.2, imaginary part is 0 * *
Double Complex y = 5.0 * I; / * Real part is 0, the imaginary part is 5.0 * *
Double complex z = 5.2–5.0 * I; / * Real part is 5.2, the imaginary part is 5.0 * *
Attention : the _complex type is optional for stand-alone environments ( freestanding Environment). Optionally, it is not mandatory to support this type. The so-called independent environment , refers to the C program can operate without operating system. The _imaginary type is optional in any environment. The current compilers are not very good at both types of support, so I'm not going to discuss the two types in more detail here.
At this point, the basic data types are all discussed.


20. Escape character usages
The following example shows the role of \a, \b, \ t, \ r, and \ n . The concept of \b, \ t , and \ r can be traced back to the time when computers still use typewriters as an output device, so for some modern computers, they may not work. On some Mackintosh (Macintosh), they do not function as we say below. First, please look at the following program carefully, and then compile the run to see if the results of the operation and you imagine whether the same, and then look at the detailed explanation later.
/ * ESC_SQ.C-- Use the escape character /*
#include <stdio.h >
int main (void)
{
float height;
printf ("\aplease Enter your height: _____ cm\b\b\b\b\b\b\b\b");
scanf ("%f", &height);
printf ("\tyour height is%.2f.\roh!\n", height);
return 0;
}
The first printf usually causes a beep ( \a) and displays the following prompts
Please enter your height: _____ cm
where the active position (cursor) is the beginning of the underline. The active position is at the beginning of the underscore because the eight \b take the active position back eight positions. Incidentally, the underline _____ is composed of five _ . Note : In general, \b does not erase the characters that were previously exported, but in some systems \b erases the preceding characters, causing the result to be displayed as please enter your height: . The effect that \a produces depends on the hardware . Generally speaking, output \a will produce a ringing . However, in some systems, the output \a does not produce any effect, or simply displays a special character.
after entering a number (such as input 180.5), the display of the screen becomes
Please enter your height:180.5 cm
The number we entered replaces the original underline. Then we need to  press ENTER to determine our input. after pressing the ENTER key,
The active position moves to the beginning of the next line.
After the second printf run is finished, the screen appears as follows:
Please enter your height:180.5 cm
Oh! Your the height is 180.50.
This is because\ tMoves the active position backward several positions (usually eight), and then outputs
Your the height is 180.50.。 Then\ rReturns the active position to the beginning of the current row and then outputsoh!
At last\ nCauses a newline.
         
     
The addition operator adds the value of the left and right sides of it. For example:
printf ("%d", 9 + 11);
The output result is 20. The operand of the addition operator can be a constant or a variable. For example:
var = var_1 + var_2;
This statement first adds the value of the var_1 to the value of the var_2, and then assigns the result of the addition to Var.
2. subtraction operator (subtraction Operator): –
The subtraction operator causes the left-hand operand to be subtracted from the right-hand operand. For example:
var = 100–40;
The value of the 100–40 is 60, and then this 60 is assigned to Var.
The addition and subtraction operators are called two-mesh operators ( binary operator) because they require two operands.
3. positive and negative operators (Sign Operator): – and +
For example:
var_1 =-5;
var_2 =-var_1;
var = +5
The value of var_1 is the value of -5,var_2 is the value of 5,var is 5.
The positive and negative operators are called monocular operators ( unary operator) because they require only one operand.

23. Multiplication and division operators
1. Multiplication operator (multiplication Operator): *
* is the multiplication operator. The following statement:
meter = cm;
Constant 100 is multiplied by the variable cm, and the product is assigned to the variable meter.

2. Division operator (Division Operator):/
C uses / as the division operator. / left operand divided by the right-hand operand. In other words, / left is the divisor, the right is the divisor. For example:
var = 6/2;
6 divided by 2 to 3, then 3 is assigned to variable var.
Integer division differs from floating-point number division. A floating-point number divides to get a floating-point number, and an integer divides to get an integer . The C language stipulates that the number of decimal parts produced by the integer division is discarded , which is called truncation ( truncation). For example: 7/4 results are 1, not 1.75, and not 2.
Here we 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 floating-point numbers divided by integers to get the same result as the second printf. This is because C automatically converts the types of these operands to the same type. In this example, Integer 4 is converted to a floating-point type and then to floating-point number 7. Divide
Before C99, a positive integer and a negative integer are divided, and if a decimal part is produced, the result of the division is indeterminate. For example: The result of the 7/-4 may be-2, or it may be-1. C99 stipulates that a positive integer and a negative integer are divided, and that the resulting number of decimal parts is discarded. in other words, in C99, the result of 7/-4 is OK, must be-1.
Related Article

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.