Which of the following are valid variable names in C? Which are not? Which variable names conform to the Style Guide used in this subject?
THX1138
2for1
mrBean
My Space
still_counting
^oo^
_MEMLIMIT
C is a typed language. What does this mean? Why is this useful?
What is the most likely cause of the following compile time error and how would you fix it?
example.c: In function 'main': example.c:4:5: error: 'num' undeclared (first use in this function) num = 3; ^ example.c:4:5: note: each undeclared identifier is reported only once for each function it appears in
3 + 5 * 10 - 12
3 + 15 % 10 - 12 / 2
Note: The precedence rules of arithmetic operators in C follow the conventions used in mathematics, as detailed in Chapter 3.1 of Moffat and in C_Basics lecture notes.
Determine the type and value of each expression and sub-expression:
1 / 2 * 500
1 / 2.0 * 500
(17 / 5) * 5 + (17 % 5)
(12 - 17) % 6 - 4
10/(1/2)
int num1 = 3; int num2 = 8; printf("The numbers are %d\n",num1,num2);
int num1 = 3; int num2 = 8; printf("The numbers are %d %d\n",num1);
double num1 = 3; int num2 = 8; printf("The numbers are %d %d\n",num1,num2);
int num; printf("Enter a number: "); scanf("%d",&num); printf("Your number was %d\n",num);
nextBirthday.c
which reads someones age and prints out the age they will turn on their next birthday. Assume they are entering their age as integers.
For example:
./nextBirthday Please enter your age: 2 On your next birthday you will turn 3
cm2feet.c
which reads a height in centimetres
and prints it in feet.
Reminder: there are 2.54 centimetres in an inch and 12 inches in a foot.
Use only int variables.
Your program should behave like this:
./cm2feet Enter your height in centimetres: 183 Your height in feet is 6
Would double variables have been a better choice?
Hint: Use the math.h library function
sqrt
. How would you compile this if you were using gcc?
Consider the following expressions:
(x == 4) && (y == 4)
(x == 4) || (y == 4)
!((x == 4) || (y == 4))
(!(x == 4)) && (!(y == 4))
What do each of the expressions evaluate to given the following values of x and y
Compare the evaluation of the last 2 expressions on the given inputs. What do you notice?
if
statements in programs?
odd_even.c
that reads in an integer and prints out whether it is odd or even. For example:
./odd_even Please enter an integer: 42 EVEN ./odd_even Please enter an integer: 1 ODD ./odd_even Please enter an integer: -3 ODD ./odd_even Please enter an integer: 0 EVEN
classifyNumber.c
that reads in an integer and prints out whether it is odd or even and also whether it is positive, negative or zero. For example:
./classifyNumber Please enter an integer: 42 EVEN, POSITIVE ./classifyNumber Please enter an integer: 1 ODD, POSITIVE ./classifyNumber Please enter an integer: -3 ODD, NEGATIVE ./classifyNumber Please enter an integer: 0 EVEN, ZERO ./classifyNumber Please enter an integer: -2 EVEN, NEGATIVE
pass_fail.c
that reads in an integer and prints out "PASS" if the integer is between 50 and 100 (exclusive) and fail if it is between 49 and 0 (exclusive). It should print out ERROR if the number is less than 0, more than 100, or if the user does not enter a number. For example:
./pass_fail Please enter your mark: 42 FAIL ./pass_fail Please enter your mark: 50 PASS ./pass_fail Please enter your mark: 256 ERROR
is_triangle.c
to read 3 integers and indicate whether they can be the sides of a triangle.
Reminder: 3 numbers can be the sides of a triangle if the sum of any two of the numbers is larger than the third.
At a theme park there are rides that anyone who is 120cm or over can go on ("Green Rides"), rides that anyone who is 130cm or over can go on ("Yellow Rides") and rides that anyone who is 140cm or over can go on ("Red Rides"). Write an application that asks the user to enter their height to the nearest cm and displays all the types of rides they can go on.For example:
./rides Please enter your height: 130 Green Rides Yellow Rides ./rides Please enter your height: 119 Sorry, no rides are safe for you! ./rides Please enter your height: 256 Green Rides Yellow Rides Red Rides
Discuss the concept of short-circuit evaluation for the C logical operators ||
and &&
.
Give examples. Why is this feature useful?