0% found this document useful (0 votes)
32 views66 pages

Week2 2

This document outlines a lecture on conditional flow control in C, beginning with a recap of using if statements to print different outputs based on inputs, and then introducing the switch statement as another method to achieve conditional branching. It presents a problem of printing student cohort details based on a code and discusses designing functions to split the problem into independent parts like printing the year or program.

Uploaded by

Rodrigo Silva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views66 pages

Week2 2

This document outlines a lecture on conditional flow control in C, beginning with a recap of using if statements to print different outputs based on inputs, and then introducing the switch statement as another method to achieve conditional branching. It presents a problem of printing student cohort details based on a code and discusses designing functions to split the problem into independent parts like printing the year or program.

Uploaded by

Rodrigo Silva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

COMS11500: Introduction to C

Julian Gough

October 7, 2013
Recap of this morning

Outline

1 Recap of this morning


The if statement
Problem: Cohort printing

2 Conditional Flow Control, Revisited


The switch statement
Applied to our Problem

3 Types in C
Integral Types
Character Type
Floating Point Types
Advanced Topics

4 Recapitulation
Recap of this morning The if statement

Problem of the Week


Required skills

1 Conditional Control Flow


How to let the program do (print) different things depending on the
input?
2 Types
How to input and process letters (or characters)?
3 Software Engineering
How to turn a problem into a program?
Recap of this morning The if statement

Flow Diagram for if


if (test) { statements }

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

Flow Diagram for if


if (test) { statements }

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

Flow Diagram for if with else


if (test) { statements } else { alt-statements }

Start

true
test

false
alt-
statements
statements

End
Recap of this morning Problem: Cohort printing

Problem of the Week


Expanding cohort codes

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

Task we will try to solve during this weeks lectures:


Write a program that asks for the code, and prints the fullhand.
Recap of this morning Problem: Cohort printing

Problem of the Week


Towards a Top-Down Solution

Please enter your cohort code: 1G161


You are a First Year MEng Engineering Mathematics student

Please enter your cohort code: 3H606


You are a Third Year MEng Electrical and Electronic
Engineering student

Please enter your cohort code: 1G400


You are a First Year mystery student

Given a code xUyyy then


x is needed to print First or Third
yyy is needed for MEng Engineering Mathematics or mystery.
Even for the printed statements, these two choices are independent.
Recap of this morning Problem: Cohort printing

Problem of the Week


Towards a Top-Down Solution

Please enter your cohort code: 1G161


You are a First Year MEng Engineering Mathematics student

void printyear(int year);

void printprogramme(int code);

int main(void)
{
int year;
int code;

/* Prompt user for input */


/* TODO */

/* Print the cohort descriptively */


printf("You are a ");
printyear(year);
printf(" Year ");
printprogramme(code);
printf(" student\n");

return 0;
}
Recap of this morning Problem: Cohort printing

Problem of the Week


Designing the printyear Subroutine

Specification
On input print
1 First
2 Second
3 Third
4 Fourth

On other inputs we dont


care.
Recap of this morning Problem: Cohort printing

Problem of the Week


Designing the printyear Subroutine

Nested if statements can do this...

Specification
On input print
1 First
2 Second
3 Third
4 Fourth

On other inputs we dont


care.
Recap of this morning Problem: Cohort printing

Problem of the Week


Designing the printyear Subroutine

void printyear(int year)


{
Specification if (year==1) {
On input print printf("First");
1 First } else if (year==2) {
2 Second printf("Second");
3 Third } else if (year==3) {
4 Fourth printf("Third");
} else if (year==4) {
On other inputs we dont printf("Fourth");
care. }
}
Conditional Flow Control, Revisited

Outline

1 Recap of this morning


The if statement
Problem: Cohort printing

2 Conditional Flow Control, Revisited


The switch statement
Applied to our Problem

3 Types in C
Integral Types
Character Type
Floating Point Types
Advanced Topics

4 Recapitulation
Conditional Flow Control, Revisited The switch statement

Flow Diagram for switch with breaks


switch( expression ){ case constant-1: statements-1 break; . . . }

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

switch and break


