Bài giảng Introduction to Computer Programming (C language) - Chapter 3: Variables and Basic Data Types - Võ Thị Ngọc Châu

Content

 Introduction

 Data and Data Types

 enum Data Type

 struct Data Type

 Variables and Variable Declaration

 Constant Definition

 Expressions

 Operators

 Summary

Bài giảng Introduction to Computer Programming (C language) - Chapter 3: Variables and Basic Data Types - Võ Thị Ngọc Châu trang 1

Trang 1

Bài giảng Introduction to Computer Programming (C language) - Chapter 3: Variables and Basic Data Types - Võ Thị Ngọc Châu trang 2

Trang 2

Bài giảng Introduction to Computer Programming (C language) - Chapter 3: Variables and Basic Data Types - Võ Thị Ngọc Châu trang 3

Trang 3

Bài giảng Introduction to Computer Programming (C language) - Chapter 3: Variables and Basic Data Types - Võ Thị Ngọc Châu trang 4

Trang 4

Bài giảng Introduction to Computer Programming (C language) - Chapter 3: Variables and Basic Data Types - Võ Thị Ngọc Châu trang 5

Trang 5

Bài giảng Introduction to Computer Programming (C language) - Chapter 3: Variables and Basic Data Types - Võ Thị Ngọc Châu trang 6

Trang 6

Bài giảng Introduction to Computer Programming (C language) - Chapter 3: Variables and Basic Data Types - Võ Thị Ngọc Châu trang 7

Trang 7

Bài giảng Introduction to Computer Programming (C language) - Chapter 3: Variables and Basic Data Types - Võ Thị Ngọc Châu trang 8

Trang 8

Bài giảng Introduction to Computer Programming (C language) - Chapter 3: Variables and Basic Data Types - Võ Thị Ngọc Châu trang 9

Trang 9

Bài giảng Introduction to Computer Programming (C language) - Chapter 3: Variables and Basic Data Types - Võ Thị Ngọc Châu trang 10

Trang 10

Tải về để xem bản đầy đủ

pdf 85 trang xuanhieu 5240
Bạn đang xem 10 trang mẫu của tài liệu "Bài giảng Introduction to Computer Programming (C language) - Chapter 3: Variables and Basic Data Types - Võ Thị Ngọc Châu", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

Tóm tắt nội dung tài liệu: Bài giảng Introduction to Computer Programming (C language) - Chapter 3: Variables and Basic Data Types - Võ Thị Ngọc Châu

Bài giảng Introduction to Computer Programming (C language) - Chapter 3: Variables and Basic Data Types - Võ Thị Ngọc Châu
l variables 
 40 
Variables and Variable Declaration 
 Variable names actually correspond to 
 locations in the computer‟s memory. 
 A variable name in C is any valid identifier. 
  a series of characters consisting of letters, digits 
 and underscores (_) that does not begin with a digit 
 : _minNumber, global_counter, i1, i2 
 X: min#, 123Iteration, ThisVar., @g_Variable 
  of any length, but only the first 31 characters are 
 required to be recognized by C standard compilers 
  not a keyword in C 
 C is case sensitive. 
  Global_counter is different from global_counter. 41 
Variables and Variable Declaration 
 A data type of a variable is specified in its 
 declaration. 
 type_name variable_name_1 [= initial_value_1] 
 [, variable_name_2 [= initial_value_2]] 
  [, variable_name_n [= initial_value_n]]; 
 A compiler allocates memory for declared 
 variables up to the data type and its storage 
 class at run time. 
 A compiler associates variable_name to the 
 allocated memory. 
 A compiler sets initial_value to the content of 
 the allocated memory if initial_value exists. 42 
 Variable 
 Declaration 
000000000022FE36 
 0 
 i 
 memory43 
Variables and Variable Declaration 
 Global variables 
  Declared outside of all the functions 
  Globally accessed inside of any functions 
  Hold values throughout the lifetime of a program 
  Initialized by the system once defined 
 Local variables 
  Declared and locally accessed inside a function 
 (main, others) or block between the brackets 
  Should be defined immediately after the left 
 bracket that begins the body of a function/block 
  Exist only as long as the function or block where 
 the variables are declared is still executing 
  Not initialized by the system once defined 44 
 A value of each local variable 
Variables and Variablecan be set Declarationin its declaration. 
 Otherwise, local variables 
 start with random values in 
 their memory at run time. 
 45 
 Initialized values for global variables: 
 - char „\0‟ (i.e. NULL) 
 - int 0 
 - float 0 
 - double 0 
? - pointer NULL 
 All the bytes in memory are filled with zeros. 
 46 
