"0 Basic Learning iOS development" "02-c language" 07-Basic data type

Source: Internet
Author: User
Tags control characters integer numbers modifiers

directory of this document
    • I. Range of values
    • Two, Char
    • Three, specifier
    • Iv. Automatic type Promotion
    • V. Coercion type conversion

C has a rich data type, so it is well suited for writing databases, such as DB2, Oracle and other large databases are written in C. 4 of the most commonly used basic data types are available: char, int, float, double, and using these data types, we can define the corresponding variables to store the data. This tells you a bit more about the usage details of the basic data types.

Go back to the top one, take a range of values

We already know that the storage space for different data types is not the same. For example, in a 64bit compiler environment, the char type occupies 1 bytes, and the int type occupies 4 bytes. The byte length is different, contains the bits number is different, can represent the data range also is different. Therefore, an int type can represent a range of data that is definitely larger than the char type. The following is a simple calculation of the value range of the int type in the 64bit compiler environment.

1. Calculate the range of values for the int type

The int type occupies 4 bytes, so altogether 32 bits, then the value range should be: 0000 0000 0000 0000 0000 0000 0000 0000~1111 1111 1111 1111 1111 1111 1111 1111, which is 10 binary of 0 ~ 232-1. But the int type is positive or negative, including positive and negative numbers, how do you mean negative numbers? is to take the highest bit as the sign bit, when the highest bit is 0 is positive, the highest bit is 1 is negative. namely: 1000 0000 1001 1011 1000 0000 1001 1011 is a negative number, 0000 1001 0000 1101 0000 1001 0000 1101 is a positive number. Since the highest bit is 0, the maximum positive number is 0111 1111 1111 1111 1111 1111 1111 1111, or 231-1. The smallest negative number is 1000 0000 0000 0000 0000 0000 0000 0000, which is 231 (why is this value?). Can be based on the previous section of the negative number of the binary form, to the conversion of their own, see 1000 0000 0000 0000 0000 0000 0000 0000 is not-231. Do not have to struggle, do not affect the writing code, know that there is such a thing will be finished). Therefore, the value range for the int type is-231 ~ 231-1.

Note: This projection process is not mastered, the general knowledge of the process on the line, and this conclusion does not have to remember, roughly know the scope of the line.

2. Range of values for various data types

The range of values for the int type is already gone, so the range of other data types can be the same.

(Note: float and double because it is a decimal, they are stored in a very different way, so they take the value range of the algorithm is very different, here do not introduce, do not have to master.) The E38 means multiplying by 10 by 38, e-38 means multiplying by 10 minus 38 times. )

The above table lists only situations in the 64bit compiler environment. If your compiler is 16bit or 32bit, the range of values for these data types must be different. For example, int type, in 16bit compiler environment is occupied 2 bytes, a total of 16bit, so the value range of int type is:-215 ~ 215-1.

3. Numerical Cross 1> Example Demo

As you've seen before, each data type has its own range of values. If a variable is assigned a value that exceeds the range of values, the consequences can be disastrous.

1 #include <stdio.h>2 3 int main () 4 {5     int c = 1024x768 * 1024x768 * 1024x768 * 4;6     7     printf ("%d\n", c); 8     return 0;9}

As we all know, the maximum value that the int type can hold is 231-1. In line 5th, the variable c of type int is assigned a value greater than 231-1:232 (1024 is 210)

First look at the output in the terminal:, you can see the value of the output is 0.

2> Results Analysis

We can simply analyze why 232 is assigned to the variable c after the output is 0. The binary form of 232 is: 1 0000 0000 0000 0000 0000 0000 0000 0000, a total of 33-bit binary numbers. Variable c occupies 4 bytes, can only accommodate 32-bit binary numbers, and memory addressing is from large to small, so the variable C in memory storage is 0000 0000 0000 0000 0000 0000 0000 0000, that is 0, the first 1 is not the variable C.

3> Conclusion

