C Language Basics Tutorial (my trip to C began) [Seven]_c languages]

Source: Internet
Author: User
Tags constant

17. Basic Data Type: Boolean type (_bool type)

The _bool type is added by C99 to represent a Boolean value, that is, to represent logic true ( true) and logical false (false). Because C uses 1 for true and 0 for false, _bool is actually an integer type. Theoretically _bool only needs 1 bit storage units, because 1 bit is enough to represent 0 and 1. In fact, the_bool is an unsigned integral type that typically occupies 1 bytes. For example:
_bool flag = 1;
Flag = 0;
With the standard header file stdbool.h , we can use bool instead of _bool, true instead of 1, false instead of 0. For example:
BOOL flag = TRUE;
Flag = false;
This is done to be compatible with C + +.
Note : stdbool.h is added by C99.

18. Basic data type: floating-point type

1. Float, double, and long double
The data type mentioned earlier can only be used to handle integers. If we need to use decimals, we'll use floating-point types ( floating-point). C provides three floating-point types: float, double, and long double. ,
The C standard requires that the float type be at least accurate to 6 digits after the decimal point , and that the integer portion of the representation range must reach at least -37 - 10 +37 . float is generally a bit.
The C standard stipulates that the smallest representation range of the integer part of the double type is the same as float, which is -37 to +37 , but it requires that the decimal part of the double type be at least as accurate as the decimal digit. The double is usually 64 digits.
C also provides a long double type, which is intended to provide a more precise type than double. However, the C standard merely stipulates that a long double should be at least as accurate as a double.
2. declaring floating-point variables
the Declaration and initialization of a floating-point variable is the same as an integer variable. For example:
float f_1, f_2;
Double d_1;
float f_3 = 6.63;
Long double ld_1;
3. Floating-point constants
Floating-point constants are written in several ways. The basic form is: First write the integer part (can be signed), then write a decimal part , and then write e or e, and finally write a signed integer . For example:
+1.2E+5
1.5e-9
-5.0e10
where E or E is called a step code flag , the signed integer following E or E is called the Order code . The order code represents the order of 10 of the second square . For example: The value of +1.2E+5 is 1.2 * 5. Assuming that a is the section preceding e, and N is the part behind E, then Aen equals A * n. In addition, a plus sign can be omitted without writing. The decimal Part is also not required, that is to say, 5e3 is also correct. Order signs and order codes can also not be written, such as: 13.5. After the decimal point, the part of the integer before the order mark can not be written (9.E5), and the integer before the decimal point may not be written ( . 96e-8), but it cannot be written at the same time. For example:
56.
.5
3.14
3e6
.6E-8
Note: There can be no spaces in floating-point constants! For example:
3.21E-12/ * There are spaces, wrong! */
3.14 e5/ * have space, wrong! */
Floating-point constants are of type double by default. Suppose Var_f is a float-type variable if the following statement is available:
Var_f = 9.0 * 3.0;
Then 9.0 and 3.0 are both constants of type Double. Their product is also a double type. When the assignment is made, the product is converted to a float type and then assigned to Var_f.
of course, we can also specify the type of floating-point constants. If you add f or fto a floating-point constant, the compiler uses the float type to handle the constant. For example: 1.5f, 2.1e6f. After adding l or l , the compiler uses a long double to handle the constant. For example: 4.1l, 50.2e5l. It is best to use capital l, because lowercase l is easily confused with the number 1.
C99 Adds a format that represents a floating-point constant: using the hexadecimal prefix (0x or 0x,0 is the number 0, not the letter O), substituting p or p for the preceding E or E, and the order code represents the 2 of the order of the second square . For example:
0xb.1ep5
where b is equal to one in the decimal, 1e equals 1/16 plus 14/256, P5 equals 2 5, which is 512. This floating-point literal is converted to decimal: (one + 1/16 + 14/256) * 2 5 = 5692
Note: Not all compilers support this new format for C99!
4. output floating-point number
format qualifier %f command printf functions output float and double floating-point numbers in decimal form; the %e command printf function outputs float and double floating-point numbers in exponential form; The%a or %a command printf function to C99 the new hexadecimal format output, but not all compilers support it. If you want to output a long double floating-point number, use %lf, %le, %la, or %la. For example:
/* showfloat.c– uses two forms to represent the floating-point number * *
#include <stdio.h >
int main (void)
{
float var_f = 5.0;
Double VAR_DF = 3.14e2;
Long double var_ld = 6.51e-5;
printf ("%f is equal to%e\n", Var_f, Var_f);
printf ("%f is equal to%e\n", VAR_DF, VAR_DF);
printf ("%lf is equal to%le\n", Var_ld, VAR_LD);
return 0;
}
The output is as follows:
5.000000 is equal to 5.000000e+00
314.000000 is equal to 3.140000e+02
0.000065 is equal to 6.510000e-05
Note: The above is the output that I get from Suse Linux 10 with the GCC 4.02 compile run. If you run this program using dev-c++ 4.9.9.2 compilation, you cannot output var_ld normally. Probably because Dev-c++ uses the compiler gcc, long double is 96-bit, and it uses the printf function in the function library to treat the long double as 64-bit.
5. floating point Overflow (Overflow) and underflow (underflow)
Suppose that in your compiler, float can only reach 3.4e38, if you have the following statement:
float Toobig = 3.4E38 * 100.0f;
printf ("%e\n", Toobig);
This must lead to overflow! Because Toobig cannot represent the product of 3.4E38 and 100.0f. The consequences of the overflow were not defined in the past, but now C stipulates that if an overflow occurs, a special value representing Infinity is generated. Therefore, the value of the Toobig will eventually become a special value that represents infinity . In turn, the printf function will output words like INF or infinity.
divide a floating-point number with a very small absolute value and cause the precision of the floating-point number to be reduced, called underflow . For example, suppose that 3.1415e-10 is divided by 10 and then becomes 0.3141e-10, which is the underflow.

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.