Variables and Variable Declaration 
 The scope of a name is the part of the program within 
 which the name can be used. 
 A scope of a variable name is a region of the program 
 (function() {}, block {..}) where a variable can have 
 its existence. Beyond that, it cannot be accessed. 
 For a variable declared at the beginning of a function, 
 the scope is the function where the name is declared. 
  Local variables of the same name in different functions are 
 unrelated. 
  The same is true for the parameters of the function, which are 
 in fact local variables. 
 The scope of an external variable or a function lasts 
 from the point at which it is declared to the end of the 
 file being compiled. 47 
Variables and 
Variable Declaration 
 gChar2 is unable to be accessed 
 in the main function due to its 
 improper declaration place! 
 48 
 gChar 
 bChar 
cChar 
 49 
 The most “local” 
 variables will take 
 precedence over the 
 others. 
 How to refer to them? 
 Naming! 
Which aChar is printed? 
 50 
Variables and Variable Declaration 
 Where are variable values stored? 
  Storage of data in variables and arrays is temporary in 
 (registers and) RAM. That is such data is lost when a 
 program terminates. 
  Storage classes for different distinct memory areas 
 Variable Type Keyword Storage class Scope 
 Local variables auto (redundant) Automatic (default) Declared function/block 
 Register local variables register Register if possible. Declared function/block 
 If not, automatic 
 Static local variables static Static Declared function/block 
 Global variables Static Program 
 Global variables extern Static Program 
 Static global variables static Static File 
 Variables with dynamically malloc(), calloc(), Dynamic Variable‟s scope: local, 
 allocated memory free(), realloc() global 51 
Variables and Variable Declaration 
 Memory layout of a C program 
 Higher address 
 Command-line arguments 
 and environment variables 
 Local variables, arguments, Stack 
 grown/shrunk with function calls 
 Grown/shrunk with dynamic 
 allocation and de-allocation Heap 
 Uninitialized (static) global Uninitialized data Initialized to 
 variables, static local variables .bss zero by exec 
 Initialized (static) global variables, Initialized data Read from 
 static local variables, constants 
 .data program file 
 Machine code, often read-only Code by exec 
 .text 
 Lower address 
 bss = block started by symbol, better save space 
Variables and Variable Declaration 
 Memory areas in C 
  Constant data area 
 Read-only memory for string constants and other data 
 whose values are known at compile time, existing for the 
 lifetime of the program 
  Static area 
 Read-writable memory for extern/static variables existing 
 for the lifetime of the program 
  Stack area 
 Read-writable last-in-first-out memory for a local variable, 
 existing from the point where/when the variable is defined 
 and released immediately as the variable goes out-of-scope 
  Heap area 
 Memory dynamically allocated explicitly by programmers 53 
Constant Definition 
 Constants refer to fixed values that the 
 program may not alter during its 
 execution. 
 Constants can be of any of the basic data 
 types like an integer constant, a 
 floating constant, a character constant, a 
 string literal, or enumeration constants. 
 Constants are treated just like regular 
 variables except that their values cannot 
 be modified after their definition. 
 54 
Constant Definition 
 Defined by: 
  Using #define preprocessor 
 #define identifier value 
  Using const keyword 
 const type_name variable_name = value; 
  Using enum type 
 Integer constants represented by identifiers 
 enum [type_name] {identifier [= value], }; 
 55 
Constant Definition 
 Defined by: 
  Using #define preprocessor 
 #define MAX 50 
  Using const keyword 
 const short MAX = 50; 
  Using enum type 
 Integer constants represented by identifiers 
 enum min_max {min=0, MAX=50}; 
 enum {min=0, MAX=50}; 56 
 Expressions 
 An expression is simply a valid combination 
 of operators and their operands (variables, 
 constants, ) 
 Each expression has a value and a type. 
 Primary expressions 
  Identifier (variable, function, symbolic constant) 
  Constant 
 involves only constants 
 evaluated at during compilation rather than run-time 
 used in any place that a constant can occur 
  String literal 
  (expression) 
 type and value are identical to the enclosed expression 57 
 Expressions 
 Expression values are determined by the 
 operations in a certain order based on 
 precedence and associativity of each operator. 
 Expressions are grouped by the operators 
  Arithmetic expressions 
  Logical expressions 
  Relational expressions 
  Bitwise expressions 
  Assignment expressions 
  Conditional expressions 
   58 
Expressions 
 Valid expressions 
  12 + intVar – sqrt(23)*2 
  “This is an expression.” 
  „A‟ + 32 
  (12/4)%2 + 8 – floatVar 
   
 Invalid expressions 
  „ABC‟ 
  0”Wrong”1 
  intVar*2# - 10 
   59 
