Professional Documents
Culture Documents
and Engineering
Computation
(BIL 110E)
Lecture 5
Conditional Operators
&
Data Modifiers and Math Functions
1
sizeof (expression)
The operand of the sizeof operator may be a data type (such as int,
char, or float), or a constant or the name of a variable. 2
1
Conditional Operators: Measuring Data Sizes
Computer Screen
2
Conditional Operators: Logical operators
The AND and OR operators, are binary operators; that is, they
both take two operands.
3
Conditional Operators: AND Operator (&&)
#include <stdio.h>
int main( )
{
int num;
num = 1;
printf("%d && %d yields %d\n",(num%2 == 0), (num%3 == 0), (num%2 == 0) && (num%3 == 0));
num = 2;
printf("%d && %d yields %d\n",(num%2 == 0), (num%3 == 0), (num%2 == 0) && (num%3 == 0));
num = 3;
printf("%d && %d yields %d\n",(num%2 == 0), (num%3 == 0), (num%2 == 0) && (num%3 == 0));
num = 6;
printf("%d && %d yields %d\n",(num%2 == 0), (num%3 == 0), (num%2 == 0) && (num%3 == 0));
getchar();
return 0;
}
0 && 0 yields 0
1 && 0 yields 0
0 && 1 yields 0
1 && 1 yields 0 7
exp1 || exp2
4
Conditional Operators: OR Operator (||)
!expression
10
5
Conditional Operators: NEGATION Operator (!)
11
A bit can only hold the values 0 and 1 (0 and 1 are used to
represent the off and on states of electronic.)
12
6
Conditional Operators: Manipulating Bits
13
Example 1 Example 2
+ +
14
7
Conditional Operators: Manipulating Bits
How to convert a binary number into a decimal number
Second way:
x?y:z
8
Conditional Operators: x?y:z operation
For instance:
x > 0 ? ‘T’ : ‘F’
evaluates to ‘T’ if the value of x is greater than 0. Otherwise, the
conditional expression evaluates to the value ‘F’.
17
Data Modifiers
In order to have greater control over the data, the C keywords
for the four data modifiers are:
signed
unsigned
short
long
18
9
Data Modifiers : Enabling or Disabling the Sign Bit
19
if the int data type is 32 bits long and the rightmost bit is counted
as bit 0, you can use bit 31 (leftmost) as a sign bit.
When the sign bit is set to 1, the C compiler knows that the value
represented by the data variable is negative.
By default, all the integer data types except the char data type are
signed quantities
20
10
Data Modifiers : The unsigned Modifier
The unsigned modifier can be used to tell the C compiler that the
specified data type is only capable of holding non-negative
values.
The declaration
unsigned int x;
tells the C compiler that the integer variable x can only assume
positive values using 32 bits.
unsigned int x, y;
x = 12345U;
y = 0xABCDu;
you can use unsigned constant decleration by suffixing u or U. 21
short x;
decreases the size of bits for integer x. (Normally x is represented
by 32 bits but after short modifier x is represented by 16 bits)
22
11
Data Modifiers : Changing Data Sizes
The long Modifier
If you need more memory to keep values from a wider range, you
can use the long modifier to define a data type with increased
storage space.
For instance :
given an integer variable x that is 32 bits long, the declaration
long int x;
increases the size of x to at least 64 bits.
long int x, y;
x = 123456789l;
y = 0xABCD1234L;
you can use long constant decleration by suffixing l or L. 23
24
12
Data Modifiers : Adding h, l, or L to printf
You can add h into the integer format specifier (like this: %hd or
%hu) to specify that the corresponding argument is a short int or
unsigned short int.
25
13
Mathematical Functions in C: sin(), cos(), tan()
27
28
14
Mathematical Functions in C: pow(), sqrt()
C has no intrinsic operator for raising a number to a power.
#include <math.h>
double pow(double x, double y);
#include <math.h>
double sqrt(double x);
30
15