ch 4 - 1 chapter 4 basic data types and variables 4.1 basic data types in c table 4.1 introduction...

24
Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type Size Description char 1 byte Characters or small integer variables int 2 or 4 bytes Integer values float 4 bytes Floating point numbers double 8 bytes Floating point numbers

Upload: everett-davis

Post on 17-Jan-2016

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 1

Chapter 4 Basic Data Types and Variables

4.1 Basic Data Types In C

TABLE 4.1 Introduction to Basic Data Types in C

Type Size Description

char 1 byte Characters or small integer variables

int 2 or 4 bytes Integer values

float 4 bytes Floating point numbers

double 8 bytes Floating point numbers

Page 2: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 2

4.2 CHARACTERS

The Standard for Text: ASCII(American Standard Code for Information Interchange) was first defined by the American National Standards Institute in 1986. In this code, each letter of the alphabet, punctuation mark, and decimal number is assigned a unique 7-bit code number. With 7 bits, 128 unique symbols can be coded. See Table 4.2.

Page 3: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 3

0 nul 16 dle ^P 32 48 0 64 @ 80 P 96 ` 112 p1 soh ^A 17 dcl ^Q 33 ! 49 1 65 A 81 Q 97 a 113 q2 stx ^B 18 dc2 ^R 34 " 50 2 66 B 82 R 98 b 114 r3 etx ^C 19 dc3 ^S 35 # 51 3 67 C 83 S 99 c 115 s4 eot ^D 20 dc4 ^T 36 $ 52 4 68 D 84 T 100 d 116 t5 enq ^E 21 nak ^U 37 % 53 5 69 E 85 U 101 e 117 u6 ack ^F 22 syn ^V 38 & 54 6 70 F 86 V 102 f 118 v7 bel ^G 23 etb ^W 39 ' 55 7 71 G 87 W 103 g 119 w8 bs ^H 24 can ^X 40 ( 56 8 72 H 88 X 104 h 120 x9 tab ^I 25 em ^Y 41 ) 57 9 73 I 89 Y 105 I 121 y10 lf ^J 26 eof ^Z 42 * 58 : 74 J 90 Z 106 j 122 z11 vt ^K 27 esc ^[ 43 + 59 ; 75 K 91 [ 107 k 123 {12 np ^L 28 fs ^\ 44 ' 60 < 76 L 92 \ 108 l 124 |13 cr ^M 29 gs ^] 45 - 61 = 77 M 93 ] 109 m 125 }14 so ^N 30 rs ^^ 46 . 62 > 78 N 94 ^ 110 n 126 ~15 si ^O 31 us ^_ 47 / 63 ? 79 O 95 _ 111 o 127 del

TABLE 4.2 ASCII Character Format

Page 4: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 4

4.2.1 Defining Character Variables in C

Program 4.1: C Program Containing Variables

int main ( void ) { /* Beginning of the (main) block. */

Char cAChar; /* We put variable definitions here. */

cAChar = 65 ; /* Executable statement : assign the */

/* code for ‘A’ to “cAChar” */

} /* End of (main) block */

Page 5: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 5

4.2.2 Character Constants

cAChar = ‘A’ ; /*assign ‘A’ to character */ /* variable “cAChar”

*/

Page 6: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 6

4.2.3 Escape Characters

Page 7: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 7

Remark 4.2

Page 8: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 8

4.3 INTEGERS

Consider for example the declarationsint iPennies;

int iCounter;

Initial Values can also be supplied in definitions, as with

int iCounter = 0;

int iNickels = 5;

Page 9: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 9

4.3.1 Short and Long Integersshort <= int <= long

Page 10: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 10

4.3.2 Unsigned Integers

Ex:

unsigned int uiPennies;

unsigned uUpCounter;

Page 11: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 11

4.4 SINGLE AND DOUBLE PRECISION FLOATING POINT NUMBERS

Page 12: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 12

4.4.1 Floating Point Variables

Floating point variables and constants:

3.4, -45.33, 2.714,

Exponential notation:

3.0e-25, 4.5e+05, 2.345678e+19,

Page 13: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 13

Figure 4.2

Page 14: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 14

Table 4.5

Page 15: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 15

Program 4.2 : Print size of some basic data types

Page 16: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 16

接續 Program 4.2

Page 17: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 17

4.5 ENUMERATION DATA TYPESenum weekday { Mon, Tue, Wed, Thu, Fri, Sat, Sun }; or

enum dayofweek { Mon=1, Tue, Wed, Thu, Fri, Sat, Sun };

Program 4.3

Page 18: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 18

Page 19: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 19

4.6 VARIABLE ATTRIBUTES: TYPE, ADDRESS, NAME, AND VALUE

int iVal = 10;

Type Name Value

Page 20: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 20

Program 4.4

Page 21: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 21

Page 22: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 22

4.7 VARIABLE NAMING CONVENTIONS

A variable name cannot start with a digit, and it cannot have the same as a reserved word.

Page 23: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 23

An appropriate name name for our count variable:

int iCount ;

unsigned int uiCount ;

unsigned uCount;

Page 24: Ch 4 - 1 Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1

Ch 4 - 24