CSI 121 Structured Programming Language Lecture 6 Components



























- Slides: 27
CSI 121 Structured Programming Language Lecture 6: Components of a C Program (Part 3) 1
Topics • • • Type Variables Keywords and Identifiers Assignments Constant Variables 2
Type • The kind of a value is its “type” • Not all values are of the same kind – For example: 7 + “cat” makes no sense Value Variable 3
Type • Built-in types: char, int, float • Type modifiers: long, short, const • User-defined types (arrays and records) • What about “strings”? – Strings are arrays of char (discussed later) 4
Character Representation • Characters are stored as a small integer • Each character has a unique integer equivalent specified by its position in the ASCII table (pronounced “as-key”) – American Standard Code for Information Interchange – see Appendix D of D&D 5
6
Character Representation • The ASCII values range from 0 to 127 – value 0: special character ’ ’ (a. k. a. NUL character) – value 127: special character <DEL> – other special characters: ’n’ ’t’ ’’’ ’\’ etc. – various “extended” sets from 128 to 255 7
Variable • Is a logical name for a container – (an actual piece of computer memory for values) • Has a type associated with it – tells the computer how to interpret the bits • Must be declared before use: int i; float result; int i=0; char initial=’K’; 8
Variable Declaration: Examples int my. ID; my. ID 9
Variable Declaration: Examples int my. ID; char my. Initial = ’J’; Single “forward quotes” or apostrophe (’) rather than “back quotes” (‘) 10
Variable Declaration: Examples int my. ID; char my. Initial = ’J’; 01001010 my. Initial 11
Variable Declaration: Examples int my. ID; char my. Initial = ’J’; char my. Initial = 74 ; 01001010 my. Initial 12
Variable Declaration: Examples float commission = 0. 05; short int my. Height = 183; /* cm */ long int my. Salary = 10000000000; long float chance. Of. ADate = 3 e-500; double chance. Of. A 2 nd. Date = 1. 5 e-500; 13
Variable Declaration: Examples float commission = 0. 05; “Keywords” short int my. Height = 183; /* cm */ long int my. Salary = 10000000000; long float chance. Of. ADate = 3 e-500; double chance_of_a_2 nd_date = 1. 5 e-500; 14
Keyword • • . . . has a special meaning in C. . . is “case-sensitive”. . . cannot be used as variable names Examples: int, char, long, main, float, double, const, while, for, if, else, return, break, case, switch, default, typedef, struct, etc. (see D&D 2/e Appendix A or D&D 3/e page 545, Figure 15. 4) 15
Variable Declaration: Examples float commission = 0. 05; “Identifiers” short int my. Height = 183; /* cm */ long int my. Salary = 10000000000; long float chance. Of. ADate = 3 e-500; double chance. Of. A 2 nd. Date = 1. 5 e-500; 16
Identifier • . . . is a series of characters consisting of letters, digits and underscores ( _) • • . . . cannot begin with a digit. . . must not be a keyword. . . is “case-sensitive” Examples: s. Umo. FA, x 1, y 2, _my_ID_, Main (careful!) 17
Assignment • Puts a specified value into a specified variable • Assignment operator: = <variable name> = <expression> ; not to be confused with == 18
Assignment: Examples float x = 2. 5 ; char int ch ; number ; ch = ’n’ ; number = 4 + 5 ; /* current value of number is 9. */ number = number * 2; /* current value of number is now 18. */ 19
Assignment • Value must have a type assignable to the variable • Value may be automatically converted to fit the new container • Example: – various. c 20
#include <stdio. h> integer = 33. 33; character = 33. 33; floating. Point = 33. 33; /* Do various assignment statements */ int main() { integer; character; floating. Point; integer = floating. Point; floating. Point = integer; return 0; } integer = 33; character = 33; floating. Point = 33; integer = 'A'; character = 'A'; floating. Point = 'A'; 21 various. c
Constant Variables • . . . are variables that don’t vary • . . . may not be assigned to. • . . . must be initialized const float Pi = 3. 14159; const int class. Size = 100; 22
Constant Variables: Examples const int my. ID = 192; my. ID = 666; /* Error! */ const int pass. Mark = 80; short char p. As. Sg. Ra. De = ’P’; const float pi = 3. 1415926; /* oops */ const double golden_ratio = 1. 61803398874989; 23
Example: Constants Converts an angle from degrees to radians output “Enter angle in degrees” input angle. In. Degrees #include <stdio. h> /* Converts an angle in degrees to radians. */ const float PI = 3. 1415926; int main() { float angle. In. Degs; float angle. In. Rads; angle. In. Radians = / 180 * angle. In. Degrees printf("Enter angle in degrees: "); scanf("%f", &angle. In. Degs); angle. In. Rads = PI/180*angle. In. Degs; output angle. In. Radians printf("%fn", angle. In. Rads); return 0; } 24
Example: Constants “Global” constant variable #include <stdio. h> /* Converts an angle in degrees to radians. */ const float PI = 3. 1415926; int main() { float angle. In. Degs; float angle. In. Rads; printf("Enter angle in degrees: "); scanf("%f", &angle. In. Degs); “Local” variables angle. In. Rads = PI/180*angle. In. Degs; printf("%fn", angle. In. Rads); return 0; } more on this later. . . 25
Topics ü Type ü Variables ü Keword and Identifiers ü Assignments ü Constant Variables 26
Reading • King – Chapter 2, 2. 4 – 2. 8 – Chapter 4, 4. 2 – 4. 5 – Chpater 7, 7. 1 – 7. 3 • D&D: – Chapter 9, 9. 1 – 9. 5, 9. 11 • Kernighan & Ritchie – 2. 4 – 2. 8, 2. 10, 2. 12 27