switch( expression ){ case constant-1: statements-1 break; . . . }
Allows an easy way to make a choice of many options.
expression is evaluated to some int
if the evaluation equals constant-i, then statements-i are executed
if there is no matching constant-i, then the default option is chosen
a default is not obligatory (fall through otherwise).
Conditional Flow Control, Revisited The switch statement

switch and break


switch( expression ){ case constant-1: statements-1 break; . . . }
Allows an easy way to make a choice of many options.
expression is evaluated to some int
if the evaluation equals constant-i, then statements-i are executed
if there is no matching constant-i, then the default option is chosen
a default is not obligatory (fall through otherwise).

The role of break;


The use of breaks is not mandated.
A switch will actually jump to the matching case and start
executing in straight-line fashion from there on.
The break causes a further jump to the closing } of the switch.
Conditional Flow Control, Revisited The switch statement

switch and break


switch( expression ){ case constant-1: statements-1 break; . . . }
Allows an easy way to make a choice of many options.
expression is evaluated to some int
if the evaluation equals constant-i, then statements-i are executed
if there is no matching constant-i, then the default option is chosen
a default is not obligatory (fall through otherwise).

The role of break;


The use of breaks is not mandated.
A switch will actually jump to the matching case and start
executing in straight-line fashion from there on.
The break causes a further jump to the closing } of the switch.

Forgetting breaks is a great way to create bugs.


Conditional Flow Control, Revisited The switch statement

Flow Diagram for switch without breaks


switch( expression ){ case constant-1: statements-1 . . . }

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

Example: switch with or without breaks


int year = 3;
int year = 3;
printf("You are in your ");
printf("You are in your ");
switch (year) {
case 1: printf("First"); switch (year) {
break; case 1:
case 2: printf("Second"); printf("First");
break; case 2:
case 3: printf("Third"); printf("Second");
break; case 3:
case 4: printf("Fourth"); printf("Third");
break; case 4:
default: /* skip */ printf("Fourth");
break; // Superfluous default: /* skip */
} }

printf(" year"); printf(" year");


Conditional Flow Control, Revisited The switch statement

Example: switch with or without breaks


int year = 3;
int year = 3;
printf("You are in your ");
printf("You are in your ");
switch (year) {
case 1: printf("First"); switch (year) {
break; case 1:
case 2: printf("Second"); printf("First");
break; case 2:
case 3: printf("Third"); printf("Second");
break; case 3:
case 4: printf("Fourth"); printf("Third");
break; case 4:
default: /* skip */ printf("Fourth");
break; // Superfluous default: /* skip */
} }

printf(" year"); printf(" year");

You are in your


Conditional Flow Control, Revisited The switch statement

Example: switch with or without breaks


int year = 3;
int year = 3;
printf("You are in your ");
printf("You are in your ");
switch (year) {
case 1: printf("First"); switch (year) {
break; case 1:
case 2: printf("Second"); printf("First");
break; case 2:
case 3: printf("Third"); printf("Second");
break; case 3:
case 4: printf("Fourth"); printf("Third");
break; case 4:
default: /* skip */ printf("Fourth");
break; // Superfluous default: /* skip */
} }

printf(" year"); printf(" year");

You are in your Third


Conditional Flow Control, Revisited The switch statement

Example: switch with or without breaks


int year = 3;
int year = 3;
printf("You are in your ");
printf("You are in your ");
switch (year) {
case 1: printf("First"); switch (year) {
break; case 1:
case 2: printf("Second"); printf("First");
break; case 2:
case 3: printf("Third"); printf("Second");
break; case 3:
case 4: printf("Fourth"); printf("Third");
break; case 4:
default: /* skip */ printf("Fourth");
break; // Superfluous default: /* skip */
} }

printf(" year"); printf(" year");

You are in your Third


Conditional Flow Control, Revisited The switch statement

Example: switch with or without breaks


int year = 3;
int year = 3;
printf("You are in your ");
printf("You are in your ");
switch (year) {
case 1: printf("First"); switch (year) {
break; case 1:
case 2: printf("Second"); printf("First");
break; case 2:
case 3: printf("Third"); printf("Second");
break; case 3:
case 4: printf("Fourth"); printf("Third");
break; case 4:
default: /* skip */ printf("Fourth");
break; // Superfluous default: /* skip */
} }

