Lecture 6: Assignment, scanf and calculations |
Giving a new value to a variable is called assignment. In C this
is done with the operator
a = 3.0;
b = 3.0*a;
c = cos(t);
wrong (assuming a is of type float):
1.0 = a;
a = TRUE;
a = 1;
In reality, for C the last example was correct. C knows what we want and helps us by converting the integer 1 to the real 1.0. In any case, it is bad practice to mix floats with ints and we'd better write: a = 1.0;
To summarize, the = symbol is
NOT part of a comparison ("a is equal to b?") NOT part of an equation ("a2 = 2a -1") it IS an assignment ("a takes the value of b") |
after executing the line |
after executing the line |
|
main()
{ float a; float b; float c = 4.3; a = 1.0;
|
1.0 1.0 9.6 |
undefined
|
In
the previous lectures we have learned how to show the results of our calculations
on the screen. With printf we can make our program have output.
In many cases, we would also like our program to have
input. The
user enters his name, or enters numbers that our program has to process.
Or even more simple, we want that the users can control the program. For
example, we want that the user can stop the program with a simple stroke
of the escape key.
The format descriptions are the same as for the output instruction printf. For example %d for int's, etc. Note that the variables to be read do not have to be of the same type. As an example // lines starting with these // are
cooment
main()
// don't forget to declare the variables
we will use
printf("Please enter two
numbers separated by a space\n");
When run, the program will display the message
|
|
|
|
|
|
|
|
|
|
|
These four operators, when used for calculations, need two operands
and are called binary operators. In C one is placed on the left side and
one on the right side. As examples:
|
|
|
|
|
|
These expressions will result in values that can be assigned to variables as we have seen above. As an example:
c = 3 * a;
Note again, on the left side of the assignment symbol = we have a variable and on the left side we put our expression resulting in a new value for the variable.
In C There also exists unary operators which need only one operand:
operator | operation |
|
increment |
|
decrement |
|
negate (boolean algebra) |
|
invert all bits of this int |
|
negate number |
|
address of .. |
For example
i++;
--k;
Increases the value of i and decreases the value of k by 1 respectively.
The operator can be placed before or after the operator. The difference
is the moment the variable is changing value, before execution of the rest
of the instruction (++i) or after (i++). For example
i = 1;
j = i++; at the end
|
i = 1;
j = ++i; at the end
|
The ! and ~ operators we will see later in the lecture on Boolean algebra.
The get-address operator & will be discussed in the lecture
on pointers.
The unary - operator returns the negated number (floating point or
integer).
The operators shown in the previous section are used for floating
point calculations. For integer calculations (used for types like byte,
word, integer, longint, etc), the division operator works a little different.
Imagine the calculation of, for example, 7/3. As we have learned in primary
school, this is equal to 2 with a remainder of 1 to be divided
by 3:
|
|
||
|
|
|
|
|
|
In C exist two operators / and % that reproduce these results. Example:
|
|
|
|
|
|
These replace the floating-point operator /. The other three operators
(*, +, -) are the same for integer numbers.
printf("Give the value of the first
variable x:\n");
scanf("%f", &x);
printf("Give the value of the second
variable y:\n");
scanf("%f", &y);
sum = x + y;
printf("The sum of %f and %f is %f\n",
x, y, sum);
diff = x - y;
printf("The difference between %f
and %f is %f\n", x, y, diff);
divis = x / y;
printf("%f divided by %f is %f\n",
x, y, divis);
}
will produce, when running:
Give the value of the first variable x:
3.4
Give the value of the second variable y:
1.8
The sum of 3.4000 and 1.8000 is 5.2000
The difference between 3.4000 and 1.8000
is 1.6000
3.4000 divided by 1.8000 is 1.8889
printf("Give the value of the first
variable x:\n");
scanf("%d", &x);
printf("Give the value of the second
variable y:\n");
scanf("%d", &y);
sum = x + y;
printf("The sum of %d and %d is %d\n",
x, y, sum);
diff = x - y;
printf("The difference between %d
and %d is %d\n", x, y, diff);
divis = x / y;
divr = x % y;
printf("%d divided by %d is %d plus
%d/%d\n", x, y, divis, divr, y);
}
will produce, when running:
Give the value of the first variable x:
13
Give the value of the second variable y:
5
The sum of 13 and 5 is 18
The difference between 13 and 5 is 8
13 divided by 5 is 2 plus 3/5