Title: CSE1301 Computer Programming Lecture 6: Components of a C Program (Part 2)
1CSE1301Computer ProgrammingLecture
6Components of a C Program(Part 2)
2Topics
- Type
- Variables
- Keywords and Identifiers
- Assignments
- Constant Variables
3Type
- The kind of a value is its type
- Not all values are of the same kind
- For example cant logically evaluate 7 cat
4Type
- 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)
5Character Representation
- Characters are stored as a small integer
- Each character has a unique integer equivalent
specified by its position in the ASCII table
(pronounced ask-key) - American Standard Code for Information
Interchange - see Appendix D of DD
6(No Transcript)
7Character Representation
- The ASCII values range from 0 to 127
- value 0 special character \0(a.k.a. NUL
character) - value 127 special character ltDELgt
- other special characters \n \t \ \\
etc. - various extended sets from 128 to 255
8Variable
- Is a logical name for a container (an actual
piece of computer memory for values) - Have a type associated with them, which tells the
computer how to interpret the bits - Must be declared before use
modifiers lttype namegt ltvariable namegt
modifiers lttype namegt ltvariable namegt ltinit
valuegt
9Variable Declaration Examples
10Variable Declaration Examples
- int myID
- char my_initial J
Single forward quotes or apostrophe () rather
than back quotes ()
11Variable Declaration Examples
- int myID
- char my_initial J
12Variable Declaration Examples
- int myID
- char my_initial J
- char my_initial 74
13Variable Declaration Examples
- float commission 0.05
- short int myHeight 165 / cm /
- long int mySalary 100000000000000000000
- long float chanceOfADate 3e-500
- double chance_of_a_2nd_date 1.5e-500
14Variable Declaration Examples
- float commission 0.05
- short int myHeight 165 / cm /
- long int mySalary 100000000000000000000
- long float chanceOfADate 3e-500
- double chance_of_a_2nd_date 1.5e-500
Keywords
15Keyword
- ...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 DD
2/e Appendix A or DD 3/e page 545, Figure 15.4)
16Variable Declaration Examples
- float commission 0.05
- short int myHeight 165 / cm /
- long int mySalary 100000000000000000000
- long float chanceOfADate 3e-500
- double chance_of_a_2nd_date 1.5e-500
Identifiers
17Identifier
- ...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!)
18Assignment
- Puts a specified value into a specified variable
- Assignment operator
ltvariable namegt ltexpressiongt
19Assignment Examples
float x 2.5 char ch int number ch
\n number 4 5 / current value of
number is 9. / number number 2 / current
value of number is now 18. /
20Assignment
- Value must have a type assignable to the variable
- Value may be automatically converted to fit the
new container - Example
- various.c
21- include ltstdio.hgt
- / 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
22Constant Variables
- ...are variables that dont vary
- ...may not be assigned to.
- ...must be initialized
const modifiers lttypegt ltconstant namegt ltinit
valuegt
23Constant Variables Examples
- const int myID 192
- myID 666 / Error! /
const int passMark 80 short char pAsSgRaDe
P const float e 3.1415926 / oops
/ const double golden_ratio 1.61803398874989
24Example Constants
include ltstdio.hgt / Converts an angle in
degrees to radians. / const float PI
3.1415926 int main() float
angleInDegrees float angleInRadians
printf("Enter angle in degrees ")
scanf("f", angleInDegrees) angleInRadians
PI / 180 angleInDegrees
printf("f\n", angleInRadians) return 0
- Converts an angle from degrees to radians
- output Enter angle in degrees
- input angleInDegrees
- angleInRadians ? / 180 angleInDegrees
- output angleInRadians
25Example Constants
include ltstdio.hgt / Converts an angle in
degrees to radians. / const float PI
3.1415926 int main() float
angleInDegrees float angleInRadians
printf("Enter angle in degrees ")
scanf("f", angleInDegrees) angleInRadians
PI / 180 angleInDegrees
printf("f\n", angleInRadians) return 0
Global (constant) variable
Local variables
more on this later...
26Readings
- This Lecture
- DD 2/e Sections 2.1 to 2.10
- DD 3/e Sections 2.1 to 2.5
- Next Lecture Booleans
- DD 2/e
- Sections 2.12 to 2.13, 3.2 and 4.13 to 4.15
- DD 3/e Sections 2.6, 3.5, 4.10 and 4.11