printf(" year"); printf(" year");

You are in your Third year


Conditional Flow Control, Revisited The switch statement

Example: switch with or without breaks


int year = 3;
int year = 3;
printf("You are in your ");
printf("You are in your ");
switch (year) {
case 1: printf("First"); switch (year) {
break; case 1:
case 2: printf("Second"); printf("First");
break; case 2:
case 3: printf("Third"); printf("Second");
break; case 3:
case 4: printf("Fourth"); printf("Third");
break; case 4:
default: /* skip */ printf("Fourth");
break; // Superfluous default: /* skip */
} }

printf(" year"); printf(" year");

You are in your Third year You are in your


Conditional Flow Control, Revisited The switch statement

Example: switch with or without breaks


int year = 3;
int year = 3;
printf("You are in your ");
printf("You are in your ");
switch (year) {
case 1: printf("First"); switch (year) {
break; case 1:
case 2: printf("Second"); printf("First");
break; case 2:
case 3: printf("Third"); printf("Second");
break; case 3:
case 4: printf("Fourth"); printf("Third");
break; case 4:
default: /* skip */ printf("Fourth");
break; // Superfluous default: /* skip */
} }

printf(" year"); printf(" year");

You are in your Third year You are in your Third


Conditional Flow Control, Revisited The switch statement

Example: switch with or without breaks


int year = 3;
int year = 3;
printf("You are in your ");
printf("You are in your ");
switch (year) {
case 1: printf("First"); switch (year) {
break; case 1:
case 2: printf("Second"); printf("First");
break; case 2:
case 3: printf("Third"); printf("Second");
break; case 3:
case 4: printf("Fourth"); printf("Third");
break; case 4:
default: /* skip */ printf("Fourth");
break; // Superfluous default: /* skip */
} }

printf(" year"); printf(" year");

You are in your Third year You are in your Third


Conditional Flow Control, Revisited The switch statement

Example: switch with or without breaks


int year = 3;
int year = 3;
printf("You are in your ");
printf("You are in your ");
switch (year) {
case 1: printf("First"); switch (year) {
break; case 1:
case 2: printf("Second"); printf("First");
break; case 2:
case 3: printf("Third"); printf("Second");
break; case 3:
case 4: printf("Fourth"); printf("Third");
break; case 4:
default: /* skip */ printf("Fourth");
break; // Superfluous default: /* skip */
} }

printf(" year"); printf(" year");

You are in your Third year You are in your ThirdFourth


Conditional Flow Control, Revisited The switch statement

Example: switch with or without breaks


int year = 3;
int year = 3;
printf("You are in your ");
printf("You are in your ");
switch (year) {
case 1: printf("First"); switch (year) {
break; case 1:
case 2: printf("Second"); printf("First");
break; case 2:
case 3: printf("Third"); printf("Second");
break; case 3:
case 4: printf("Fourth"); printf("Third");
break; case 4:
default: /* skip */ printf("Fourth");
break; // Superfluous default: /* skip */
} }

printf(" year"); printf(" year");

You are in your Third year You are in your ThirdFourth


Conditional Flow Control, Revisited The switch statement

Example: switch with or without breaks


int year = 3;
int year = 3;
printf("You are in your ");
printf("You are in your ");
switch (year) {
case 1: printf("First"); switch (year) {
break; case 1:
case 2: printf("Second"); printf("First");
break; case 2:
case 3: printf("Third"); printf("Second");
break; case 3:
case 4: printf("Fourth"); printf("Third");
break; case 4:
default: /* skip */ printf("Fourth");
break; // Superfluous default: /* skip */
} }

printf(" year"); printf(" year");

You are in your Third year You are in your ThirdFourth year
Conditional Flow Control, Revisited Applied to our Problem

Problem of the Week


Writing the printprogramme Subroutine

Degree programme codes


