Week2 2
Week2 2
Julian Gough
October 7, 2013
Recap of this morning
Outline
3 Types in C
Integral Types
Character Type
Floating Point Types
Advanced Topics
4 Recapitulation
Recap of this morning The if statement
Start
int a=3;
int a=3; int b=4;
int b=4;
if (a<b)
true {
a<b
printf("a is smaller than b");
false
}
printf(...);
// "a is smaller than b" is printed
End
Recap of this morning The if statement
Start
int a=7;
int a=7; int b=4;
int b=4;
if (a<b)
true {
a<b
printf("a is smaller than b");
false
}
printf(...);
// nothing is printed
End
Recap of this morning The if statement
Start
true
test
false
alt-
statements
statements
End
Recap of this morning Problem: Cohort printing
Cohort codes
There are shorthand codes that represent
Your year of study
The degree programme you are following, e.g.
G161 MEng Engineering Mathematics
H600 BEng Electrical and Electronic Engineering
H606 MEng Electrical and Electronic Engineering
H623 MEng Electronic and Communications Engineering
H640 BEng Electronic and Communications Engineering
J925 BEng Engineering Mathematics
int main(void)
{
int year;
int code;
return 0;
}
Recap of this morning Problem: Cohort printing
Specification
On input print
1 First
2 Second
3 Third
4 Fourth
Specification
On input print
1 First
2 Second
3 Third
4 Fourth
Outline
3 Types in C
Integral Types
Character Type
Floating Point Types
Advanced Topics
4 Recapitulation
Conditional Flow Control, Revisited The switch statement
Start
switch ( expression ) {
case constant-1:
statements-1 Switch
break;
case constant-2: 1 2 default
statements-2 statements-1 statements-2 statements-3
break;
default:
statements-3 End
}
Conditional Flow Control, Revisited The switch statement
Start
switch ( expression ) {
case constant-1:
switch
statements-1
case constant-2:
statements-2 1 2 default
default: statements-1 statements-2 statements-3
statements-3
} End
Conditional Flow Control, Revisited The switch statement
You are in your Third year You are in your ThirdFourth year
Conditional Flow Control, Revisited Applied to our Problem
Option 2:
1 For 161, 606, 623 print MEng, for 600, 640, 925 print BEng
2 For 161, 925 print EngMath, etc.
Conditional Flow Control, Revisited Applied to our Problem
Option 2:
1 For 161, 606, 623 print MEng, for 600, 640, 925 print BEng
2 For 161, 925 print EngMath, etc.
Conditional Flow Control, Revisited Applied to our Problem
...
/* Print the type of programme */
switch (code) {
case 161:
case 925:
printf("Engineering Mathematics");
break;
case 600:
case 606:
printf("Electrical and Electronic Engineering");
break;
case 623:
case 640:
printf("Electronic and Communications Engineering");
break;
default:
printf("mystery");
}
}
Option 2:
1 For 161, 606, 623 print MEng, for 600, 640, 925 print BEng
2 For 161, 925 print EngMath, etc.
Conditional Flow Control, Revisited Applied to our Problem
Option 2:
1 For 161, 606, 623 print MEng, for 600, 640, 925 print BEng
2 For 161, 925 print EngMath, etc.
Conditional Flow Control, Revisited Applied to our Problem
...
/* Print the type of programme */
if ( (code==161) || (code==925) )
{
printf("Engineering Mathematics");
}
else if ( (code==600) || (code==606) )
{
printf("Electrical and Electronic Engineering");
}
else if ( (code==623) || (code==640) )
{
printf("Electronic and Communications Engineering");
}
else
{
printf("mystery");
}
}
Option 2:
1 For 161, 606, 623 print MEng, for 600, 640, 925 print BEng
2 For 161, 925 print EngMath, etc.
Types in C
Outline
3 Types in C
Integral Types
Character Type
Floating Point Types
Advanced Topics
4 Recapitulation
Types in C Integral Types
Integer Types in C
Overview of int
Arithmetic
Maths C Remarks
Z int ints have limited range
a+b a + b careful if a + b exceeds ints range
ab a - b careful when using unsigned ints
ab a * b careful if a b exceeds ints range
a div b a / b avoid b = 0
a mod b a % b avoid b = 0
Input/Output
int i;
scanf("%d", &i);
printf("%d"), i);
Types in C Character Type
Characters in C
Characters in C
The type char
C uses the type char to represent characters such as a, b, A, 1, etc.
C really treats a char as an 8-bit integer.
Use single quotation marks for characters, e.g. char letter = a
Characters in C
The type char
C uses the type char to represent characters such as a, b, A, 1, etc.
C really treats a char as an 8-bit integer.
Use single quotation marks for characters, e.g. char letter = a
switch (ch) {
case a: case e: case i: case o: case u:
ch = ch - a + A;
break;
case A: case E: case I: case O: case U:
ch = ch - A + a;
break;
}
Types in C Character Type
Characters in C
Input/output
char ch;
scanf("%c", &ch);
printf("%c", ch);
Types in C Character Type
Characters in C
Input/output
char ch;
scanf("%c", &ch);
printf("%c", ch);
char a;
int b;
scanf("%c%d", &a, &b); // Expected input format: B44
scanf("%c %d", &a, &b); // Expected input format: B 44
Types in C Character Type
Characters in C
Input/output
char ch;
scanf("%c", &ch);
printf("%c", ch);
Real Types in C
x f 10e
Real Types in C
x f 10e
The conversion from decimal to binary can cause a small rounding error.
Types in C Floating Point Types
Overview of double
Arithmetic
Maths C Remarks
R float floats are only an approximation
a+b a + b careful if a and b are of different magnitude
ab a - b careful if a and b are of different magnitude
ab a * b overflow error if a b exceeds floats range
a/b a / b underflow error if a/b exceeds floats range
Basic Input/Output
float x; double x;
scanf("%f", &x); scanf("%lf", &x);
printf("%f", x); printf("%f", x);
Types in C Floating Point Types
Floating-type I/O
Careful with input
Look up the details if you need them (not used for the course).
Types in C Advanced Topics
Floating-type I/O
Modifying your output
printf("Log_%f(%f) = %f\n", base, power, exponent);
printf("Recomputing gives error %f\n", delta);
Floating-type I/O
Modifying your output
printf("Log_%f(%f) = %f\n", base, power, exponent);
printf("Recomputing gives error %f\n", delta);
Floating-type I/O
Modifying your output
printf("Log_%.0f(%.0f) = %f\n", base, power, exponent);
printf("Recomputing gives error %f\n", delta);
Floating-type I/O
Modifying your output
printf("Log_%.0f(%.0f) = %f\n", base, power, exponent);
printf("Recomputing gives error %f\n", delta);
Floating-type I/O
Modifying your output
printf("Log_%.0f(%.0f) = %g\n", base, power, exponent);
printf("Recomputing gives error %10.2e\n", delta);
Floating-type I/O
Modifying your output
printf("Log_%.0f(%.0f) = %g\n", base, power, exponent);
printf("Recomputing gives error %10.2e\n", delta);
Casting variables
Mixing variables of different types
int i=1;
float x;
double y;
x = i;
y = x;
Casting
Changing the type of a value (constant or variable) is called casting.
Implicit casting: Let C sort it out for you.
Types in C Advanced Topics
Casting variables
Mixing variables of different types
int i=1;
float x;
double y;
x = i;
y = x;
Casting
Changing the type of a value (constant or variable) is called casting.
Implicit casting: Let C sort it out for you.
Dangerous habit as casting might happen without you knowning
bugs
Types in C Advanced Topics
Casting variables
Mixing variables of different types
int i=1;
float x;
double y;
x = i;
y = x;
Casting
Changing the type of a value (constant or variable) is called casting.
Implicit casting: Let C sort it out for you.
Dangerous habit as casting might happen without you knowning
bugs
Explicit casting: By writing e.g. (int) or (float)
There is still a danger of loss of precision!
Types in C Advanced Topics
Casting variables
Mixing variables of different types
Casting
Changing the type of a value (constant or variable) is called casting.
Implicit casting: Let C sort it out for you.
Dangerous habit as casting might happen without you knowning
bugs
Explicit casting: By writing e.g. (int) or (float)
There is still a danger of loss of precision!
Types in C Advanced Topics
Casting variables
Mixing variables of different types
Casting
Changing the type of a value (constant or variable) is called casting.
Implicit casting: Let C sort it out for you.
Dangerous habit as casting might happen without you knowning
bugs
Explicit casting: By writing e.g. (int) or (float)
There is still a danger of loss of precision!
Types in C Advanced Topics
Casting variables
Mixing variables of different types
Casting
Changing the type of a value (constant or variable) is called casting.
Implicit casting: Let C sort it out for you.
Dangerous habit as casting might happen without you knowning
bugs
Explicit casting: By writing e.g. (int) or (float)
There is still a danger of loss of precision!
Types in C Advanced Topics
Casting variables
Mixing variables of different types
Casting
Changing the type of a value (constant or variable) is called casting.
Implicit casting: Let C sort it out for you.
Dangerous habit as casting might happen without you knowning
bugs
Explicit casting: By writing e.g. (int) or (float)
There is still a danger of loss of precision!
Types in C Advanced Topics
Casting variables
Mixing variables of different types
Casting
Changing the type of a value (constant or variable) is called casting.
Implicit casting: Let C sort it out for you.
Dangerous habit as casting might happen without you knowning
bugs
Explicit casting: By writing e.g. (int) or (float)
There is still a danger of loss of precision!
Types in C Advanced Topics
Casting variables
Mixing variables of different types
Casting
Changing the type of a value (constant or variable) is called casting.
Implicit casting: Let C sort it out for you.
Dangerous habit as casting might happen without you knowning
bugs
Explicit casting: By writing e.g. (int) or (float)
There is still a danger of loss of precision!
Recapitulation
Outline
3 Types in C
Integral Types
Character Type
Floating Point Types
Advanced Topics
4 Recapitulation
Recapitulation
Terminology
Variable
#include
Identifier
<stdio.h>
Type
main
Declaration
return
Statement
int
Function
float
Prototype
void
Definition
printf
Body
scanf
Parameter
"%d"
Argument
"\n"
Return value