
So for an 8-bit char, range signed −128 ::: 127 unsigned 0 ::: 255 Again, C has these types as they are useful in real programs Unsigned integers are often used as simple bit patterns rather than integers per se, e.g., in cryptography There is a signed keyword if you want to be explicit: e.g., signed char and signed int Types There are also unsigned variants of the integer types: unsigned char, unsigned int and so on 1 / 102 Again, C has these types as they are useful in real programs Unsigned integers are often used as simple bit patterns rather than integers per se, e.g., in cryptography There is a signed keyword if you want to be explicit: e.g., signed char and signed int Types There are also unsigned variants of the integer types: unsigned char, unsigned int and so on So for an 8-bit char, range signed −128 ::: 127 unsigned 0 ::: 255 2 / 102 Unsigned integers are often used as simple bit patterns rather than integers per se, e.g., in cryptography There is a signed keyword if you want to be explicit: e.g., signed char and signed int Types There are also unsigned variants of the integer types: unsigned char, unsigned int and so on So for an 8-bit char, range signed −128 ::: 127 unsigned 0 ::: 255 Again, C has these types as they are useful in real programs 3 / 102 There is a signed keyword if you want to be explicit: e.g., signed char and signed int Types There are also unsigned variants of the integer types: unsigned char, unsigned int and so on So for an 8-bit char, range signed −128 ::: 127 unsigned 0 ::: 255 Again, C has these types as they are useful in real programs Unsigned integers are often used as simple bit patterns rather than integers per se, e.g., in cryptography 4 / 102 Types There are also unsigned variants of the integer types: unsigned char, unsigned int and so on So for an 8-bit char, range signed −128 ::: 127 unsigned 0 ::: 255 Again, C has these types as they are useful in real programs Unsigned integers are often used as simple bit patterns rather than integers per se, e.g., in cryptography There is a signed keyword if you want to be explicit: e.g., signed char and signed int 5 / 102 Types Integers Exercise. %d is the printf specifier for signed int. Find the specifiers for the other integer types Exercise. Find out what happens to the value when you overflow an unsigned char and a signed char Exercise. An unadorned int is signed. Find out whether an unadorned char has a sign or not Exercise. Find out the sizes of the integer types on machines you have access to Exercise. Read up on the operators that operate on the bits of the integer types: &, |, <<, >>, etc. 6 / 102 Correct: C does not have character as a separate type like some other languages We shall see in a moment that C does not have a string type either! In fact, it would probably be better to think of char as a byte since many compilers have an 8-bit char Aside: technically a “byte” is not necessarily 8 bits; use the word “octet” to mean precisely 8 bits But the name “char” indicates a popular use of this type: characters encoded as ASCII integers Types Integers So char is an integer type? 7 / 102 We shall see in a moment that C does not have a string type either! In fact, it would probably be better to think of char as a byte since many compilers have an 8-bit char Aside: technically a “byte” is not necessarily 8 bits; use the word “octet” to mean precisely 8 bits But the name “char” indicates a popular use of this type: characters encoded as ASCII integers Types Integers So char is an integer type? Correct: C does not have character as a separate type like some other languages 8 / 102 In fact, it would probably be better to think of char as a byte since many compilers have an 8-bit char Aside: technically a “byte” is not necessarily 8 bits; use the word “octet” to mean precisely 8 bits But the name “char” indicates a popular use of this type: characters encoded as ASCII integers Types Integers So char is an integer type? Correct: C does not have character as a separate type like some other languages We shall see in a moment that C does not have a string type either! 9 / 102 Aside: technically a “byte” is not necessarily 8 bits; use the word “octet” to mean precisely 8 bits But the name “char” indicates a popular use of this type: characters encoded as ASCII integers Types Integers So char is an integer type? Correct: C does not have character as a separate type like some other languages We shall see in a moment that C does not have a string type either! In fact, it would probably be better to think of char as a byte since many compilers have an 8-bit char 10 / 102 But the name “char” indicates a popular use of this type: characters encoded as ASCII integers Types Integers So char is an integer type? Correct: C does not have character as a separate type like some other languages We shall see in a moment that C does not have a string type either! In fact, it would probably be better to think of char as a byte since many compilers have an 8-bit char Aside: technically a “byte” is not necessarily 8 bits; use the word “octet” to mean precisely 8 bits 11 / 102 Types Integers So char is an integer type? Correct: C does not have character as a separate type like some other languages We shall see in a moment that C does not have a string type either! In fact, it would probably be better to think of char as a byte since many compilers have an 8-bit char Aside: technically a “byte” is not necessarily 8 bits; use the word “octet” to mean precisely 8 bits But the name “char” indicates a popular use of this type: characters encoded as ASCII integers 12 / 102 To reiterate: 'A' is a way of writing an integer value, typically 65 when using the usual ASCII encoding; the two ways of writing sixty-five are then more-or-less interchangeable char c = 'Z' - 'A' + 1; is valid C We use the single quote syntax as it is easier (we don’t have to look up the relevant value) and it is portable: not everyone uses ASCII Types Integers The C syntax for characters is single quotes: 'A' is the integer value that encodes for the character “A” 13 / 102 char c = 'Z' - 'A' + 1; is valid C We use the single quote syntax as it is easier (we don’t have to look up the relevant value) and it is portable: not everyone uses ASCII Types Integers The C syntax for characters is single quotes: 'A' is the integer value that encodes for the character “A” To reiterate: 'A' is a way of writing an integer value, typically 65 when using the usual ASCII encoding; the two ways of writing sixty-five are then more-or-less interchangeable 14 / 102 We use the single quote syntax as it is easier (we don’t have to look up the relevant value) and it is portable: not everyone uses ASCII Types Integers The C syntax for characters is single quotes: 'A' is the integer value that encodes for the character “A” To reiterate: 'A' is a way of writing an integer value, typically 65 when using the usual ASCII encoding; the two ways of writing sixty-five are then more-or-less interchangeable char c = 'Z' - 'A' + 1; is valid C 15 / 102 Types Integers The C syntax for characters is single quotes: 'A' is the integer value that encodes for the character “A” To reiterate: 'A' is a way of writing an integer value, typically 65 when using the usual ASCII encoding; the two ways of writing sixty-five are then more-or-less interchangeable char c = 'Z' - 'A' + 1; is valid C We use the single quote syntax as it is easier (we don’t have to look up the relevant value) and it is portable: not everyone uses ASCII 16 / 102 Types Integers Exercise. Find out which character encoding your machine uses Exercise. Is 'A' + 1 always 'B'? Exercise. Is 'A' < 'B' always true? Exercise. What about 'A' < 'a' or 'a' < 'A'? 17 / 102 • float also called “single precision float” • double also called “double precision float” • long double is sometimes supported These overwhelmingly conform to a particular standard for floating point representations, namely IEEE 754 Many machines support double in hardware, so this is the “natural” size in programs: but not always Types Floating Point C has a few floating point types 18 / 102 These overwhelmingly conform to a particular standard for floating point representations, namely IEEE 754 Many machines support double in hardware, so this is the “natural” size in programs: but not always Types Floating Point C has a few floating point types • float also called “single precision float” • double also called “double precision float” • long double is sometimes supported 19 / 102 Many machines support double in hardware, so this is the “natural” size in programs: but not always Types Floating Point C has a few floating point types • float also called “single precision float” • double also called “double precision float” • long double is sometimes supported These overwhelmingly conform to a particular standard for floating point representations, namely IEEE 754 20 / 102 Types Floating Point C has a few floating point types • float also called “single precision float” • double also called “double precision float” • long double is sometimes supported These overwhelmingly conform to a particular standard for floating point representations, namely IEEE 754 Many machines support double in hardware, so this is the “natural” size in programs: but not always 21 / 102 That said, there is a significant class of hardware out there that does it differently, e.g., fixed-point arithmetic Types Floating Point It turns out that the flexibility of having explicitly undefined sizes works against you when you want to do numerical analysis with floating point, so pretty much all hardware uses IEEE 754 Type bytes float 4 double 8 long double 16 22 / 102 Types Floating Point It turns out that the flexibility of having explicitly undefined sizes works against you when you want to do numerical analysis with floating point, so pretty much all hardware uses IEEE 754 Type bytes float 4 double 8 long double 16 That said, there is a significant class of hardware out there that does it differently, e.g., fixed-point arithmetic 23 / 102 IEEE 754 also has many other curious features, such as support for infinities and “not a number”s These have their expected behaviours, e.g., 1:0=0:0 returns infinity; sqrt(-1.0) returns a NaN Also, there is a signed zero, namely ±0:0.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages102 Page
-
File Size-