161 MEng Engineering Mathematics
600 BEng Electrical and Electronic Engineering
606 MEng Electrical and Electronic Engineering
623 MEng Electronic and Communications Engineering
640 BEng Electronic and Communications Engineering
925 BEng Engineering Mathematics
rest 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

Problem of the Week


Writing the printprogramme Subroutine

void printprogramme(int code)


{
/* Print the degree */
switch (code) {
case 600:
case 640:
case 925:
printf("BEng ");
break;
case 161:
case 606:
case 623:
printf("MEng ");
}
...

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

Problem of the Week


Writing the printprogramme Subroutine

...
/* 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

Problem of the Week


Writing the printprogramme Subroutine

void printprogramme(int code)


{
/* Print the degree */
if ( (code==600) || (code==640) || (code==925) )
{
printf("BEng ");
}
else if ( (code==161) || (code==606) || (code==623) )
{
printf("MEng ");
}
...

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

Problem of the Week


Writing the printprogramme Subroutine

...
/* 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

1 Recap of this morning


The if statement
Problem: Cohort printing

2 Conditional Flow Control, Revisited


The switch statement
Applied to our Problem

3 Types in C
Integral Types
Character Type
Floating Point Types
Advanced Topics

4 Recapitulation
Types in C Integral Types

Integer Types in C

int and its cousins


The basic type in C to represent integers is int,
but this type has a bounded range:
type size smallest value largest value
int 32 bits -2,147,483,648 2,147,483,647
231 231 1
unsigned int 32 bits 0 4,294,967,295
0 232 1
Use the modifiers short, long, or long long for different bitlengths.
Types in C Integral Types

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

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.
A signed char has a value between 128, . . . , 127
An unsigned char has a value between 0, . . . , 255
If you just write char, undefined which one is chosen!
Use single quotation marks for characters, e.g. char letter = a
Types in C Character Type

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

Converting from lower to upper case


Small snippet that checks whether char ch is lower case and if so, converts it to
upper case (does not incorporate accents etc.)

if ((a <= ch) && ( ch <= z) {


ch = ch - a + A;
}

Better to use toupper from <ctype.h> library.


Types in C Character Type

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

Converting from lower to upper case


Small snippet that switches the case of vowels only (both ways)

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

Using printf and scanf


The conversion specifier %c is used for char

char ch;
scanf("%c", &ch);
printf("%c", ch);
Types in C Character Type

Characters in C
Input/output

Using printf and scanf


The conversion specifier %c is used for char

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

Using printf and scanf


The conversion specifier %c is used for char

char ch;
scanf("%c", &ch);
printf("%c", ch);

Using putchar and getchar


To write a single character, just write putchar(ch);
To read a single character, write ch = getchar();
Be very careful when mixing scanf and getchar in a single program!
Types in C Floating Point Types

Real Types in C

For real numbers, consider the (scientific) floating-point representation:

x f 10e

where 1 f < 10 is a fraction and e is an integer. E.g. 1.11500 105 .


C uses a binary equivalent and with bounded f and e.
Types in C Floating Point Types

Real Types in C

For real numbers, consider the (scientific) floating-point representation:

x f 10e

where 1 f < 10 is a fraction and e is an integer. E.g. 1.11500 105 .


C uses a binary equivalent and with bounded f and e.
Usual range of the basic floating-point types
type precision smallest positive largest positive
float 6 digits 1.17549 1038 3.408282 1038
double 15 digits 2.22507 10308 1.79769 10308
C defaults to doubles, for added precision use long double.

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

Using scanf for floats and doubles


By default, using %f reads in a float, possibly in scientific notation.
For doubles this could lead to a loss of precision, or worse.
Use %f for floats.
Use %lf for doubles.
Similar options exist for int vs. long etc.

Look up the details if you need them (not used for the course).
Types in C Advanced Topics

The <math.h> library


#include<math.h>

Basic arithmetic +, -, *, / only gets you that far.


For the fun stuff, use the <math.h> library
#include<math.h>

Some of <math.h> functions


Trigonometric: cos, sin, tan
Powering: exp, pow
Inverse Powering: log, sqrt
Rounding: round, ceil, floor
For details and a full listing, consult a reference (or wiki).
Types in C Advanced Topics

The <math.h> library


#include<math.h>

Basic arithmetic +, -, *, / only gets you that far.


For the fun stuff, use the <math.h> library
#include<math.h>
#include<stdio.h>
#include<math.h>
int main(void){
double delta, exponent, base=2, power=4096;
exponent = log(power)/log(base);
delta = power - pow(base,exponent);
printf("Log_%f(%f) = %f\n", base, power, exponent);
printf("Recomputing gives error %f\n", delta);
return 0;
}
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);

Using printf for floats and doubles


By default, using %f prints 6 decimal places. Disadvantages:
Often we can do with less information

For very small numbers, it just prints 0.000000

For very large numbers, it prints meaningless trailing zeroes


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);

Using printf for floats and doubles


By default, using %f prints 6 decimal places. Disadvantages:
Often we can do with less information
"%8.2f" prints 2 decimals using at least 8 characters
For very small numbers, it just prints 0.000000

For very large numbers, it prints meaningless trailing zeroes


Types in C Advanced Topics

Floating-type I/O
Modifying your output
printf("Log_%.0f(%.0f) = %f\n", base, power, exponent);
printf("Recomputing gives error %f\n", delta);

Using printf for floats and doubles


By default, using %f prints 6 decimal places. Disadvantages:
Often we can do with less information
"%8.2f" prints 2 decimals using at least 8 characters
For very small numbers, it just prints 0.000000

For very large numbers, it prints meaningless trailing zeroes


Types in C Advanced Topics

Floating-type I/O
Modifying your output
printf("Log_%.0f(%.0f) = %f\n", base, power, exponent);
printf("Recomputing gives error %f\n", delta);

Using printf for floats and doubles


By default, using %f prints 6 decimal places. Disadvantages:
Often we can do with less information
"%8.2f" prints 2 decimals using at least 8 characters
For very small numbers, it just prints 0.000000
"%10.2e" uses scientific notation.
For very large numbers, it prints meaningless trailing zeroes
"%g" picks either decimal or scientific.
Types in C Advanced Topics

Floating-type I/O
Modifying your output
printf("Log_%.0f(%.0f) = %g\n", base, power, exponent);
printf("Recomputing gives error %10.2e\n", delta);

Using printf for floats and doubles


By default, using %f prints 6 decimal places. Disadvantages:
Often we can do with less information
"%8.2f" prints 2 decimals using at least 8 characters
For very small numbers, it just prints 0.000000
"%10.2e" uses scientific notation.
For very large numbers, it prints meaningless trailing zeroes
"%g" picks either decimal or scientific.
Types in C Advanced Topics

Floating-type I/O
Modifying your output
printf("Log_%.0f(%.0f) = %g\n", base, power, exponent);
printf("Recomputing gives error %10.2e\n", delta);

Using printf for floats and doubles


By default, using %f prints 6 decimal places. Disadvantages:
Often we can do with less information
"%8.2f" prints 2 decimals using at least 8 characters
For very small numbers, it just prints 0.000000
"%10.2e" uses scientific notation.
For very large numbers, it prints meaningless trailing zeroes
"%g" picks either decimal or scientific.
Conclusion: Three options %e, %f, and %g plus availability of modifiers.

Similar options exist for int etc.


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.
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

int i=1; int i=1;


float x; float x;
double y; double y;
x = i; x = (float) i;
y = x; y = (double) 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

Dangers when casting


Source Target Dangers
float double
double float
int float
float int

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

Dangers when casting


Source Target Dangers
float double none
double float
int float
float int

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

Dangers when casting


Source Target Dangers
float double none
double float loss of precision, overflow
int float
float int

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

Dangers when casting


Source Target Dangers
float double none
double float loss of precision, overflow
int float loss of precision
float int

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

Dangers when casting


Source Target Dangers
float double none
double float loss of precision, overflow
int float loss of precision
float int truncates, wrap around

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

1 Recap of this morning


The if statement
Problem: Cohort printing

2 Conditional Flow Control, Revisited


The switch statement
Applied to our Problem

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

You might also like