Download presentation
Presentation is loading. Please wait.
Published byArthur Hancock Modified over 6 years ago
1
DCP2073 Asas Pengaturcaraan C Lecture 6: Components of a C Program (Part 3)
Lecture 6: C Primitives 6
2
Topics Type Variables Keywords and Identifiers Assignments
Constant Variables Lecture 6: C Primitives 6
3
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
4
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)
5
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
7
Character Representation
The ASCII values range from 0 to 127 value 0: special character ’\0’ (a.k.a. NUL character) value 127: special character <DEL> other special characters: ’\n’ ’\t’ ’\’’ ’\\’ etc. various “extended” sets from 128 to 255
8
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’;
9
Variable Declaration: Examples
int myID; myID
10
Variable Declaration: Examples
int myID; char myInitial = ’J’; Single “forward quotes” or apostrophe (’) rather than “back quotes” (‘)
11
Variable Declaration: Examples
int myID; char myInitial = ’J’; myInitial
12
Variable Declaration: Examples
int myID; char myInitial = ’J’; char myInitial = 74 ; myInitial
13
Variable Declaration: Examples
float commission = 0.05; short int myHeight = 183; /* cm */ long int mySalary = ; long float chanceOfADate = 3e-500; double chanceOfA2ndDate = 1.5e-500;
14
Variable Declaration: Examples
float commission = 0.05; short int myHeight = 183; /* cm */ long int mySalary = ; long float chanceOfADate = 3e-500; double chance_of_a_2nd_date = 1.5e-500; “Keywords”
15
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)
16
Variable Declaration: Examples
float commission = 0.05; short int myHeight = 183; /* cm */ long int mySalary = ; long float chanceOfADate = 3e-500; double chanceOfA2ndDate = 1.5e-500; “Identifiers”
17
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: sUmoFA, x1, y2, _my_ID_, Main (careful!)
18
not to be confused with ==
Assignment Puts a specified value into a specified variable Assignment operator: = <variable name> = <expression> ; not to be confused with ==
19
Assignment: Examples float x = 2.5 ; char ch ; int number ;
ch = ’\n’ ; number = ; /* current value of number is 9. */ number = number * 2; /* current value of number is now 18. */
20
Assignment Value must have a type assignable to the variable
Value may be automatically converted to fit the new container Example: various.c
21
various.c #include <stdio.h>
/* Do various assignment statements */ int main() { int integer; char character; float floatingPoint; integer = 33; character = 33; floatingPoint = 33; integer = 'A'; character = 'A'; floatingPoint = 'A'; integer = 33.33; character = 33.33; floatingPoint = 33.33; integer = floatingPoint; floatingPoint = integer; return 0; } various.c
22
Constant Variables ...are variables that don’t vary
...may not be assigned to. ...must be initialized const float Pi = ; const int classSize = 100;
23
Constant Variables: Examples
const int myID = 192; myID = 666; /* Error! */ const int passMark = 80; short char pAsSgRaDe = ’P’; const float pi = ; /* oops */ const double golden_ratio = ;
24
Example: Constants #include <stdio.h>
/* Converts an angle in degrees to radians. */ const float PI = ; int main() { float angleInDegs; float angleInRads; printf("Enter angle in degrees:"); scanf("%f", &angleInDegs); angleInRads = PI/180*angleInDegs; printf("%f\n", angleInRads); return 0; } Converts an angle from degrees to radians output “Enter angle in degrees” input angleInDegrees angleInRadians = / 180 * angleInDegrees output angleInRadians
25
“Global” constant variable
Example: Constants #include <stdio.h> /* Converts an angle in degrees to radians. */ const float PI = ; int main() { float angleInDegs; float angleInRads; printf("Enter angle in degrees: "); scanf("%f", &angleInDegs); angleInRads = PI/180*angleInDegs; printf("%f\n", angleInRads); return 0; } “Global” constant variable “Local” variables more on this later...
26
Topics Type Variables Keword and Identifiers Assignments
Constant Variables
27
Reading King D&D: Kernighan & Ritchie Chapter 2, 2.4 – 2.8
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.