computer methods c programming
computer methods c programming
LECTURE 4
This is a poor handle of the situation since we need to remember what the value
2 stands for.
An improvement could be achieved by using the pre-processor to give names to
the values:
#define club 0
#define diamond 1
#define heart 2
#define spade 3
int card;
...
card = heart;
C offers a more compact way of achieving the same effect. The enumeration
type enables us to set the values which a variable may take without the need for
many #define directives
enum suit {club, diamond, heart, spade};
Which defines a new type enum suit together with the values it may take; We
may then declare variables of type:
enum suit card;
Operators / and %
When applied to integer operands, the operators / and % calculate the integer
quotient and remainder, respectively.
38 / 7 = 5 and 38 % 7 = 3
n p q r s t
10.0 4.8 -2.0 8.5 12.0 0.5
p / -q + (r - s + t) * n
----- Within parentheses,
-3.5 - and + are of the
-------- same precedence:
-3.0 apply left associativity
---
2.0 Unary operators have highest
-------- precedence / and
2.4 * have same precedence
-------------- Apply left associativity.
-30 + has lowest precedence
--------------------------
-27.6
/***************************************************
* cm18_3_3.c -- Casting *
***************************************************/
#include <stdio.h>
int main()
{
float PseudoIntNumber = (int)(12.5/2.0);
float FloatNumber = (float) 13/(float) 2;
printf("\nPseudoIntNumber holds %f ", PseudoIntNumber );
printf("\nFloatNumber holds %f ", FloatNumber );
return 0;
}