Home   MosaicPresentation format
<http://cnfolio.com/IntroToComputingTopic03Print>
Introduction to Computing – B142L

Working with numbers

Academic year 2011/12



Objectives:
  1. Basic method to obtain input data
  2. Use arithmetic, assignment, comparison and logic operators






Basic method to obtain input








 



There are detailed formatting rules to specify the data type when obtaining input and displaying output:






An expression is a combination of operators and operands



  1. Unary operators require only 1 operand, e.g. line 6 below.
  2. Binary operators require 2 operands, e.g. line 9 below.
  3. There is only one ternary operator in the C language, which is the conditional operator ? : that requires 3 operands, e.g. line 12 below.

 






Automatic data type conversion



The C compiler automatically converts the value on the right side of an assignment operation to match the data type of the variable on the left side.

 






Arithmetic operators



OperatorExampleMeaning
*x * yMultiply x and y
/x / yDivide x by y
%x % yThe remainder after dividing x by y
+x + yAdd x and y
-x - ySubtract y from x
++xValue of x
--xArithmetic negative of x
++++xIncrease x by 1 before using x
----xDecrease x by 1 before using x
++x++Increase x by 1 after using x
--x--Decrease x by 1 after using x

Use arithmetic operators to perform the tasks below:

 






Evaluation trees show order of precedence



Operators in C arithmetic expressions follow the same order of precedence as normal math expressions.
  1. Parentheses are the highest precedence
  2. Unary operators, such as ++ or --
  3. Positive and negative sign operators, e.g. + or -
  4. Multiplication and division operators
  5. Addition and subtraction operators
  6. Assignment operators are the lowest precedence, such as = or *=

 








Parentheses can make programs easier to read



Like math expressions, parentheses can be used to indicate specific orders of precedence. The use of extra parentheses in a complex arithmetic expression can make it easier to read and change later.






Division depends on the data type of the operands



The result of division using all integer operands is an integer value. If a fractional result is required, then one of the operands must be forced to be float or double type.

 






Assignment operators



OperatorExampleMeaning
=x = yAssign the value of y to the variable x
*=x *= yAssign the product of x multiplied by y to the variable x
/=x /= yAssign the quotient of x divided by y to the variable x
+=x += yAssign the sum of x and y to the variable x
-=x -= yAssign the result of subtracting y from x to the variable x

When using the combination assignment operators, e.g. *=, the expression on the right side of the operator is fully evaluated before the assignment operator is applied.

 






Comparison operators only have 2 possible values




OperatorExampleMeaning
<x < y1 if x is less than y
<=x <= y1 if x is less than or equal to y
>x > y1 if x is greater than y
>=x >= y1 if x is greater than or equal to y
==x == y1 if x is equal to y
!=x != y1 if x is not equal to y

 






Be careful using equal signs



Errors often happen with the use of the equal signs:






Logic operators only have 2 possible values




OperatorExampleMeaning
&&x && y1 if the values of x and y are both not 0
||x || y1 if the values of x or y are not 0
!!x1 if the value of x is equal to zero.

 






Review questions




Working with numbers


In the C program below, what should replace the XXX placeholder at line 7?

  1. #include <stdio.h>
  2.  
  3. int main( void )
  4. {
  5.    int age;
  6.  
  7.    scanf( "%d", XXX );
  8.  
  9.    age += 3;
  10.    printf( "You will be %d years old after 3 years of university.", age );
  11. }








Review questions




Working with numbers


In the C program below, how could you replace the unary operator with a binary operator?

  1. #include <stdio.h>
  2.  
  3. int main( void )
  4. {
  5.    int age;
  6.  
  7.    scanf( "%d", &age );
  8.  
  9.    age += 3;
  10.    printf( "You will be %d years old after 3 years of university.", age );
  11. }








Review questions




Working with numbers


In the C program below, what is the displayed output?

  1. #include <stdio.h>
  2.  
  3. int main( void )
  4. {
  5.    int age;
  6.  
  7.    scanf( "%d", &age );
  8.  
  9.    age += 3;
  10.    printf( "%d", ( age + 4 ) > ( 4 + age++ ) );
  11. }

1
false
true
0






Review questions




Working with numbers


In the C program below, how could you change the comparison operator so that the displayed output would be 1?

  1. #include <stdio.h>
  2.  
  3. int main( void )
  4. {
  5.    int age;
  6.  
  7.    scanf( "%d", &age );
  8.  
  9.    age += 3;
  10.    printf( "%d", ( age + 4 ) > ( 4 + age++ ) );
  11. }


Average rating is