You are on page 1of 73

Objective-C

2 Literals
Robert Chi
contact@robertchi.tw

Objective-C Programming 2

Objective-C Programming 2


o 123
o A
o This is a book

Objective-C Programming 2

(Integers)

(Floating Points)

Literals

(Texts)

(Booleans)
Objective-C Programming 2

Objective-C Programming 2


123

0123

0x123

Objective-C Programming 2


printf()

printf(Decimal: %d\n, 123);


printf(Octal: %o\n, 0123);

printf(Hexadecimal: %x\n, 0x123);


Objective-C Programming 2


%o %x

printf(Octal: %o\n, 0123);

123

printf(Hexadecimal: %x\n, 0x123);

123

printf(Octal: %#o\n, 0123);

0123

printf(Hexadecimal: %#x\n, 0x123);

0x123

Objective-C Programming 2

Lab 02-01

o printf()


o
o
o
o

Lab/Practices/ 02-01-IntLiteralRadix
printf() 123, 0123, 0x123
123
%#o, %#x %o, %x

Objective-C Programming 2

10

Lab 02-01

Objective-C Programming 2

11

/
123u

-123

Objective-C Programming 2

12

/
printf()

printf(Signed: %d\n, -123);


printf(Unsigned: %u\n, 123u);

Objective-C Programming 2

13

/
0 1

-7

Objective-C Programming 2

00000111

11111001

14

/
2s Complement

18
- 7
-----11

Objective-C Programming 2

18
+ (-7)
-------11

00010010
????????
--------------- 00001011

15

/
2s Complement

18
+ (-7)
-------11

00010010
11111001
------------1 00001011

-7

11

Objective-C Programming 2

16

/

o -7
o
-7 7
7 00000111
0 1 00000111 11111000
1s Complement
11111000 + 1 = 11111001

(-7) = 11111001

Objective-C Programming 2

17

/

o 1
1

01001011
11001010

o

0
1 1
0 / 1

72

01001000

-72

10111000

Objective-C Programming 2

18

o 0 1

1 1 0 1 0 0 1 1

11010011

11010011

= 27+26+0+24+0+0+21+20+

= - 00101101

= 128+64+16+2+1

= - (0+0+25+0+23+22+0+20)

211

= - (32+8+4+1)
=

Objective-C Programming 2

- 45
19

Lab 02-02

o printf()
o 0 1


o Lab/Practices/ 02-02-IntLiteralSign
o
printf()
printf()
o printf()
o printf()
Mac Magic Number Machine for Mac
-123 32 bits

Objective-C Programming 2

20

Lab 02-02

Objective-C Programming 2

21


123

Integers

16 2 bytes
32/64 4 bytes
4 bytes

123L

Long Integers

4 bytes

8 bytes

123LL

Long Long Integers



8 bytes

8 bytes

Objective-C Programming 2

22


printf()

printf(Integer: %d\n, 123);


printf(Long Integer: %ld\n, 123L);

printf(Long Long Integer: %lld\n, 123LL);


Objective-C Programming 2

23

sizeof( 123 )

printf(Size: %lu \n,

sizeof( 123L )

);

sizeof( 123LL )
sizeof()

Objective-C Programming 2

24

Lab 02-03

o
o sizeof


o Lab/Practices/ 02-03-IntLiteralSize
o printf()

o sizeof() bytes

Objective-C Programming 2

25

Lab 02-03

Objective-C Programming 2

26



o
o printf()

123
0123
0x123

123
123u

123
123L
123LL

Objective-C Programming 2

()
l
ll

d
u
o
x
27

Objective-C Programming 2

28


1.23F

1.23

1.23L

Objective-C Programming 2

29


printf()

printf(Float: %f\n, 1.23F);


printf(Double: %f\n, 1.23);

printf(Long Double: %Lf\n, 1.23L);


Objective-C Programming 2

30

1.23F

4 Bytes

1.23

8 Bytes

1.23L

16 Bytes

Objective-C Programming 2

31

Lab 02-04

o %f%Lf floatdoublelong double
o floatdoublelong double


o Lab/Practices/ 02-04-FloatLiteralSize
o printf() floatdoublelong double
o printf() floatdoublelong double

Objective-C Programming 2

32

Lab 02-04

Objective-C Programming 2

33


1.23

1.23e-10

1.23 x 10-10

Objective-C Programming 2

34


printf()

printf(Normal: %f\n, 1.23);


printf(Scientific: %e\n, 1.23e-5);

Objective-C Programming 2

35

Lab 02-05

o %f%e


o Lab/Practices/ 02-05-FloatLiteralNotation
o printf()

o printf()

o printf() specifiers%12.8f

12
o %12.8f

0 .

0 0 0 0 1 2 3 0
8

Objective-C Programming 2

36

Lab 02-05

Objective-C Programming 2

37

0 1
IEEE 754

14 bits
(exponent)

48 bits
(normalized mantissa)

(Sign bit of exponent)


(Sign bit of mantissa)

Objective-C Programming 2

38

0 1

o
o
o
o
o

IEEE 754


o (52.234375)10
= (110100.001111)2
= (0.110100001111)2 x 26
o IEEE 754
00000000000110
00000110100001111
000000000000011000000110100001111

Objective-C Programming 2

0 1

39



o Objective-C
o printf()
o 0 1

1.23F
1.23
1.23L

1.23
1.23e-5

12

.8

Objective-C Programming 2

f
e

40

Objective-C Programming 2

41

C
SZ Format

Objective-C Programming 2

Objective-C
NSString

42

La

Objective-C Programming 2

1 byte
Mac 4 bytes

2 byte
Mac 4 bytes

43

\141

Objective-C Programming 2

()

ASCII

44



o Escape Sequence

\n

New line

\t

Tab

\f

Form feed

\b

Backspace

\\

\\
\

Objective-C Programming 2

45


printf()
o %c

printf(Char: %c \n,

La

);

\141

Objective-C Programming 2

46


0 1
o Encoding 0 1
o

ASCII

American Standard Code for Information Interchange


1 byte
0= 4810A= 6510a= 9710\141=9710

Big5



2 bytes
= 1010010001000001 (0xA441)

Unicode

Universal Code

2 bytesUTF-8UTF-16 4 bytesUTF-32
= 10001110010110010x4E59

Objective-C Programming 2

0 1

47


Unicode
o
1991 10

1999 9 ISO-10646
o
UCS-2 (Universal Character Set 2 bytes)
o 2 bytes 65536
o U+hhhhh 16 U+4E59
o Objective-C \uhhhh \u4e59
o
UCS-4 (Universal Character Set 4 bytes)
o 4 bytes 42
o U+hhhhhhhh U+7FFFFFFF

Objective-C Programming 2

48

Unicode
o

UTF-8 UCS-2
o Universal Transformation Format Unicode
o ASCII 1 bytes U+0000 U+007F
(00000000 00000000 0zzzzzzz 0zzzzzzz (00-7F))
o 2 bytes U+0080 U+07FF
(00000000 00000yyy yyzzzzzz 110yyyyy 10zzzzzz)
o CJK 3 bytes U+0800 U+FFFF
(00000000 xxxxyyyy yyzzzzzz 1110xxxx 10yyyyyy 10zzzzzz)
o 4 bytes U+10000 U+10FFFF
(000wwwxx xxxxyyyy yyzzzzzz 11110www 10xxxxxx 10yyyyyy 10zzzzzz)
o ASCII
UTF-16 UCS-2
o ASCII 2 bytes U+0000 U+FFFF
(00000000 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx)
o 4 bytes U+10000 U+10FFFF
(U+10FFFF U+10000 = U+FFFFF 0000yyyy yyyyyyxx xxxxxxxx
110110yyyyyyyyyy 110111xxxxxxxxxx)
o ASCII
UTF-32 UCS-4
o 4 bytes Unicode
o
Mac OS X Unicode
UTF-8 UTF-8-Mac
UTF-8 4 bytes
4 bytes UTF-32

Objective-C Programming 2

49


Unicode Objective-C

printf(Unicode: %s\n, \u4e59);


printf(Unicode: %s\n, );
Unicode http://javawind.net/tools/native2ascii.jsp

Objective-C Programming 2

50

Lab 02-06

o
o


o Lab/Practices/ 02-06-CharLiteral
o printf() aLa\141\u4e59
o printf() aLa\141\u4e59

Objective-C Programming 2

51

Lab 02-06

Objective-C Programming 2

52


Hello

C
SZ Format

@Hello

Objective-C
NSString Format

Objective-C Programming 2

53


SZ Format
o String ended with Zero Format
o C

This is a book
T h i

B o o k \0

Objective-C Programming 2

54


NSString
o Objective-C Objects
o

NSString
Hello
length
UTF8String
lowercaseString
uppercaseString

Objective-C Programming 2

55


NSString
o [@Hello length]
Hello 5
o [@Hello UTF8String]
@Hello C SZ
o [@Hello lowercaseString]
NSString

o [@Hello uppercaseString]
NSString

Objective-C Programming 2

56



SZ Format printf(%s)

printf(%s\n, Hello);
NSString Format NSLog(@%@)

NSLog(@%@, @Hello);
NSLog() NSString NSString

Objective-C Programming 2

57

Lab 02-07

o Objective-C
o


o Lab/Practices/ 02-07-StringLiteral
o Line 7 / Line 8 printf() NSLog() Hello
@Hello
o NSString
@Hello
@Hello NSString SZ
@Hello
@Hello

Objective-C Programming 2

58

Lab 02-07

Objective-C Programming 2

59



o
o Unicode UCS-2 / UCS-4UTF-8 / UTF-16 / UTF-32
UTF-8-Mac
o = + NSString

printf(%c);

\141

ASCII

printf(%c);

La

printf(%c);

\u4e59

Unicode

printf(%s);

Hello

SZ

printf(%s);

@Hello

NSString

NSLog(@%@);

Objective-C Programming 2

60

Objective-C Programming 2

61


truefalse
George Boole

False

George Boole (1815 ~ 1864)

Objective-C Programming 2

62

true C
TRUE C
YES Objective-C

false C
FALSE C
NO
Objective-C

Objective-C Programming 2

63


0 1

true = TRUE = YES = 1



sizeof(true) = sizeof(TRUE) = 4 bytes
sizeof(YES) = 1 byte

false = FALSE = NO = 0

sizeof(false) = sizeof(FALSE) = 4 bytes
sizeof(NO) = 1 byte

0 1

Objective-C Programming 2

64

printf(True: %d\n, true);


printf(True: %d\n, TRUE);
printf(True: %d\n, YES);

Objective-C Programming 2

65

Lab 02-08

o Objective-C
o 0 1
o


o Lab/Practices/ 02-08-BoolLiteral
o printf()
o printf()

Objective-C Programming 2

66

Lab 02-08

Objective-C Programming 2

67



o Objective-C

true

4 bytes

printf(%d);

false

4 bytes

printf(%d);

TRUE

4 bytes

printf(%d);

FALSE

4 bytes

printf(%d);

YES

1 bytes

printf(%d);

NO

1 bytes

printf(%d);

Objective-C Programming 2

68



o
o
o
o

123

%d

0123

%o

0x123

%x

123u

%u

123L

%ld

123LL

%lld

Objective-C Programming 2

69

1.23F

%f

IEEE 754

1.23

%f

IEEE 754

1.23L

%Lf

16

IEEE 754

1.23e-5

%e

4 / 8 / 16

IEEE 754

Objective-C Programming 2

70

%c

ASCII / Unicode

\141

%c

ASCII

\n, \t,

%c

ASCII / Unicode

La

%c

ASCII / Unicode

\u4e59

%s

Unicode

Hello

%s

N/A
(length, not size)

SZ format

@Hello

%@
N/A
(NSLog() only) (length, not size)

Objective-C Programming 2

NSString format

71

true

%d

false

%d

TRUE

%d

FALSE

%d

YES

%d

NO

%d

Objective-C Programming 2

72

Objective-C Programming 2

73

You might also like