Data Type and constant

Source: Internet
Author: User

You have encountered the basic data type int of objective-C. In retrospect, variables declared as int type can only be used to save integer values, that is, they do not contain decimal places.

Objective-C also provides three basic data types: float, double, and char. Variables declared as float can store floating-point numbers (that is, values containing decimal places ). The double type is the same as the float type. Generally, the former indicates that the range is about twice that of the latter. The Char data type can store a single character, such as the letter A, number 6, or a semicolon (more details will be discussed later ).

In objective-C, any number, single character, or string is usually called a constant. For example, the number 58 represents a constant integer, and the string @ "programming in objective-C
Is Fun. "represents a constant string object. An expression completely composed of constant values is called a constant expression. Therefore, the following expression is a constant expression, because each item of the expression is a constant value:

128 + 7-17

However, if I is declared as an integer variable, the expression is not a constant expression:

128 + 7-I

Int type

An integer constant is a sequence of one or more numbers. The negative sign before the sequence indicates that the value is a negative number. The values 158,-10, and 0 are valid Integer constants. Spaces cannot be inserted in the middle of a number, and comma (, therefore, 12,000 is an invalid integer constant and must be written as 12000 ).

Each value, whether it is a character, integer, or floating-point number, has its corresponding value range. This value field is related to the amount of memory allocated by the system for a specific type of value. Generally, this amount is not specified in the language. It usually depends on the computer that runs. Therefore, it is called the number of devices or machines. For example, an integer can occupy 32 bits on a computer or use 64-bit storage. If you use 64-bit storage, integer variables can store values larger than 32-bit.

 

Note:

In Mac OS X, you can select whether the application is compiled in 32-bit or 64-Bit mode. In the previous case, an int occupies 32 bits; in the latter case, an int occupies 64 bits.

Float Type

Variables declared as float can store values that contain decimal places. To distinguish between floating-point constants, you can check whether it contains the decimal point. Values 3., 125.8, and-. 0001 are valid floating point constants. To display floating point values, use nslog to convert characters % f or % G.

Floating-point constants can also be expressed by the so-called scientific notation. The value 1.7e4 is the floating point value expressed by this notation, which represents the value 1.7 × 104.

As mentioned above, the double type is very similar to the float type, except that the double type variables can be stored in about twice the float variable.

Char type

The Char variable can store a single character. Put the character in a pair of single quotes to get the character constant. Therefore, 'A', ';', and '0' are valid character constants. The first constant represents the letter A, the second represents the semicolon, and the third represents the character 0, which is not equivalent to the number 0. Do not confuse character constants with C-style strings. character constants are a single character in single quotes, while strings are any number of characters in double quotes. As mentioned in chapter 3rd, strings prefixed with @ characters and placed in double quotation marks are nsstring string objects.

The character constant '\ n' (linefeed) is a valid character constant, although it seems to be in conflict with the rules mentioned above. This is because the backslash is considered a special symbol. In other words, the objective-C compiler regards the character '\ n' as a single character, although it actually consists of two characters. Other special characters begin with a backslash. In nslog calls, the format character % C can be used to display the value of the char variable.

In the code listing 4-1, the basic objective-C data type is used.

Code list 4-1

# Import <Foundation/Foundation. h>

 

Int main (INT argc, char * argv [])

{

@ Autoreleasepool {

Int integervar = 100;

Float floatingvar = 331.79;

Double doublevar = 8.44e + 11;

Char charvar = 'W ';

 

Nslog (@ "integervar = % I", integervar );

Nslog (@ "floatingvar = % F", floatingvar );

Nslog (@ "doublevar = % E", doublevar );

Nslog (@ "doublevar = % G", doublevar );

Nslog (@ "charvar = % C", charvar );

}

Return 0;

}

 

Code list 4-1 Output

Integervar = 100

Floatingvar= 331.790009

Doublevar = 8.440000e + 11

Doublevar = 8.44e + 11

Charvar = W

 