It can be found that if the scope of the variable is exceeded, then the loss of precision, to get "junk data" ("junk data" refers to the data is not what we want). However, sometimes we do want to store a large and large integer, which is larger than 231-1, what should we do? This will use the type specifier, which is discussed later in this talk.

Back to the top two, char1. Simple to use

Char is a flexible data type in the C language, called a "character type." Since it is called "character type", it must be used to store characters, so we can assign a character constant to a type variable.

1 #include <stdio.h>2 3 int main () 4 {5     char c = ' A '; 6     7     printf ("%c\n", c); 8     return 0;9}

In line 5th, a char type variable c is defined, and the character constant ' A ' is assigned to C. In line 7th, the character variable C is output to the screen, and%c means to output as a character.

Output Result:

2. Character constants must be enclosed in single quotes

1> The following syntax is wrong:

1 int Main () 2 {3     char c = a;4     return 0;5}

The compiler will report the error on line 3rd directly because the identifier a cannot be found. You write an uppercase a directly, and the compiler will think that a is a variable. So write ' A ' is correct, or in front of the 3rd line of code, define 1 variables named char of type A.

2> The following wording is also wrong:

1 int Main () 2 {3     char c = "A"; 4     return 0;5}

The "a" in line 3rd is not a character constant, but a string constant, and assigning the string "a" to the character variable C is a bad practice. strings and characters have different storage mechanisms, so "a" and "a" are fundamentally different.

3. Character variables can also be used as Integer variables

The 1-character variable takes 1 bytes, a total of 8 bits, so the value range is -27~27-1. Within this range, you can use the character variable as an integer variable entirely.

1 #include <stdio.h> 2  3 int main () 4 {5     char c1 = -10; 6      7     char c2 =; 8      9     printf ("c1= %d  c2=%d \ n ", C1, C2); 10     

Because the 9th line uses%d, the output is expressed in decimal integer format, and the result is:. Therefore, if you use an integer that is not very large, you can use char instead of int, which saves memory overhead.

4. Character variables can only store single-byte characters

There are actually 2 types of characters: single-byte characters and double-byte characters.

    • Single-byte character: a character that occupies 1 bytes in memory. Including 26 letters of the case, 10 Arabic numerals and other characters;
    • Double-byte character: a character that occupies 2 bytes in memory. Including Chinese, Japanese and Korean and other countries, such as Chinese characters.

The 1-character variable occupies only 1 bytes, so 1 character variables can store only 1 single-byte characters.

The following syntax is incorrect:

1 #include <stdio.h>2 3 int main () 4 {5     char c = ' ABCD '; 6     7     printf ("%c\n", c); 8     return 0;9}

The compiler warns you of the above code and does not give an error, so the program can run. Since variable C can store only 1 single-byte characters, the final variable C only stores ' D ' in ' ABCD '.

Output Result:

5. Character type variable cannot store Chinese characters

In memory, 1 Chinese characters need to be stored in 2 bytes, while 1 character variables only occupy 1 bytes of storage space, so character variables cannot be used to store Chinese characters.

The following syntax is incorrect:

1 int Main () 2 {3     char c = ' male ';    4     return 0;5}

The compiler will report the error on line 3rd directly. Keep in mind one principle: single-byte characters must be enclosed in one quotation mark.

6.ASCII

When it comes to characters, we have to mention the concept of ASCII

1> ASCII is a set of computer coding systems based on the Latin alphabet and is the most versatile single-byte encoding system in the modern age, with the full name "American Standard code for Information interchange". The coding system, which looks like a high-level, is actually a set of character sets---.

The 2> ASCII character set includes: all uppercase and lowercase English letters, numbers 0 through 9, punctuation, and special control characters such as BACKSPACE, delete, tab, carriage return, total 128 characters, all "single-byte characters".

3> any data in the computer is stored in binary form, so each ASCII character is stored in memory in binary form and occupies only 1 bytes, and the value of the binary number is called the ASCII value of the ASCII character. For example, the binary form of capital A in memory is: 0100 0001, then its ASCII value is 65.

4> Below is an ASCII code character table with an ASCII value range of 0~127

