COP-2220C
Instructor: Brian Williamson
“Hardware: The parts of a computer system that can be kicked.” – Jeff Pesis
About Me
E-mail: BWilliamson9@valenciacollege.edu
int total;
A Program with Variables
#include <stdio.h>
#include <stdlib.h>
void main()
{
double taxRate;
int total = 127;
int countSamples;
taxRate = 0.07;
system("pause");
}
Arithmetic Operators
In C there are arithmetic operators.
These are symbols representing functions that calculate a
value.
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Modulus (%)
Arithmetic Syntax
operand operator operand;
Operands can be variables: a + b;
Constants: 10 + 5;
Other calculations: (10 * 2) + 5;
You don’t HAVE to assign the calculation to anything.
a + b; is valid in the C syntax, but what is the point?
c = a + b; assigns the calculation somewhere.
Modulus Operator
Some may be unfamiliar with modulus (%)
This operator returns the remainder of a division.
10 / 3 = 3 with a remainder of 1.
10 % 3 = 1
4%2=0
3%2=1
There are many uses for this operator that you will see.
Can constrain the range of a variable
Such as with the rand() function.
Can determine if a variable is even or odd.
Variable % 2 will be 0 if even or 1 if odd.
Order of Operations
Typically Arithmetic in C follows the orders we know:
P E M D A S (Please Excuse My Dear Aunt Sally)
Parenthesis, Exponent, Multiplication, Division, Addition,
Subtraction
c = 10 * 2 + 5
What happens first? 10 * 2
Next? + 5
c = 10 * (2 + 5)
What happens first? 2 + 5
Next? * 10
TIP: Don’t assume order of operations. Use parenthesis
to always make your intention known.
Concepts Summary
Variables – named locations that hold a piece of data.
Data type – the type of data that can be put in a variable.
int double
Naming rules and conventions.
Assignment operator = pronounce it “gets” not “equals.”
Section 3 – More Data
Types and Mixed Types
More Data Types
There is far more data types in C than just int and double.
Eventually we will learn how to make our own data types.
The issue is choose the type of variable you need.
Also to be aware of mixing types and the effect.
Choose a variable based on:
Size of data (what is its min/max values)
This is good for efficiency, but not typically a problem in modern
programming.
Type of data (real number or whole number)
Reference Table
Type Range Bytes Required
unsigned char 0 -> 255 1