Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Computer Programming
Lecture 6:
Components of a C Program
(Part 2)
1
Topics
Type
Variables
Keywords and Identifiers
Assignments
Constant Variables
Type
The kind of a value is its type
Not all values are of the same kind
For example: cant logically evaluate 7 + cat
Value
Variable
3
Type
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 ask-key)
American Standard Code for Information Interchange
see Appendix D of D&D
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
Variable
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] <type name> <variable name> ;
[modifiers] <type name> <variable name> = <init value> ;
myID
9
01001010
my_initial
11
01001010
my_initial
12
Keywords
Keyword
15
Identifiers
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!)
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;
floatingPoint = 33.33;
/* 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 = floatingPoint;
floatingPoint = integer;
return 0;
}
21
various.c
Constant Variables
...are variables that dont vary
...may not be assigned to.
...must be initialized
const [modifiers] <type> <constant name> = <init value>;
22
/* Error! */
/* oops */
Example: Constants
Converts an angle from
degrees to radians
#include <stdio.h>
/* Converts an angle in degrees to
radians. */
const float PI = 3.1415926;
int main()
{
float angleInDegrees;
float angleInRadians;
angleInRadians = / 180 *
angleInDegrees
angleInRadians = PI / 180 *
angleInDegrees;
output angleInRadians
printf("%f\n", angleInRadians);
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 angleInDegrees;
float angleInRadians;
Local variables
25
Readings
This Lecture:
D&D 2/e: Sections 2.1 to 2.10
D&D 3/e: Sections 2.1 to 2.5
Next Lecture: Booleans
D&D 2/e:
Sections 2.12 to 2.13, 3.2 and 4.13 to 4.15