CPS 125
Alexander Ferworn
Week 3
Integers and Floats
What we will cover this week
More about binary
Operators and Expressions
Operators let you calculate values,
you are probably already familiar with some of these (+,-, etc.)
Operands are things that get acted
on by operators, for example in 2 + 3, the operands are 2 and 3 and the
operator is +.
An expression in C is anything that
can produce a value, for example
In the first case the value is itself (2), in the second it is the value of
whatever a + b is after you do the operation, in the third case the value is
either true or false (we wonÌt worry about that yet).
Integer Operators
There are five basic integer operations
(in the following examples assume num1, num2 and num3 are integer variables.
+ means add two values
e.g.
num1 = 1;
num2 = 2;
num3 = num1 + num2;
num3 will be equal to 3.
- means subtract two values
e.g.
num1 = 1;
num2 = 2;
num3 = num2 - num1;
num3 will be equal to 1;
/ means integer divide two values
e.g.
num1 = 1;
num2 = 2;
num3 = num1 / num2;
num3 will be equal to 1 (not 0.5) since this is integer division
* means multiply two values
e.g.
num1 = 1;
num2 = 2;
num3 = num1 * num2;
num3 will be equal to 2
% means give the remainder of an integer divide between two values
e.g.
num1 = 1;
num2 = 2;
num3 = num1 / num2;
num3 will be equal to 2. since 1 divided by 2 = 0 with remainder of 2.
Order of operations (precedence)
Works the way you would expect it to work in algebra but watch out;
num1 = 1 + 1 / 2; /* equals 1 */
You can use braces () to change the order of operations.
How about an example program?
#include <stdio.h>
/* age enhancer */
void main()
{
int age;
printf("What is your age?\n");
scanf("%d",&age);
printf("In 10 years you will be %d years old\n",age+10);
}
notes:
Floating Point Numbers
Floating point numbers are those that have a fractional component like (real
numbers);
0.2,
.7,
8.,
9.0,
-56.9, and
1.98E07
Even More on Binary
Well, there was special notation to store negative numbers and there is
special notation to store floating point numbers as well.
When you declare a variable like;
float real_num;
it is stored in what is called single precision.
Single-precision floating point numbers have the following format:
This is a direct analogy to scientific notation.
Note how this type of storage is significantly different from integers.
Like there were different types of integers, there are basically 2 types of
floats
example:
float float_var; /* declaring a single precision floating point variable
*/
example:
double double_var; /* declaring a double precision floating point
variable */
There are certain characteristics that floats have that integers do not.
Floating Point Operators
There are four basic floating point operations
(in the following examples assume fnum1, fnum2 and fnum3 are float
variables.
+ means add two values
e.g.
fnum1 = 1.0;
fnum2 = 2.0;
fnum3 = fnum1 + fnum2;
num3 will be equal to 3.0 .
note the answer is not just 3 but 3.0...a floating point number;
- means subtract two values
e.g.
fnum1 = 1.0;
fnum2 = 2.0;
fnum3 = fnum2 - fnum1;
fnum3 will be equal to 1.0;
/ means float point divide two values
e.g.
fnum1 = 1;
fnum2 = 2;
fnum3 = fnum1 / fnum2;
fnum3 will be equal to 0.5.
* means multiply two values
e.g.
fnum1 = 1.0;
fnum2 = 2.0;
fnum3 = fnum1 * fnum2;
fnum3 will be equal to 2.0
An example program (with problems)
/* Temp Conversion: write a program to convert temperatures from
C to F
F = 32+ 9/5(C)
*/
#include <stdio.h>
void main()
{
float
f,c;
printf("Enter
the temperature in degree C\n");
scanf("%f",
&c);
f
= 32 + 9/5 * c;
printf("The
F temp is %f\n",f);
}
Notes:
the expression to produce a value for f is executed incorrectly. Lets say we
input the value 1.0;
Mixing floats with ints
You can have mixed expressions of floats and ints but C will do silent
conversions for you (meaning you have no
control over it). Sometimes this is not a problem, eg.
short j;
j = 3.0;
These statements work like we would probably expect, 3.0 gets converted or cast into the integer 3 and then the assignment to j
takes place.
Sometimes it is a problem. eg.
short j;
j = 3.9;
now j gets assigned the value 3 because C truncates 3.9 to 3...this is
probably not what you wanted.
Real problems can occur when you mix floats and ints if you are not careful,
eg.
short j,k=2,m=3;
float fnum=2.0,answer;
answer = k/m + fnum;
since division has higher precedence k/m becomes zero, zero gets cast to 0.0
and added to 2.0 to produce the value for answer of 2.0 which is not correct.
Casting
To avoid this silent conversion problem C allows you to deliberately convert
values. This is done by something called a cast.
To cast a value simply put the type that you want it to be before the value
and put braces around it. In our example above;
short j,k=2,m=3;
float fnum=2.0,answer;
answer = (float) k/m + fnum;
You often use explicit casting when dealling with certain library functions.
Sometimes a function wants a different type than you have...you can fix that
with a cast.
An example program (fixed)
/* Temp Conversion: write a program to convert temperatures from
C to F
F = 32+ 9/5(C)
*/
#include <stdio.h>
void main()
{
float
f,c;
printf("Enter
the temperature in degree C\n");
scanf("%f",
&c);
f
= 32 + (float) 9/5 * c;
printf("The
F temp is %f\n",f);
}
Notes:
the expression to produce a value for f is executed correctly. Lets say we
input the value 1.0;
Some Truth About printf() and scanf()
These are not part of the C programming language but are functions and are
part of the library <stdio.h>.
a function can be thought of as a self-contained part of a program that can
be called or invoked or used many times (we will see more later)
Like all function these are linked into your program at link time (after
compile time and before execution time).
They are kind of the mirror image of one another with printf() allowing
output to something called standard output
(usually your monitor) and scanf() allowing input from standard input (usually your keyboard).
Use the following handy list to make up useful version.
printf(fmt,exp1,exp2,...);
and
scanf(fmt,exp1,exp2,...);
where fmt is;
%[flags][width][conversion
character]
flags do a lot of stuff but we will not worry about it yet
width says how much stuff is in the data field and has the form
w.m
where w is the width of the whole field
and .m refers to the number of digits following the decimal (printf() only)
conversion characters of current interst;
% print or read %
d signed decimal integer
f float
lf double
e scientific notation
some examples
/* printing decimal integers */
#include <stdio.h>
void main()
{
printf("/%d/\n",336);
printf("/%2d/\n",336);
printf("/%10d/\n",336);
printf("/%-10d/\n",336);
}
this would yield the output
/336/
/336/
/ 336/
/336 /
/* printing floats */
#include <stdio.h>
void main()
{
printf("/%f/\n",1234.56);
printf("/%e/\n",1234.56);
printf("/%4.2f/\n",1234.56);
printf("/%3.1f/\n",1234.56);
printf("/%10.3f/\n",1234.56);
printf("/%10.3e/\n",1234.56);
}
/1234.560000/
/1.234560E+03/
/1234.56/
/1234.6/
/ 1234.560/
/ 1.234E+03/
it works pretty much the same way for scanf().
Math
There are many math functions in C. Most are written for you and accessed
through the math.h library. You can include this in your C program like you
would stdio.h. They work pretty much as described in the text.