Note: This C language topic is a prelude to iOS development. To enable programmers with object-oriented language development experience to quickly get started with the C language. If you have no programming experience or are not interested in C and iOS development, ignore
C language has rich data types, so it is suitable for writing databases, such as DB2 and Oracle are written in C language.
The data types in C language can be roughly divided into the following types:
I. Variables
Like other languages, the C language uses variables to store the values used in the calculation process. Any variable must first define the type before use. Why must we define it first? Because the type of the variable determines the storage space occupied by the variable, the variable type is defined to allocate an appropriate storage space for the variable to store data. For example, if you are of the char type, it is enough to allocate only one byte to you. There is no need to allocate 2 bytes, 3 bytes, or more storage space.
The following table describes the storage space occupied by basic data types in a 16-bit compiler environment. Understanding these details will be helpful for learning pointers and arrays in the future.
Note that:
1. The use of local variables is slightly different from that of Java
1> after you declare a local variable in Java, the variable is used if it is not initialized and assigned a value. The Compiler reports an error directly.
Row 3 reports an error because variable a is not initialized.
2> in C language, after you declare a local variable, it can be used without initialization assignment.
1 #include <stdio.h>2 3 int main()4 {5 int b; 6 printf("%d", b);7 return 0;8 }
But this is very dangerous. We do not recommend this. Most people should think that the value of variable B should be 0, but it is not. Because the system assigns a value to variable B at will, the result is junk data.
The printed result of the above Code is: Therefore, local variables must be initialized and assigned before use. This is the safest way.
* If it is a global int type variable, the system will assign a value of 0 by default.
2. the char value range is:
ASCII charactersOr
-128 ~ 127Integer
Therefore, there are two ways to assign values to uppercase letters A using char:
// Method 1 char c = 'a'; // method 2 char c = 65;
The preceding two methods are equivalent, because the ASCII value of uppercase letter A is exactly 65. Click to view all values of the ASCII code table.
3. char can only store one character
A Chinese character or string needs to be stored in a character array, because a Chinese character occupies two characters, and a string consists of one or more characters.
Therefore, the following statements are incorrect:
Char c1 = 'I'; char c2 = '2016'; char c3 = "123 ";
Ii. Type Modifier
We can also add modifiers before the basic data types, which are also called delimiters.
There are four types of modifiers:
- Short Type
- Long
- Signed type
- Unsigned
1. Usage demonstration
These modifiers are most often used to modify the int type (int can be omitted)
1 // the two statements below are equivalent 2 short int s1 = 1; 3 short s2 = 1; 4 5 // The following two statements are equivalent to 6 long int l1 = 2; 7 long l2 = 2; 8 9 // two long10 long ll = 10 can be used consecutively; 11 12 // The following two statements are equivalent to 13 signed int si1 = 3; 14 signed I2 = 3; 15 16 // The following two statements are equivalent to 17 unsigned int us1 = 4; 18 unsigned us2 = 4; 19 20 // you can also use two modifiers 21 signed short int ss = 5; 22 unsigned long int ul = 5;
2. short and long
1> short and long can provide integer numbers of different lengths, that is, they can change the value range of integer numbers. For example, the short value range is-32768 ~ 32767. The value range of long is-2147483648 ~ 2147483647
2> of course, the data storage length will also change. For example, in a 64-bit compiler, short occupies 2 bytes (16 bits), int occupies 4 bytes (32 bits), and long occupies 8 bits (64 bits ). There are many compilers in the world. In different compiler environments, the value range and occupied length are different. Fortunately, ANSI \ ISO has the following rules:
* Short and int must be at least 16 bits (2 bytes)
* Long must be at least 32 bits (4 bytes)
* The short length cannot be greater than int, And the int length cannot be greater than long.
* Char must be 8 bits (1 byte). After all, char is the minimum data type that we can use for programming.
3. signed and unsigned
1> signed indicates signed, including positive, negative, and 0; unsigned indicates unsigned, including only positive and 0. For example, the value range of signed is-32768 ~ 32767, the value range of unsigned is 0 ~ 65535. Of course, different compilers have different value ranges.
2> In fact, the difference between signed and unsigned is whether the highest bit of signed is used as a symbol bit. It does not change the data length, that is, the number of bytes, as short and long do,
4. signed and unsigned can also modify char, long can also modify double
unsigned char c1 = 10;signed char c2 = -10;long double d1 = 12.0;
Iii. Storage length of basic data types in different compiler Environments
Red represents common data types