Week 02 Tutorial Questions
- What are the types of variables that we've seen so far and what do they represent?
- What is the difference between a variable and a constant?
- What is the difference between declaring variable and using #define?
-
scanf() and printf() are similar functions.
printf(): print to the terminal
scanf(): scan in from the terminal
Fill in the following table with the correct syntax to print out a variable called a with value 12/ scan in a variable called a.
printf() scanf() int a = 12;
printf();
int a;
scanf();
-
Write a program
rectangle_area.c
that scans in 2 integers which are the side-length of a rectangle, and then prints the area of the rectangle. -
The value of C arithmetic operations depends on the types of the operand. If the types of the operands differ, C automatically converts the types as appropriate.
Determine the value and type of each expression and sub-expression:
- 1 / 2 * 500
- 1 / 2.0 * 500
- (17 / 5) * 5 + (17 % 5)
- (12 - 17) % 6 - 4
- 'A' + 5 (Assume we treat this as a
char
) - 'A' + 32 (Assume we treat this as a
char
)
-
What is the syntax of C
if
statements? What is the role ofif
statements in programs? -
What is the difference between the following if statement structures?
if (num >= 1) { printf("num is 1 or greater\n"); } if (num > 1) { printf("num is greater than 1\n"); }
if (num >= 1) { printf("num is 1 or greater\n"); } else if (num > 1) { printf("num is greater than 1\n"); }
if (num >= 1) { printf("num is 1 or greater\n"); } else { printf("num is greater than 1\n"); }
-
A leap year is a year that has 366 days instead of 365. There is a leap year every year divisible by four except for years which are both divisible by 100 and not divisible by 400.
For more detail information of leap years see: Leap Year.
Draw a simple flow chart showing the branches of possibilities to decide if a year is a Leap Year.
-
Can you then write some simple code using if/else statements in C that decides if a year is a Leap Year? (hint: finding remainders is a useful way to decide whether a number is divisible by another) This code question will be continued in this week's lab, so try to remember or keep a copy/photo of the diagram and code you've written.
-
Is this true or false: (There are 4 penguins) || (They are the same height)? What values does this have in C?
-
Is this true or false: (There are 4 penguins) && (They are the same height)? What values does this have in C?
Variables
Input/Output
C Arithmetic Operators
Program Flow
For some humourous information about flow charts see: Flow Charts (xkcd).
Complex if conditions
The and (&&) and or (||) operators can be used to combine multiple statments into one (such as the statements created in the leap year activity).

(Source: Penguins of Madagascar, 2014)