Operators 
 Arithmetic operators 
 Relational operators 
 Logic operators 
 Bitwise operators 
 Comma operator 
 Assignment operators 
 Type casting operator 
 Other operators 
 60 
Arithmetic Operators 
+, -, *, / All numeric types. Integer division yields integer results. 
% Integer types (including enum) only. 
 61 
Increment and Decrement 
Operators 
 Increment and decrement operators: ++, -- 
 preincrement 
 postincrement 
 predecrement 
 postdecrement 
int x=4, y=5; int x=4, y=5; 
++x – y = ?, x = ?, y = ? x++ – y = ?, x = ?, y = ? 
x = x + 1 = 5, increment 4 – 5 = -1, use 
5 – 5 = 0, use pre. x = x + 1 = 5, increment post. 
y = 5 y = 5 62 
 Relational Operators 
All numeric types. 
!!! EQUALITY with == 
 63 
Logic Operators 
 Logic operators: &&, ||, ! 
corresponding to AND, OR, NOT 
 The C language has no boolean data type. 
 Zero (0) is used to represent FALSE. 
 Non-zero ( 0) is used to represent TRUE. 
 1 && 2 1 1 || 2 1 !1 0 
 1 && 1 1 1 || 1 1 !2 0 
 1 && 0 0 1 || 0 1 !-2 0 
 0 && 0 0 0 || 0 0 !0 1 64 
Bitwise Operators 
The binary bitwise operators are used to manipulate the bits of integral 
operands (char, short, int and long; both signed and unsigned). 
Unsigned integers are normally used with the bitwise operators. 
Bitwise data manipulations are machine dependent. 65 
 Assignment Operators 
 Assignment operator: = 
 Assignment shorthand operators: 
 66 
RHS = right hand side 
 Assignment Operators 
 Assignment operator: = 
  copies the value from its right hand side to the 
 variable on its left hand side 
  also acts as an expression which returns the 
 newly assigned value 
1. Copy: 
 variable = RHS; int x=4, y=2; 
 x = y + 1; 
2. return: RHS y = (x = x + 10); 
 Data type of the variable and data type of RHS x = ? y = ? 
 must be the same. 
 Result: 
 Otherwise, data type of RHS will be casted to 
 data type of the variable. x = 13, y = 13 67 
Assignment Operators 
 Assignment operator: = 
 int x = 2, y = 3; int x = 2, y = 3; 
 float f = 1.5; float f = 1.5; 
 char c; char c; 
 y = 1.5 + 2*2 = 5.5 = 5; 
 y = f + x*2; 
 c = 5*20 + 2 + 8*1.5 
 c = y*20 + x + 8*f; 
 = 100 + 2 + 12.0 = 114.0 = 114; 
 f = (y = c - x*3); y = 114 – 2*3 = 108; 
 f = (y=108) = 108.0; 
 x = f/5.0; 
 x = 108.0/5.0 = 21.6 = 21; 
 x = ? y = ? f = ? c = ? x = 21? y = 108? f = 108.0? c = 114? 
 68 
Assignment Operators 
 Assignment operator: = 
 int x = 2, y = 3; int x = 2, y = 3; 
 float f = 1.5; float f = 1.5; 
 truncation 
 char c; char c; 
 y = 1.5 + 2*2 = 5.5 = 5; 
 y = f + x*2; 
 promotion 
 truncation 
 c = 5*20 + 2 + 8*1.5 
 c = y*20 + x + 8*f; 
 = 100 + 2 + 12.0 = 114.0 = 114; 
 f = (y = c - x*3); y = 114 – 2*3 = 108; 
 f = (y=108) = 108.0; truncation 
 x = f/5.0; 
 x = 108.0/5.0 = 21.6 = 21; 
 x = ? y = ? f = ? c = ? x = 21? y = 108? f = 108.0? c = 114? 
 69 
Assignment Operators 
 int x = 2, y = 3; 
 float f = 1.5; 
 char c; 
 y = f + x*2; 
 c = y*20 + x + 8*f; 
 f = (y = c - x*3); 
 x = f/5.0; 
 x = ? y = ? f = ? c = ? 
 70 
 Assignment Operators 
 Assignment shorthand operators: 
 variable operator= RHS; int x=4, y=5; Result: 
 variable = variable operator (RHS); x *= y – 2; x = 12; 
 x = x * (y-2); NOT: x = 18!71 
RHS = right hand side 
Comma Operators 
 Comma operator: , 
  A pair of expressions separated by a comma is 
 evaluated left-to-right, and the value of the left 
 expression is discarded. 
  The type and value of the result are the type 
 and value of the right operand. 
  All side effects from the evaluation of the left-
 operand are completed before beginning the 
 evaluation of the right operand. 72 
