C language - the basic data types

1.1 Data: Data type keyword

K & C gives 7 relating to types of keywords. C90 standard adds two keywords, C99 standard has added three keywords.

K & C given keyword C90 standard add keywords C99 standard add keywords
int signed _Bool
long void _Complex
short   _Imaginary
unsigned    
char    
float    
double    

 In the C language, use the keyword int to represent the basic integer types. After three keywords (long, short, and unsigned) and C90 for providing a substantially new signed integer type of variant, e.g. unsigned short int and long long int. char keyword is used to specify the letters and other characters (such as, #, ¥,% and *). Further, char types can also represent a smaller integer. float, double, and long double represents a number with a decimal point. _Bool type indicates a Boolean value (true or false), _ Complex and _Imaginary represent complex and imaginary number.

Storage by the computer can be divided into two basic types: type integer and floating-point type.

1.2 int type

int type is a signed integer, i.e. the value must be an integer int type, may be a positive integer, zero or negative integer. Value range varies depending on the computer system. In general, store an int to take up a machine word. Thus, the early 16-bit IBM PC compatible int used to store a 16-bit value, which is in the range -32768 to 32767. The current 32-bit personal computer generally, thus storing a 32-bit value int.

 1 #include <stdio.h>
 2 void main(void){
 3     
 4     int ten = 10;
 5     int two = 2;
 6 
 7     printf("Doing it right: ");
 8     printf("%d minus %d is %d\n",ten,2,ten-two);
 9     printf("Doing it wrong: ");
10     printf("%d minus %d is %d\n",ten);
11 
12     getchar();
13 }

You may be used printf (print function of type int value). % d indicates an integer in the print line position. % d conversion specifier become, he named printf () What format should be used to display a value. Each format string% d match the list of variables to be printed in the corresponding int value.

1.2.2 display octal and hexadecimal

In a C program, you can either use can also display several different hexadecimal. To use a different different binary conversion instructions. It is displayed in decimal numbers, using % D ; to octal numbers, using % O ; displayed in hexadecimal numbers, using % X . Also, to display the prefix for each binary numbers and 0,0x 0X, respectively, must % O #,% # X, # X-% .

 1 #include <stdio.h>
 2 void main(void){
 3     
 4     int x = 100;
 5 
 6     printf("dec = %d;octal = %o;hex = %x\n",x,x,x);
 7     printf("dec = %d;octal = %#o;hex = %#x\n",x,x,x);
 8 
 9 
10     getchar();
11 }
Output Results: 
On Dec = 100 ; Octal = 144 ; hex = 64 On Dec = 100 ; Octal = 0144 ; hex = 0x64

1.2.3 Other integer types

C language provides three basic integer types subsidiary modified keywords: short, long, and unsigned. You should remember the following:

  • short int type (abbreviated short) may be less than the space occupied by int type, commonly the case with the smaller value to save space.
  • long int or long storage may occupy more than int, larger values ​​for the occasion.
  • long long int or long long (C99 standard is added) may take up storage space than long, larger values ​​for the occasion. This type constitutes at least 64.
  • unsigned int or unsigned used only for non-negative worth occasions. Bits used to represent the sign of another binary bit presently used, it may represent a larger unsigned integer number.
  • In C90 standard, add or unsigned long unsigned long int and unsigned short int or unsigned short type. C99 standard has added unsigned long long int or unsigned long long

Use multiple integer types of reasons:

Why short type "probably" less than the space occupied by the int type, long type "likely" than the space occupied by more than int type? Because the C language provides only a short space occupied by no more than int, long the storage space of not less than int. This provision is to adapt to different machines.

Now, the most common settings on the computer is a personal, long long accounted for 64, long accounted for 32, short accounted for 16, int accounting for 16 or 32 (according to the natural word of the computer may be). In principle, these four types represent four different sizes, but in practical use, there is generally some overlap between types.

1.2.4 long long long constants and constants

Typically, program code numbers are stored for use to type int. If you exceed the range of int type can be represented, the compiler treats it as a long int. If the number exceeds the maximum long can be expressed, the compiler is treated as an unsigned long, if it is not big enough, it is considered a long long or unsigned long long type.

In some cases, the compiler needs to store a small number of long type. For example, some values ​​C standard requires a long. Should a smaller constant as long type treated, may be added at the end worth l (lowercase L) or L suffix. We recommend the use of L. Thus, the 16-bit int, long 32-bit system, would 7 stored as 16-bit, 32-bit stored as the 7L.

Similarly, in a system supporting long long type, LL suffix may be used to represent a value of type long long as 3LL. Further, u suffix U or unsigned long long, such as 5ull, 10ULL like.

1.2.5 Print short, long, long long and unsigned types

Unsigned int type value of the print, using% u conversion specification; values ​​of the print type long, using conversion specification% ld. L prefix may be used in front of the x and o,% lx represents long integer type printing in hexadecimal format,% lo represents the type of long print octal format. Note that although C allows the use of constants suffix uppercase or lowercase, but the explanation can only be converted to lowercase,

There are a variety of C language printf () format. For short type, you can use the prefix h. % Hd display in decimal integer type short,% hd display represents the type of short integer in octal. h and prefix may be used with u, for unsigned type. For example,% lu means print unsigned long value type.

1.3 Use of characters: char type

char type for storing characters (e.g., letters or punctuation), but from a technical perspective, is an integer of type char. Because the char type is actually stored integer rather than characters. Computer using the digital code to handle character, i.e. specific characters represent specific integers.

The C language is defined as 1 byte char type occupied bits (bit) number, so whether it is 16-bit or 32-bit systems, the char type can be used.

1.3.1 printable characters

printf () function with the% c specified character to be printed.

1.4 _Bool type

C99 standard is added _Bool type, for a Boolean value indicating that the logical values ​​true and false. Since C represents a true value of 1, the value 0 represents false, so _Bool type is actually a type integer. But in principle, it only occupies a storage space.

Portable 1.5 Type: stdint.h and inttypes.h

C language provides a number of useful integer type. However, some types of functions in different systems were not the same. C99 adds two headers stdint.h and inttypes.h, in order to ensure that the same type of C language functions in the system. (To understand)

1.6 float,double和long double

By default, the compiler assumes that floating-point constants are of type double precision.

Behind the float plus the F or f suffix override the default setting, the compiler will be regarded as floating point type float constants. L or L suffix using such digital be a long double.

C99 standard adds a new floating-point constants format - hexadecimal floating-point constants, that is, before the hexadecimal number prefix plus hexadecimal (0x or 0X), with p and and were replaced with P e E, a power of 10 instead of by a power of 2. (I.e., p notation)

For example: 0xa.1fp + 10 [hex equals a decimal 10, .1f plus 1/16 15/256, p + 10 is a value of 2 ^ 10, or represents the 1024.0xa.1fp + 10 (10 + 1/16 + 15/256) * 1024 (i.e. decimal 10364.0)]

Note that not all compilers support this feature of the C99

1.6.2 Print float values

printf () function using % f conversion type float and double floating point instructions printed in decimal notation with % e print floating point exponential notation. If the system supports floating point hexadecimal format, can be used instead of a and e, respectively A and E. Use long double type to use% Lf,% Le,% La conversion instructions.

 1 #include <stdio.h>
 2 void main(void){
 3     float aboat = 32000.0;
 4     double abet = 2.14e9;
 5     long double dip = 5.32e-5;
 6 
 7     printf("%f can be written %e\n",aboat,aboat);
 8     printf("And it's %a in hexadecimal powers of 2 notation\n",aboat);
 9     printf("%f can be written %e\n",abet,abet);
10     printf("%Lf can be written %Le\n",dip,dip);
11 
12     getchar();
13     
14 }

1.6.3 overflow and underflow floating-point values ​​(temporarily omitted)

1.7 and imaginary complex (skip)

Guess you like

Origin www.cnblogs.com/mylearning-log/p/10932198.html