5> we all know that 1 char variables occupy only 1 bytes of storage space, and all ASCII characters are single-byte characters, so char variables can store any ASCII characters. And when storing ASCII characters using char variables, you can use ASCII characters directly or ASCII values.

1 #include <stdio.h> 2  3 int main () 4 {5     char c1 =; 6      7     char c2 = ' A '; 8      9     printf ("c1=%c  c2=%c \ n ", C1, C2);     return 0;11}

In line 5th and 7, the character variable c1 and c2 are defined respectively. Obviously, the variable C2 stores the acii character ' a ', the variable C1 stores 65, and the ASCII value 65 corresponds to the ASCII character ' a ', so the variable c1 is stored ' a '.

Because the 9th line uses%c, the output is expressed in character format:

5> After the above example, you should know the difference between 6 and ' 6 '.

1 #include <stdio.h> 2  3 int main () 4 {5     char c1 = 6; 6      7     Char c2 = ' 6 '; 8      9     printf (" c1=%d  c2=%d \ n ", C1, C2);     return 0;11}

The 5th line assigns the variable c1 an integer 6, the 7th line assigns the variable C2 the character ' 6 ', and the ASCII value of ' 6 ' is 54.

Because the 9th line uses%d, the output is expressed in decimal integer format:

Back to top three, specifier 1. What is a descriptor

1> we already know that in the 64bit compiler environment, the value range of 1 int type variables is-231 ~ 231-1, and the maximum is 231-1. Sometimes we have to use an integer that is larger than 231-1, such as a 234 integer, and if you persist with an int type variable to store the value, you will lose precision and get garbage data. To solve this problem, the C language allows us to add some specifiers to variables of type int, and some specifiers can increase the length of the int type variable, so that the range of data that the INT type variable can store becomes larger.

The 2> C language provides the following 4 specifiers, 4 of which are keywords:

    • Short type
    • Long type
    • Signed-Signed
    • Unsigned non-symbolic

According to the use of classification, short and long is a class, signed and unsigned is a class.

2. Usage Demo

These specifiers are generally used to modify the int type, so you can omit int when used

1//The following two formulations are equivalent to 2 short  int S1 = 1, 3 short  s2 = 1; 4   5  //The following two notation is equivalent to 6  long int l1 = 2; 7  long L2 = 2; 8   9  //Can be used continuously 2 long10  long ll = 10;11  ///below two kinds of notation are equivalent to  signed int si1 = 3;14  Signed Si2 = 3;15  //The following two types of notation are equivalent to the  unsigned int US1 = 4;18  unsigned US2 =  4;19  //can also be Use 2 modifiers of signed short  int ss = 5;22  unsigned long int ul = 5;

Short in line 2nd of 1> is equivalent to short in lines 3rd.

2> See line 10th, you can use a continuous two long. The role of long will be explained later.

3> Note the lines 21st and 22, you can use two different specifiers at the same time. However, you cannot use the same type of modifiers at the same time, that is, you cannot use both short and long or you cannot use both signed and unsigned.

3.short and Long

1> Short and long can provide integer numbers of different lengths, that is, the range of values that can be changed for integer numbers. In the 64bit compiler environment, int occupies 4 bytes (32bit), the value range is -231~231-1;short occupies 2 bytes (16bit), the value range is -215~215-1;long occupies 8 bytes (64bit), the value range is -263~ 263-1

2> summarizes: In a 64-bit compiler environment, short accounts for 2 bytes (16 bits), int accounts for 4 bytes (32 bits), and Long is 8 bytes (64 bits). Therefore, if you use an integer that is not very large, you can use short instead of int, which saves memory overhead.

3> in the world of compilers, different compiler environment, int, short, long, the range of values and occupy the length is not the same. For example, in a 16bit compiler environment, a long takes only 4 bytes. Fortunately, ANSI \ ISO has the following rules in place:

    • Short and int are at least 16 bits (2 bytes)
    • Long is at least 32 bits (4 bytes)
    • The length of short cannot be greater than the length of int,int.
    • Char must be 8 bits (1 bytes), after all, char is the smallest data type we can program

