Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter2 Constants, Variables, and Data Types

Similar presentations


Presentation on theme: "Chapter2 Constants, Variables, and Data Types"— Presentation transcript:

1 Chapter2 Constants, Variables, and Data Types

2 2.1 Introduction In this chapter, we will discuss
constants (integer, real, character, string, enum) ,symbolic constants Variables (name, declare, assign) Fundamental data type ( int, float, double, char, void) 常量 ( 整型、实型(浮点型)、字符、字符串、枚举、符号常量 ) 变量(命名、声明、赋值) 基本数据类型(整型、浮点型、双精度型、字符型、空类型)

3 Objective To be able to distinguish the constants of different data type To be able to name, declare,initialize and assign values to variables To become familiar with fundamental data types. 区分不同数据类型的常量 变量的命名、声明、初始化和赋值 基本数据类型

4 2.1 Character set Letters Digits :All decimal digits 0…9
uppercase A…Z lowercase a…z Digits :All decimal digits 0…9 Special characters Table2.1 on page23 White spaces 2.1 字符集 字母 数字 特殊符号 空字符

5 2.3 C Tokens Keywords Identifiers Constants Strings Special symbols
operators 关键字 标识符 常量 字符串 特殊符号 操作符

6 2.4 Keywords and Identifiers
Table2.3 on page24 Must in lowercase Identifiers User-defined names Consist of letters, underscore(_), and digits First character must not be digit Can’t use a keyword Can’t contain white space 关键字(keyword): 也称保留字,预先定义具有特定含义的标识符,全部小写字母 标识符:只能由字母、数字和下划线组成,不能以数字开头;严格区分大小写;不能是C语言的关键字;不能包含空白符

7 2.4 Keywords and Identifiers
Which of the following are valid identifiers Max first_name n1/n2 3row double _34 Boy num girl-num

8 2.7 Data Types Fig.2.4 on page31 Primary( fundamental) data types
Integer Floating point ( float and double ) Character Void Fig.2.4 on page31

9 2.7 Data Types Integer signed /unsigned int short int long int
Floating point float double long double Character signed / unsigned void

10 2.7 Data Types Size and range of basic data types Table 2.7 on page31

11 2.5 Constants Integer Real Character String

12 Integer constants Decimal integer Octal integer Hexadecimal integer
Consist of 0 through 9, +, - Octal integer Consist of 0 through 7, with a leading 0 Hexadecimal integer Consist of 0 to 9, and a through f preceded by 0x or 0X 整型常量:三种表示方法 十进制整数:0-9,+,- 八进制整数:0-7,以0开头 十六进制整数:0-9,a-f,或A-F,以0x开头

13 Integer constant The largest integer value is machine-dependent
Qualifiers U or u: unsigned integer l or L: long integer UL or ul: unsigned long integer Short integer 整数的范围:与机器有关 修饰符:整型数加后缀u或U表示无符号整型数;加后缀l 或L表示长整型

14 short is no longer than int and that long is no shorter than int.
设计一个程序,看一下整数越界会输出什么? (%d输出整数,%u输出无符号整数。)

15 Integer constant Example2.1 Representation of integer constants on a 16-bit computer. #include<stdio.h> main() { printf("Integer values\n\n"); printf("%d %d %d\n",32767, , ); printf("\n"); printf("Long integer values\n\n"); printf("%ld %ld %ld\n",32767L, L, L); }

16 Real constant (double)
(1) decimal point Consist of 0 through 9, . ,+ , - , , , (2)Exponential notation mantissa e exponent .56e3 2.3e E-3 The exponent must be an integer number Suffixes f or F indicate a float constant L or l indicate a long double “Default values of constants” on page35 (1)十进制小数形式 (2)指数形式 Mantissa: 尾数 Exponent: 指数

17 Floating-Point Round-off Errors
Take a number, add 1 to it, and subtract the original number. What do you get? You get 1. A floating-point calculation, may give another answer:

18 Character constant A character enclosed within ‘ ’
‘6’ ‘=‘ ‘;’ ‘ ‘ Character constants have integer values, For example ‘a’ and 97 ‘0’ and 48 Backslash character constants Table2.5 on page28 ‘\ooo’ and ‘\xhh’ 字符型常量具有整数值,为与其对应的ASCII码 ‘\ooo’表示三位八进制数,

19 String constants a sequence of characters enclosed in “ ”, for example
“hello” “34” “+()” “x” “\n” “x” and ’x’

20 2.6 variables A variable is data name that may be used to store a data value. Variable names correspond to locations in the computer's memory Every variable has a name, a type, a size and a value Whenever a new value is placed into a variable, it replaces (and destroys) the previous value Reading variables from memory does not change them

21 2.8 Declaration of variables
The declaration of variables must be done before they are used. declaration form data-type v1,v2,…,vn; for example int count; float sum; double ratio; char ch; 变量的声明 变量必须先声明,后使用 声明形式: 变量类型 变量1,变量2,…,变量n;

22 Initialization of variables
To initialize a variable means to assign it a starting, or initial, value. In C, this can be done as part of the declaration. char ch=‘ ’; int cows = 32, int dogs, cats = 94; 变量初始化 可以在声明变量的同时进行初始化

23 variable_name=value;
2.10 Assignment statement values can be assigned to variables using the assignment operator = as variable_name=value;

24 User-defined type declaration
the keyword “typedef” is used to rename an existing data type. typedef type identifier; for example typedef int integer; typedef float real; real sum1, sum2; integer count;

25 Enumerated Types You can use the enumerated type to declare symbolic names to represent integer constants. declarations: enum spectrum {red, orange, yellow, green, blue, violet}; enum spectrum color;


Download ppt "Chapter2 Constants, Variables, and Data Types"

Similar presentations


Ads by Google