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

pdf 85 trang Gia Huy 17/05/2022 3280
Bạn đang xem 20 trang mẫu của tài liệu "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 bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pdfintroduction_to_computer_programming_c_language_chapter_3_va.pdf

Nội dung text: Introduction to Computer Programming (C language) - Chapter 3: Variables and Basic Data Types - Võ Thị Ngọc Châu

  1. Ho Chi Minh City University of Technology Faculty of Computer Science and Engineering Chapter 3: Variables and Basic Data Types Introduction to Computer Programming (C language) TS. Võ Thị Ngọc Châu (chauvtn@cse.hcmut.edu.vn, chauvtn@hcmut.edu.vn) 2017 – 2018, Semester 2
  2. Course Content C.1. Introduction to Computers and Programming C.2. C Program Structure and its Components C.3. Variables and Basic Data Types C.4. Selection Statements C.5. Repetition Statements C.6. Functions C.7. Arrays C.8. Pointers C.9. File Processing 2
  3. References [1] “C: How to Program”, 7th Ed. – Paul Deitel and Harvey Deitel, Prentice Hall, 2012. [2] “The C Programming Language”, 2nd Ed. – Brian W. Kernighan and Dennis M. Ritchie, Prentice Hall, 1988 and others, especially those on the Internet 3
  4. Content Introduction Data and Data Types enum Data Type struct Data Type Variables and Variable Declaration Constant Definition Expressions Operators Summary 4
  5. Introduction Algorithm findMinNumber Given a - Input: positiveNumber[n] which is an array of n positive double values - Output: minNumber which is the smallest one whose type is double set of n - Purpose: find the smallest number in a collection - Precondition: n data inputs are positive. positive Begin Algorithm Check positiveNumber[n] contains only positive values numbers, minNumber = positiveNumber[1] iteration = 2 find the While (iteration <= n) smallest Begin While If (minNumber <= positiveNumber[iteration]) Then one. iteration = iteration + 1 Else Begin (Chapter 1 – minNumber = positiveNumber[iteration] Pseudo code) iteration = iteration + 1 End End While 5 End Algorithm
  6. Introduction Given a void main() { double positiveNumber[10] = {2, 1, 3, 10, 8, 3, 4, 5, 9, 12}; set of n int n = 10; positive double minNumber = positiveNumber[0]; int iteration = 1; numbers, while (iteration < n) { find the if (minNumber <= positiveNumber[iteration]) iteration = iteration + 1; smallest else { one. minNumber = positiveNumber[iteration]; iteration = iteration + 1; (Chapter 1 – } Real code in C) } } 6
  7. Introduction Given a void main() { double positiveNumber[10] = {2, 1, 3, 10, 8, 3, 4, 5, 9, 12}; set of n int n = 10; positive double minNumber = positiveNumber[0]; int iteration = 1; numbers, /* Variable declarations */ while (iteration < n) { find the if (minNumber <= positiveNumber[iteration]) iteration = iteration + 1; smallest else { one. minNumber = positiveNumber[iteration]; iteration = iteration + 1; (Chapter 1 – } Real code in C) } } 7
  8. Introduction Given a void main() { double positiveNumber[10] = {2, 1, 3, 10, 8, 3, 4, 5, 9, 12}; set of n INPUT int n = 10; positive double minNumber = positiveNumberOUTPUT [0]; int iteration = 1; numbers, SUPPORTING ONES /* Variable declarations */ while (iteration < n) { find the if (minNumber <= positiveNumber[iteration]) iteration = iteration + 1; smallest else { one. minNumber = positiveNumber[iteration]; iteration = iteration + 1; (Chapter 1 – } Real code in C) } } 8
  9. Introduction Handle data in a C program  Input  Output What?  Supporting ones Declare, Store, Process (Manipulate)? Who declares? Who stores? Where? Who processes (manipulates)? 9
  10. Introduction Handle data in a C program  Input  Output What? Values + Types (Our real world)  Supporting ones Declare, Store, Process (Manipulate)? Who declares? - We Where? Computer Memory Who stores? - (Computer’s world) Who processes (manipulates)? - Computer 10
  11. Data and Data Types 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  Examples: 9.5 “Introduction to Computer Programming” „A‟ (“59800172”, “Chau”, “IT”, 1998) (2, 5, 10, 3, 8, 9, 4, 1, 7, 6) 1 1, 2, 3, 11
  12. Data and Data Types 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  Examples: 9.5 Value “Introduction to Computer Programming” Meaning „A‟ (“59800172”, “Chau”, “IT”, 1998) Type (2, 5, 10, 3, 8, 9, 4, 1, 7, 6) Purpose 1 1, 2, 3, Role 12
  13. Data and Data Types 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  Examples: 9.5 Grade Value “Introduction to Computer Programming” Name Meaning „A‟ Grade (“59800172”, “Chau”, “IT”, 1998) Student‟s info Type (2, 5, 10, 3, 8, 9, 4, 1, 7, 6) Stock levels Purpose 1 The lowest stock level Iterations for problem solving 1, 2, 3, Role 13 and so on
  14. Data and Data Types 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 Types (atomic,Examples: non-atomic): - Real numbers 9.5 Grade Value - Sequences of characters “Introduction to Computer Programming” Name - Characters Meaning - Records „A‟ Grade - Sets of elements (“59800172”, “Chau”, “IT”, 1998) Student‟s info Type - Integer numbers (2, 5, 10, 3, 8, 9, 4, 1, 7, 6) Stock levels - Natural numbers The lowest stock level Purpose - 1 Iterations for problem solving Value sets1, +2, data 3, manipulations Role Storage 14 and so on
  15. Data and Data Types 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 Data Types  Represent data processed in a computer-based program  Specify what kind of data is to be stored in memory How much memory should be allocated How to interpret the bits stored in the memory  Specify what operations are allowed for data manipulation 15
  16. Data Types in C Built-in data types (primitive/fundamental)  char (signed char), unsigned char  short int, unsigned short, int, unsigned int, long int, unsigned long int, long long int, unsigned long long  float, double, long double  void  enum (enumerated data associated with integers) Derived data types  arrays [] of objects of a given type  pointers * to objects of a given type  structures struct containing objects of other types  union containing any one of several objects of various types 16
  17. Built-in Data Types in C Type Keyword Size Range Characters char 1 byte -128 to 127 Unsigned characters unsigned char 1 byte 0 to 255 Short integer numbers short 2 bytes -32768 to 32767 Unsigned short integer numbers unsigned short 2 bytes 0 to 65535 Integer numbers int 4 bytes -2,147,483,648 to 2,147,483,647 (2 bytes: -32768 to 32767) Unsigned integer numbers unsigned int 4 bytes 0 to 4,294,967,295 Long integer numbers long 4 bytes -2,147,483,648 to 2,147,483,647 Unsigned long integer numbers unsigned long 4 bytes 0 to 4,294,967,295 Single-precision floating-point float 4 bytes -3.4e+38 to 3.4e+38 numbers (6 digits of precision) Double-precision floating-point double 8 bytes -1.797693e+308 to numbers (15 digits of precision) 1.797693e+308 ? long long 8 bytes ? ? unsigned long 8 bytes ? Size varies from system long to system! ? long double 12 bytes ? sizeof (type_name) Typeless void 1 byte N/A 17 Enumerated data enum 4 bytes ? should be used.
  18. Built-in Data Types in C Fig. 5.5. Promotion hierarchy for data types ([1], p. 150) 18
  19. Built-in Data Types in C Representation – Manipulation  char, unsigned/signed  short,  int, unsigned/signed  long  float  double  long double  char < short < int < long < float < double < long double 19
  20. Built-in Data Types in C char Type name: char Memory allocation: sizeof(char) = 1 byte  The smallest addressable unit in memory  Enough to store a single ASCII character Signed range: -128 127 „A‟ + 1 0 0 0 0 0 1  1 bit for sign (0 = +; 1 = -) /- Unsigned range: 0 255 „a‟ 0 1 1 0 0 0 0 1  8 bits for value 20 ASCII = American Standard Code for Information Interchange
  21. Built-in Data Types in C char Values can be used as numbers instead of characters.  Addition, Subtraction, Comparison 21
  22. Built-in Data Types in C int Type name: int Memory allocation: sizeof(int) = 4 bytes  Depend on the implementation: 2 bytes Range: -231 (231-1) = -2,147,483,648 2,147,483,648  1 bit for sign (0 = +; 1 = -)  (4x8 – 1) = 31 bits for value Non-negative values: standard bit representation 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 32 Negative values: 2‟s complement = 2 – x -2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 220
  23. Built-in Data Types in C int Values can be used with the operators:  Arithmetic (+, -, *, /, )  Relational (comparison)  23
  24. Built-in Data Types in C float Type name: float Memory allocation: sizeof(float) = 4 bytes Range: -3.4e+38 to 3.4e+38  s bit (1 bit) for sign (0 = +; 1 = -)  m bits for exponent Floating-point number = (-1)s f 2m  f bits for fraction * * Real numbers are approximately represented. Values can be used with the operators:  Arithmetic (+, -, *, /, ), Relational (comparison),  Be careful with equality comparison! 24
  25. Built-in Data Types in C float
  26. Built-in Data Types in C double Type name: double Memory allocation: sizeof(double) = 8 bytes Range: -1.7e+308 to 1.7e+308  s bit (1 bit) for sign (0 = +; 1 = -)  m bits for exponent  f bits for fraction Real numbers are approximately represented. Values can be used with the operators:  Arithmetic (+, -, *, /, ), Relational (comparison),  Be careful with equality comparison! 26
  27. Built-in Data Types in C double
  28. enum Data Type Enumerations, introduced by the keyword enum, are unique types with values ranging over a set of named constants called enumerators. The identifiers in an enumerator list are declared as constants of type int, and may appear wherever constants are required. Enumerator names in the same scope must all be distinct from each other and from ordinary variable names, but the values need not be distinct. 28
  29. enum Data Type Type definition: enum bool {NO, YES}; enum {NO, YES}; enum {NO = „N‟, YES = „Y‟} enum bool {FALSE = 0, TRUE = 1} Variable definition: enum bool isHappy = YES; int isTrue = TRUE; [2], pp. 215 29
  30. enum Data Type If no enumerations with = appear, then the values of the corresponding constants begin at 0 and increase by 1 as the declaration is read from left to right. An enumerator with = gives the associated identifier the value specified; subsequent identifiers continue the progression from the assigned value. 30
  31. enum Data Type 31
  32. enum Data Type 32
  33. struct Data Type A structure is a collection of one or more variables grouped together under a single name for convenient handling. The keyword struct introduces a structure declaration, which is a list of declarations enclosed in braces, to define a derived type. An optional name called a structure tag may follow the word struct. The variables named in a structure are called members. Structures can be nested. 33
  34. struct Data Type A structure declaration that is not followed by a list of variables reserves no storage; merely describes a template or shape of a structure. Size of a struct data type is a total sum of all the sizes of the types of its members. An automatic structure may also be initialized by assignment or by calling a function that returns a structure of the right type. A member of a particular structure is referred to in an expression by a member operator: structure-name.member 34
  35. struct Data Type struct { struct point { struct student { int x; int x; char IdStudent[10]; int y; int y; char Name[50]; }; }; int IdMajor; int EntranceYear; struct point Location; }; Structure declaration struct { struct point { struct student aStudent; int x; int x; int y; int y; } aPoint1, aPoint2; } aPoint4, aPoint5; struct { struct point aPoint6 = {0, 0}; int x; int y; } aPoint3 = {0, 0}; Variable declaration aStudent.EntranceYear Member aPoint3.x aPoint6.y 35 aStudent.Location.x access
  36. struct Data Type tag struct { struct point { struct student { int x; int x; char IdStudent[10]; int y; int y; char Name[50]; }; }; int IdMajor; int EntranceYear; member struct point Location; }; Structure declaration struct { struct point { struct student aStudent; int x; int x; int y; int y; } aPoint1, aPoint2; } aPoint4, aPoint5; struct { struct point aPoint6 = {0, 0}; int x; int y; initialization } aPoint3 = {0, 0}; Variable declaration aStudent.EntranceYear Member aPoint3.x aPoint6.y 36 aStudent.Location.x access
  37. struct Data Type 37
  38. New Type Name Definition A mechanism for creating synonyms (or aliases) for previously defined data types typedef old_type_name new_type_name; Useful for the following cases:  Shorten the existing type names (e.g. struct)  Meaningful specific-purpose type names for the existing type names  Self-documenting, more portable No new data type is created with typedef. 38
  39. New Type Definition Unit converter: receive a value in centimeter and print it in meter. 39
  40. Variables and Variable Declaration A variable is a location in memory where a value can be stored for use by a program. All variables must be defined with a name and a data type before they are used in a program. Each variable has a value processed in a program within a scope to be referred. Variable classification  Global variables  Local variables 40
  41. 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
  42. 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
  43. Variable Declaration 000000000022FE36 0 i memory43
  44. 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
  45. 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
  46. 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
  47. 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
  48. Variables and Variable Declaration gChar2 is unable to be accessed in the main function due to its improper declaration place! 48
  49. gChar bChar cChar 49
  50. The most “local” variables will take precedence over the others. How to refer to them? Naming! Which aChar is printed? 50
  51. 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
  52. 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
  53. 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
  54. 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
  55. 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
  56. 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
  57. 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
  58. 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
  59. 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
  60. Operators Arithmetic operators Relational operators Logic operators Bitwise operators Comma operator Assignment operators Type casting operator Other operators 60
  61. Arithmetic Operators +, -, *, / All numeric types. Integer division yields integer results. % Integer types (including enum) only. 61
  62. 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
  63. Relational Operators All numeric types. !!! EQUALITY with == 63
  64. 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
  65. 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
  66. Assignment Operators Assignment operator: = Assignment shorthand operators: 66 RHS = right hand side
  67. 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
  68. 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
  69. 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
  70. 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
  71. 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
  72. 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
  73. 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
  74. 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
  75. 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
  76. 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
  77. Operators and Expressions 77
  78. 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
  79. Higher precedence Lower precedence [2], pp. 53 79
  80. 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
  81. y Vertex v2 x Vertex v1 v1.x, v1.y ? v2.x, v2.y ? ? ? 81
  82. 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
  83. 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
  84. 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
  85. Chapter 3: Variables and Basic Data Types 85