In the second line of program output, you will notice that the value assigned to floatingvar is 331.79, which is actually displayed as 331.790009. In fact, the actual displayed value is determined by the computer system in use. The reason for this inaccuracy is that the computer uses a special method to represent numbers. When using a calculator to process numbers, the same inaccuracy may occur. If you use a calculator to calculate 1 divided by 3, the result is. 33333333, which may end with some additional 3. This string 3 is the approximate value of the calculator's calculation of 1/3. Theoretically, there should be no limit of 3. However, this calculator can only store the numbers of these digits, which is the uncertainty of the computer. The same uncertainty also exists here: some floating point values cannot be exactly expressed in computer memory.

Restrictions: Long, long, short, unsigned, and signed

If you directly place the qualifier long before int declaration, the declared integer variables have extended Value Domains on some computers. An example of a long int statement is:

Long int factorial;

This statement declares the variable fractorial as an integer variable of long. Like float and double variables, the specific scope of long variables is also determined by the specific computer system.

To use nslog to display the long int value, you must use the letter L as the modifier before the integer format symbol. This means that the format symbol % Li will be displayed in decimal format long
Int value.

You can also use the long int variable or even the long double variable with a larger range of floating point numbers.

When the qualifier short is placed before the int declaration, it tells the objective-C compiler to declare a specific variable to store a fairly small integer. The main reason for using the short variable is to save memory space. When the programmer needs a large amount of memory and the amount of available memory is very limited, the short variable can be used to solve this problem.

The last qualifier that can be placed before the int variable is used when the integer variable is only used to store positive numbers. The following statements

Unsigned int counter;

Declare to the compiler that the variable counter is only used to save the positive value. By limiting the use of integer variables, it is used to store positive integers and can extend the range of integer variables.

ID type

The ID data type can store any type of objects. In a sense, it is a common object type. For example

Id graphicobject;

Declare graphicobject as an ID variable. You can declare a method to make it return values of the ID type, as follows:

-(ID) newobject: (INT) type;

This program line declares an instance method named newobject, which has a single integer parameter named type and a return value of ID type.

The ID type is an important data type frequently used in this book. The purpose of this type is to maintain the integrity of this book. The ID type is an important feature in objective-C. It is the basis of polymorphism and dynamic binding, these two features will be discussed in detail in chapter 9th "polymorphism, dynamic type and dynamic binding.

Table 4.1 summarizes the basic data types and restrictions.

Table 4.1 Basic Data Types

Type

Instance

Nslog characters

Char

'A', '\ N'

% C

Short int

-

% Hi, % HX, % Ho

Unsigned short int

-

% Hu, % HX, % ho % Hu, % HX, % Ho

Int

12,-97, 0xffe0, 0177

% I, % x, % O

Unsigned int

12u, 100u, 0 xffu

% U, % x, % O

Long int

12l,-2001, 0 xffffl

% Li, % lx, % Lo

Unsigned long int

12ul, 100ul, 0 xffeeul

% Lu, % lx, % Lo

Long long int

0xe5e5e5e5ll, 500ll

% LlI, % LLX, & llo

Unsigned long int

12ull, 0 xffeeull

% LlU, % LLX, % llo

Float

12.34f, 3.1e-5f, 0x1. 5p10, 0x1p-

% F, % E, % G, %

Double

12.34, 3.1e-5, 0x. 1p3

% F, % E, % G, %

Long double

12.34l, 3.1e-5l

% Lf, $ le, % LG

ID

Nil

% P

 

Note:

In table 4.1, an integer constant starting with 0 indicates that the constant is octal (base 8). It starts with 0x or (0x) indicates that it is hexadecimal (base 16, number 0x. 1p3 indicates a hexadecimal floating point constant. You don't have to worry about these formats. Here we just want to make a complete summary of the table. In addition, prefix F, L (L), u (u), and ll (LL) are used to explicitly indicate that constants are of the float, long, unsigned, and long types.

 

This article is excerpted from objective-C Programming (version 4th).

Published by Electronic Industry Publishing House

[Us] Stephen G. kochan (Stephen G. cochang)

Lin Ji
Translated by fan Jun Zhu Yixin

 

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.