Presentation is loading. Please wait.

Presentation is loading. Please wait.

Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei

Similar presentations


Presentation on theme: "Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei"— Presentation transcript:

1 Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei

2 C basics memory Variable, constant Integer Floating-point numbers
Expressions Type conversion Other data types Beiginning C / Qinpei Zhao 2018/11/28

3 Memory 1 kilobyte (or 1KB) is 1,024 bytes.
1 megabyte (or 1MB) is 1,024 kilobytes. 1 gigabyte (or 1GB) is 1,024 megabytes. Why 1,024, not 1,000? Beiginning C / Qinpei Zhao 2018/11/28

4 Memory distribution Code segment Constant segment Global data segment
代码区 存放程序的二进制代码 Binary code of the program 所有常量均存放在常量区,程序结束后由OS释放 All constants, released by OS when the program is over Constant segment 常量区 Global data segment 全局数据区 全局变量和静态变量,即使是函数内部的静态局部变量,程序结束后由OS释放 Global and static variables Heap segment 堆区 由程序员分配和释放,若程序员不释放,程序结束后可能由OS释放。new, malloc Allocated and released by the programmer Buffer segment 缓冲区 Stack segment 栈区 由编译器自动分配和释放,存放:函数内部的局部变量和函数的参数值 Allocated and released by compiler System Kernel 系统内核 Beginning C / Qinpei Zhao 2018/11/28