Comma Operators 
 Comma operator: , 
 The comma operator most often finds use 
 in the for statement. 
 The commas that separate function 
 arguments, variables in declarations, etc., 
 are not comma operators, and do not 
 guarantee left to right evaluation. 
 73 
Comma Operators 
 Comma operator: , 
 int intVar1, intVar2, i; 
 char charVar1 = „A‟, charVar2 = „B‟, charVar3; 
 for (i=1, intVar1=1; i<charVar1 && intVar1<charVar1; i++) {} 
 intVar1 = (charVar3 = „C‟, intVar2 = 2 + charVar3); 
 intVar1 = ? intVar2 = ? charVar1 = ? charVar2 = ? charVar3 = ? 
 charVar3 = „C‟; 
 intVar2 = 2 + charVar3 = 2 + „C‟ = 2 + 67 = 69; 
 intVar1 = 2 + charVar3 = 69; 
 charVar1 = „A‟; 
 charVar2 = „B‟; 74 
Conditional Operators 
 Conditional operator 
 ? : 
  Evaluate 
  If the value of is true (non-zero), 
 evaluate and return . 
  Otherwise, evaluate and return . 
 int x = 1, y = 4; - Evaluate: x<=y 1 <= 4 1 ( 0) 
 - Evaluate: x 
 int min; 
 - Return: 1 
 min = (x<=y) ? x : y; - Copy 1 to the variable min 
 min = ? min = 1; 75 
 Type Casting Operators 
 Type casting operator: (type) expression 
  Produces the value of expression in the type 
  Provides an explicit type conversion 
  Has the highest precedence 
 Type casting vs. Type promotion vs. Truncation 
 char < short < int < long < float < double < long double 
 promotion 
 truncation 
 76 
Operators and Expressions 
 77 
 Other Operators 
Name Operator Description Example 
sizeof sizeof(type), Returns the size (bytes) of sizeof(char) 
 sizeof(variable) a type or a variable 
 int anInt = 0; 
 sizeof(anInt); 
address &Variable Returns the address of the char aChar; 
 memory named Variable char* ptrChar; 
 ptrChar = &aChar; 
Dereferencing *Pointer Returns the value of the 
 aChar = *ptrChar + 1; 
 memory Pointer points to 
Index Variable[..] Returns the element at the int intArray[3]; 
 index 
 intArray[0] = 0; 
 intArray[1] = 1; 
 intArray[2] = 2; 
 anInt = intArray[1]; 
Structure Structure_ Refers to a member of a struct point pt; 
member name.member particular structure pt.x = 10; 78 
Higher 
precedence 
Lower 
precedence 
 [2], pp. 53 79 
 Put them altogether 
 y 
 Write a program to describe a 
 rectangle by two opposite Vertex v2 
 vertices with the following 
 requirements: x 
  Each vertex has integer coordinates. Vertex v1 
  Input the description of a given 
 rectangle via its two opposite 
 vertices v1.x, v1.y ? 
  Calculate and print the area of a v2.x, v2.y ? 
 given aforementioned rectangle. If 
 there is no area, print -1 instead. ? 
  Calculate and print the length of the 
 diagonal line of a given 
 aforementioned rectangle 
 ? 80 
 y 
 Vertex v2 
 x 
Vertex v1 
 v1.x, v1.y ? 
 v2.x, v2.y ? 
 ? 
 ? 
 81 
 Summary 
 How to handle data in a C program 
  Data types 
 Built-in: char, int, float, double, , void 
 Derived: array, pointer, structure, union, enum 
 enum and structure for abstract data 
 More advanced types (array, pointer) come later. 
  Variables: declaration vs. definition 
 Naming 
 Storage 
 Type 
 Value 
 Scope 82 
 Summary 
 Constants 
  No change during the execution of the program 
  Known at the compile time 
  Defined with: #define, enum, const 
 Expressions: value, type 
 Operators 
  Assignment 
  Arithmetic 
  Bitwise 
  Logic 
  Relational and others 
 Type casting: explicit vs. implicit conversion 83 
Summary 
 Data 
  “information, especially facts or numbers, 
 collected for examination and consideration and 
 used to help decision-making, or information in 
 an electronic form that can be stored and 
 processed by a computer” – Cambridge Dictionary 
 How to handle data in a C program 
  Under control! 
 84 
Chapter 3: Variables and 
Basic Data Types 
 85 

File đính kèm:

  • pdfbai_giang_introduction_to_computer_programming_c_language_ch.pdf