Transcript Lecture 5

CS1061 C Programming
Lecture 5: Building Blocks of Simple Programs
A. O’Riordan, 2004
Arithmetic

Arithmetic calculations


Use + (addition) - (subtraction) * (multiplication) and / (division)
Integer division truncates remainder


Modulus operator(%) returns the remainder


7 / 5 evaluates to 1
7 % 5 evaluates to 2
Operator precedence


Some arithmetic operators act before others (i.e., multiplication before
addition) - use parenthesis when needed.
Example: Find the average of three variables a, b and c


Do not use: a + b + c / 3
Use: (a + b + c ) / 3
The if statement
The Selection structure is used to choose among alternative courses of action
Example: If student’s grade is greater than or equal to 60 then Print “Passed”
Algorithm: If condition true Print statement executed and program goes on to next
statement. If false, print statement is ignored and the program goes onto the
next statement.
Code:
if (grade >= 60)
printf("Passed\n");
The else clause
The if selection statement may have an “else” part.
Example: If student’s grade is greater than or equal to 60 then Print “Passed” else
Print “Failed.”
Here is the C code. Note use of relational operator (>=):
if (grade >= 60)
printf("Passed\n");
else
printf("Failed\n");
Statement could have been written (using ?: notation):
grade >= 60 ? printf(“Passed\n”): printf(“Failed\n”);
The else clause (continued)
You can have a set of statements within a pair of braces, example:
if (grade >= 60)
printf("Passed.\n");
else{
printf("Failed.\n");
printf("You must take this course
again.\n");
}
The second printf is part of the else clause. Note also the use of the
relational operator >= (less than or equal to)
Booleans
The if statement above contains a Boolean expression. This is an
expression with two values that eveluates to either true or false.
if (x < 4) printf(“x is less than 4”);
You can also declare Boolean variables and give them an initial value.
bool myBoolean = false;
if (myBoolean)
printf(“This statement is never displayed”);
Booleans named after George Boole, First Professor of Mathematics, U.C.C.
Relational Operators
Standard algebraic
equality operator or
relational operator
C equality or
relational
operator
Example of C Meaning of C
condition
condition
=
==
x == y
x is equal to y
not =
!=
x != y
x is not equal to y
>
>
x > y
x is greater than y
<
>=
<
>=
x < y
x >= y
<=
<=
x <= y
x is less than y
x is greater than or
equal to y
x is less than or
equal to y
Equality Operators
Relational Operators
C Characters



Enclosing a character in single quotes, e.g. ‘H’, represents a printable character
constant. Internally, characters are represented numerically, usually by their
ASCII codes. For example, the ASCII code for ‘A’ is 65.
Example: Here we declare a character variable answer with the initial value ‘y’
(for yes) and change it in the couse of the program to ‘n’ (for no).
char answer = ‘y’; /* set as yes */
...
answer = ‘n’; /* changed to no */
Non-printable characters are represented using the escape sequence backslash
(\). The same convention is used to represent the characters \ and ‘ which
would otherwise cause confusion, i.e. \\ and \’.
Special Characters
C has special characters that need to be “escaped”:
new-line
horizontal tabulation
vertical tabulation
backspace
carriage return
bell
backslash
question mark
single quote
double quote
\n
\t
\v
\b
\r
\a
\\
\?
\’
\”
Here is an example within a printf statement:
printf(“\tThis is \”layed\” out nicely\n”);
Reading in data
Before we can write useful programs we need to be able to read in data from the
keyboard.
A scanf() performs a similar function to printf except its for (formatted) input.
Note the use of the address-of operator (&) in the argument list.
Here’s an example where we read in an integer and a character into variables (and
then display them).
int anInt;
char aChar;
printf(“Enter one character and one integer:”);
scanf(“%c %d”, &aChar, &anInt);
printf(“The value of the integer is %d and the value of
the character is %c\n”, anInt, aChar);
Reading in data (continued)
The scanf function uses standard input (usually keyboard).
Here is a simple example:
int int_var;
scanf(“%d”,%int_var);
This scanf statement has two arguments

%d - indicates data should be a decimal integer

&int_var - location in memory to store variable
The user responds to this scanf statement by typing in a number, and then pressing
the enter (return) key.