Let’s Talk IOS (the seventh round of OC basic type code demonstration)

The judges, we introduced the basic types of OC in the second round. Some judges said that the introduction was not detailed enough, so I summarized all the basic types using graphics. Please refer to the content in the following picture.

Insert picture description here

After reading the summary of the types, we will demonstrate how to use these types through specific codes.

  1 #import <Foundation/Foundation.h>
  2 
  3 int main ()
  4 {
    
    
  5     int intValue = 3;
  6     short int shortValue = 4;
  7     long int longValue = 5;
  8         
  9     NSLog(@"int: %d, short: %d, long: %ld",intValue,shortValue,longValue);
 10     NSLog(@"size int: %lu, short: %lu, long: %lu",sizeof(intValue),sizeof(shortValue),sizeof(longValue));
 11 
 12     float floatValue = 3.1;
 13     double doubleValue = 5.3;
 14     NSLog(@"float: %.2f, double: %.2f",floatValue,doubleValue);
 15     NSLog(@"size float: %lu, double: %lu",sizeof(floatValue),sizeof(doubleValue));
 16     
 17     char charValue = 'a';
 18     NSLog(@"char: %c,%i, size: %lu",charValue,charValue,sizeof(charValue));
 19     
 20     bool boolValue = YES;
 21     if(boolValue)  
 22         NSLog(@"bool value is true");
 23     else
 24         NSLog(@"bool value is false");
 25         
 26     intValue = (int)floatValue;
 27     NSLog(@"change value: %i",intValue);
 28     
 29     return 0;
 30 } 

In the code, we first define three types of integer variables, int, short, and long, two floating-point variables, float and double, char type variables, and bool type variables. Then output the corresponding values ​​of these variables through the terminal, and we also output the space occupied by these three types of variables in the memory. The int type occupies 4 bytes, the short type occupies 2 bytes, the long type occupies 8 bytes, the float type occupies 4 bytes, the double type occupies 8 bytes, and the char type occupies 1 byte. OC The grammar does not specify the memory space occupied by each type, this is related to the specific machine. There will be different results on different types of machines. I suggest you try it out and see how your computer allocates byte space for these different types of variables.

Some judges said: I can use these types without knowing how much space they occupy. The judge's understanding is not accurate, because after knowing the size of the space occupied by the type, the maximum and minimum values ​​of the type can be known, and it is also easy to understand the overflow caused by the assignment and conversion between different types. For example, the int type occupies 4 bytes, then its maximum value is 2 to the 31st power minus 1. If this value is exceeded when assigning it, then overflow will occur.

In the code, we use parentheses to coerce a floating-point type to an integral type. Here we only demonstrate how to perform a coercive type conversion. In fact, we don’t use parentheses to cause type conversion. This conversion is automatic. When actually writing programs, we don't recommend type conversion. If you must perform type conversion, you need to pay attention to the value range of the type, otherwise it will cause an error in the operation result after an overflow.

Regarding the integer type, you can use unsigned to modify it. After modification, it represents an unsigned integer type, which means that the values ​​of integer variables are all positive numbers and 0. This is easy to understand, and we will not demonstrate it in the code.

Char can also be output as a character or integer value. For special characters like quotation marks, you need to use the escaped "\" character to output. This is the same as the C language, and we will not demonstrate it in the program. Some officials said that everything feels the same as the C language. This feeling is right. After all, OC is compatible with the C language, but there are still differences, such as the value of a Boolean variable, which becomes YES and NO in OC.

Compile the above program and run it, you can see the following results:

2020-10-25 15:42:30.850 ex.out[1202:66929] int: 3, short: 4, long: 5
2020-10-25 15:42:30.850 ex.out[1202:66929] size int: 4, short: 2, long: 8
2020-10-25 15:42:30.850 ex.out[1202:66929] float: 3.10, double: 5.30
2020-10-25 15:42:30.850 ex.out[1202:66929] size float: 4, double: 8
2020-10-25 15:42:30.850 ex.out[1202:66929] char: a,97, size: 1
2020-10-25 15:42:30.850 ex.out[1202:66929] bool value is true
2020-10-25 15:42:30.850 ex.out[1202:66929] change value: 3

Look at the officials, in this chapter, we have demonstrated the use of the basic types of variables in the first OC through the code, and finally we make an overview of the knowledge in the article:

  • 1. The basic types of variables in OC are divided into: integer, floating-point, character and Boolean, the detailed content can be seen in the summary in the picture;
  • 2. The space occupied by the type in OC is related to the machine, and different machines have different performances;
  • 3. Variables of different types can be converted to each other, and the conversion can be performed automatically or forcedly, but pay attention to the value range of the variable to avoid overflow;
  • 4. OC is compatible with C, except for the value of Boolean type, the usage of most types is the same.

Look at the officials, if you want to know what is going on, listen to the next breakdown!

Guess you like

Origin blog.csdn.net/talk_8/article/details/109279606