4> can use 2 consecutive long, that is, a long long. In general, the range of long long is not less than long, for example, in a 32bit compiler environment, a long long takes up 8 bytes, and a long occupies 4 bytes. However, in the 64bit compiler environment, a long long is the same as long, which occupies 8 bytes.

5> another point to make clear is that short int is equivalent to Short,long int equivalent to long,long long int equivalent to long long

Use of 4.long note 1> constants

Both long and int can store integer constants, in order to differentiate between long and int, it is common to add a lowercase l to the integer constant, such as 100l, to represent a constant of type long. If it is a long long type, add 2 L, for example 100LL. If nothing is added, it is a constant of type int. Therefore, 100 is a constant of type int, 100l is a constant of type long, and 100LL is a long long type constant.

1 int Main () 2 {3     int a = 5; 4      6     long B = 100l; 7      8     long long C = 100LL; 9           

Variables A, B, and C ultimately store values that are 100, except that the bytes used are not the same, variable a uses 4 bytes to store 100, and variable B and C use 8 bytes to store 100.

In fact, you directly assign 100 to a long type of variable is also no problem, so use. Because 100 is a constant of type int, as long as there are 4 bytes, it can be stored, and a long type of variable B has 8 bytes, it can certainly be installed 100.

1 int Main () 2 {3     long B = 100;4     5     return 0;6}

2> output
1 #include <stdio.h>2 3 int main () 4 {5     long a = 100000000000l;6     7     printf ("%d\n", a); 8     return 0; 9}

In line 5th, a long type variable A is defined, and the value of a is tried out on line 7th. Note that this is using%d, which means output in decimal integer format,%d will output a as int type, it thinks a is 4 bytes. Because A is a long type, it takes 8 bytes, but when you output a, only 4 bytes of content are output, so the output is:

It's also the legendary junk data.

So how can I output a long type in full? The format character%ld should be used

1 #include <stdio.h>2 3 int main () 4 {5     long a = 100000000000l;6     7     printf ("%ld\n", a); 8     return 0;9}

Note that in line 7th, the double quotation mark is%ld, which indicates the output of 1 long integers, at which point the output is:

If it is a long long type, it should be used%LLD

1 #include <stdio.h>2 3 int main () 4 {5     long long a = 100000000000ll;6     7     printf ("%lld\n", a); 8     Retu RN 0;9}

5.signed and unsigned

1> first to be clear: Signed int is equivalent to signed,unsigned int equivalent to unsigned

The difference between 2> signed and unsigned is whether their highest bit is to be used as a sign bit, and does not change the length of the data, that is, the number of bytes, as short and long do.

    • Signed: Represents a symbol, which means that the highest bit is used as the sign bit, so it includes positive, negative, and 0. In fact, the highest bit of int is the sign bit, already includes the positive and negative number and 0, so signed and int are the same, signed equivalent to signed int, also equivalent to int. The value range of signed is 231 ~ 231-1
    • Unsigned: represents unsigned, which means that the highest bit is not used as a sign bit, so it does not include negative numbers. Under the 64bit compiler environment, int occupies 4 bytes (32bit), so the value range of unsigned is: 0000 0000 0000 0000 0000 0000 0000 0000 ~ 1111 1111 1111 1111 1111 1111 11 11 1111, i.e. 0 ~ 232-1

6.signed, unsigned can also be modified Char,long can also be modified double

Know that there is such a thing on the line.

1 unsigned char c1 = 10;2 signed char c2 = -10;3 4 long double d1 = 12.0;

7. Storage space occupied by different data types

Back to top four, automatic type lifting 1. What is automatic type promotion

Let's take a look at the following operation

1 #include <stdio.h> 2  3 int main () 4 {5     int a = ten; 6      7     Double d = a + 9.5; 8      9     printf ("%f \ n ", d);     return 0;12}

1> defines a variable a of type int in line 5th, assigning an integer 10.

2> then takes the value 10 of a in line 7th, plus the floating-point number 9.5, where an "addition operation" is made, and the "sum" is assigned to D. So the value of D should be 19.5.

3> in line 9th uses the format character%f to output the floating-point variable D, which is reserved for 6 decimal places by default. The output is:

4> seems to be such a simple operation, in fact, contains some grammatical details inside. Strictly speaking, values of the same data type can be performed (such as addition operations), and the result of the operation remains the same data type. The 7th line is that the value 10 of the variable A is of type int (4 bytes) and 9.5 is a double type (8 bytes). Obviously, 10 and 9.5 are not the same data types. In a logical sense, 10 and 9.5 are not allowed for addition operations. However, the system automatically makes an "automatic type boost" for a type that consumes less memory, and it also promotes 10 to a double type. That is, it would have been 4 bytes to hold 10, now instead of 8 bytes to store 10. Therefore, both 10 and 9.5 are now stored in 8 bytes, all of which are double types and can then be operated on. and assign the result of the operation to a variable d of type double.

5> Note that, after the 7th line of code, the variable A is still of type int and does not become a double type. What is the type of 1 variables at the time it is defined? Automatic type promotion is only performed during the operation.

2. Common automatic type Promotion
1 int Main () 2 {3     float a = ten + 3.45f;//int promoted to float 4      5     int b = ' a ' + +;//char promoted to int 6      7     Dou ble c = 10.3f + 5.7; Float promoted to double 8      9     return 0;10}

1> Note that the 5th line, the system will promote the character ' a ' to the int type data, that is, the ASCII value of ' a ' followed by the addition operation with 32. The ASCII value of ' A ' is 65, so the value of variable B is 65+32=97.

2> This automatic type promotion, know that there is such a thing on the line, do not rote this rule, because the system will automatically perform this operation.

Back to top v. Coercion type conversion 1. What is coercion type conversion

Let's take a look at the following code

1 #include <stdio.h>2 3 int main () 4 {5     int i = 10.7;6     7     printf ("%d \ n", i); 8     return 0;9}

1> Note on line 5th, we assign a 8-byte floating-point number 10.7 to an integer variable i with only 4 bytes of storage space. As you can imagine, it would be a loss of precision to plug a 8-byte content into 4 bytes. In the 7th row the value of the variable i is output, the output is:

The output value is 10, which is inevitable.

2> There is also a bit of syntax detail, in fact, the 5th line does a "forced type conversion" operation: Because the left is the int type of variable i, then will be forced to convert the double type of 10.7 to int type 10, and the converted value to the integer variable i. Because the C language is not strict syntax restrictions, so the system will be automatically cast, if the other syntax is a strict language, such as Java, the 5th line of code has long been an error.

3> If you write Strictly, it is obvious that "forced type conversion" should be written as follows:

1 #include <stdio.h>2 3 int main () 4 {5     int i = (int) 10.7;6     7     printf ("%d \ n", i); 8     return 0;9}

Note Line 5th, preceded by 10.7, adds an (int) that represents the data that is cast to the type int. So there is absolutely no grammatical problem. Anyway, when you convert a floating-point data to Integer data, you lose the value of the fractional part.

2. Common forced-type conversions
1 int Main () 2 {3     int a = 198l;//long is converted to int 4      5     char b = zero;//int is converted to char 6      7     int c = 19.5f; float converted to int 8      9     return 0;10}

This coercion type conversion, know that there is such a thing on the line, do not rote this rule, because many times the system will automatically perform this operation.

3. Other uses

The cast that you see earlier seems to be "big type" to "small type", which is not the same, it can be changed from "small type" to "big type"

1 int Main () 2 {3     int a = 10;4     5     Double b = (double) a  + 9.6;6     7     return 0;8}

Note Line 5th, after casting the value of a to a double type, adds the addition operation with 9.6. This way, the system does not have to perform an "automatic type boost" operation. In fact, you can not be strong, because the system will do an "automatic type promotion" operation, the value of variable A is 10 promoted to a double type. Know that this is the use of the line, some places will be used in the future.

"0 Basic Learning iOS development" "02-c language" 07-Basic data type

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.