5 Variable 变量 #include <stdio.h> int main(void) {
printf(“My salary is $1000 per month\n"); return 0; } #include <stdio.h> int main(void) { int salary = 1000; printf(“My salary is $%d per month\n“, salary); return 0; } Beginning C / Qinpei Zhao 2018/11/28

6 How to use variables? int salary; salary = 1000;
printf(“My salary is $%d per month\n“, salary); two parameters separated by comma ‘,’ para1: control string / format string para2: variable “salary” %d indicates integer What if the variable is not initialized? Beginning C / Qinpei Zhao 2018/11/28

7 Variable related Variable 全局变量 declare and define e.g., int a;
types global variable local variable naming convention numbers underscore characters 下划线 letters no keywords concept changeable during the running of the program declaration and reference basic operators data types integer float character * / % declare and define e.g., int a; assignment e.g., a = 10; 局部变量 addition, subtraction multiplication, division modulus Beginning C / Qinpei Zhao 2018/11/28

8 Declaring variables Must declare variables before use
Variable declaration int n; float phi; int a, b, c; int – integer data type float – floating point data type Many other types (later) Beginning C / Qinpei Zhao 2018/11/28

9 Naming variables A variable name is a sequence of one or more uppercase or lowercase letters, digits, and underscore characters (_) that begins with a letter (_ counts as a letter). e.g., hunter, R, Dip64, _king, 8billion, 4_digits, include, Mary-Lou, Hash! Case sensitive, e.g., computer, Computer Variable names are defined with some flexibility. It’s worth calling them something that gives you a clue to what they indicate. variable name length should be upper to 31 characters, but better not be too long. Beginning C / Qinpei Zhao 2018/11/28

10 Using variables Beginning C / Qinpei Zhao 2018/11/28

11 Arithmetic statements
arithmetic operators: + - * / % Modulus (%): calculates the remainder after dividing the value of the expression on the left of the operator by the value of the expression on the right (remainder operator), e.g., 12 % 5 = 2 unary minus (-): negative becomes positive, and vice versa arithmetic expression (算术表达式): 1 + 2 x + y / z – 3 45 / 7 Unary operator: 一元运算符 Beginning C / Qinpei Zhao 2018/11/28

12 Variables and Memory the amount of memory occupied by variables of a given type will always be the same on a particular machine a variable of a given type on one computer may occupy more memory than it does on another the amount of memory occupied by variable of these types depends on the particular compiler you’re using Beginning C / Qinpei Zhao 2018/11/28

13 unsigned variables unsigned integer types can be used when values that cannot be negative are used, e.g., the number of person Using unsigned type doesn’t provide more values than the corresponding signed type, but it allow numbers in a twice large range Beginning C / Qinpei Zhao 2018/11/28

14 Specify integer constants
long Big_Number = L; long below_sea_level = L; long long really_big_number = LL; unsigned int count = 100U; unsigned long value = UL; hexadecimal form: 0x99 or 0X99 Beginning C / Qinpei Zhao 2018/11/28

15 Floating-point values
Float-point variables hold values that are written with a decimal point. or 5e-16 Beginning C / Qinpei Zhao 2018/11/28

16 Floating-point values (cont.)
float ht = 1.003; printf(“The tree is %f height.”, ht); Control the number of decimal places printf(“The tree is %.2f height.”, ht);  1.00 general form of the format specifier for floating-point values: %[width][.precision][modifier]f width: the total number of characters in the output precision: the number of decimal places that are to appear after the decimal point modifier: L when the value is type long double %-10.4f 左对齐,字段宽度10个字符,小数点后面4位数 Beginning C / Qinpei Zhao 2018/11/28

17 More complicated expressions
Preprocessing directive 预编译指令 Beginning C / Qinpei Zhao 2018/11/28

18 More complicated expressions
Define as a variable, but to tell the compiler that the value is fixed and must not be changed. Beginning C / Qinpei Zhao 2018/11/28

19 Knowing the limits 64-bit operation system Beginning C / Qinpei Zhao
2018/11/28

20 Knowing the limits Beginning C / Qinpei Zhao 2018/11/28

21 sizeof operator How many bytes are occupied by a given type by using the sizeof operator. Beginning C / Qinpei Zhao 2018/11/28

22 Choosing the correct type
Beginning C / Qinpei Zhao 2018/11/28

23 type conversion Explicit conversion (显式类型转换)
Automatic conversion(自动转换类型) Implicit conversion (隐式类型转换) Rules for implicit conventions Beginning C / Qinpei Zhao 2018/11/28

24 More numeric data type - character
char = 1 byte, for unsigned type, ranges from [0, 255]; for signed type, ranges from [-128, +127] char letter = ‘A’; char digit = ‘9’; char exclamation = ‘!’; char newline = ‘\n’; char letter = ‘C’; (ASCII = 67) Character input char ch = 0; scanf(“%c”, &ch); Character output Printf(“The character is %c and the code value is %d”, ch, ch); See Appendix B American Standard Code for Information Interchange (ASCII). Beginning C / Qinpei Zhao 2018/11/28

25 More numeric data type - enumeration
define a new integer type where variables of the type have a fixed range of possible values. enum Weekday {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }; declare a new variable of type Weekday and initialize it: enum Weekday today = Wednesday; enum Weekday {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday} today, tomorrow; enum Weekday {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday} today = Monday, tomorrow = today +1; choosing numerator values The enumerators that follow an enumerator with an explicit value will be assigned successive integer values (Monday = 1, Tuesday = 2, …) Beginning C / Qinpei Zhao 2018/11/28

26 More numeric data type – boolean (布尔型)
represents true (1) or false (0) In C, it’s _Bool, not bool (occupied by C++) #include <stdbool.h> to use the data type Or define as follows: #define bool _Bool #define true 1 #define false 0 Beginning C / Qinpei Zhao 2018/11/28

27 Data types in C Beginning C / Qinpei Zhao 2018/11/28

28 Mathematical functions
Beginning C / Qinpei Zhao 2018/11/28

29 Designing a program problem
The height of a tree is of great interest to many people. For one thing, if a tree is being cut down, knowing its height tells you how far away safe is. This is very important to those with a nervous disposition. Your problem is to find out the height of a tree without using a very long ladder, which itself would introduce risk to life and limb. To find the height of a tree, you’re allowed the help of a friend-preferably a short friend. You should assume that the tree you’re measuring is taller than both you and your friend. Trees that are shorter than you present little risk, unless they’re of the spiky kind. problem Beginning C / Qinpei Zhao 2018/11/28


Download ppt "Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei"

Similar presentations


Ads by Google