Professional Documents
Culture Documents
Types, Operators, and Express Ions
Types, Operators, and Express Ions
ions
Background
Basic data objects in a program
Variable, constant
Data types of variables
Operators on variable
Expressions to assign values to
variables
Outline
Variable name
Data types and sizes
Constants
Declarations
Arithmetic operators
Relational and logical operators
Type conversions
Increment and decrement operators
Bitwise operators
Assignment operators and expressions
Conditional expressions
Precedence and order of evaluation
Variable names
Naming conventions
Consist of letters and digits and “_”
Case sensitive
First character must be a letter
A habit
Lower case for variable names
Upper case for symbolic constants
Avoid reserved keywords
Meaningful names
Outline
Variable name
Data types and sizes
Constants
Declarations
Arithmetic operators
Relational and logical operators
Type conversions
Increment and decrement operators
Bitwise operators
Assignment operators and expressions
Conditional expressions
Precedence and order of evaluation
Data types and sizes
char : one byte
int : one integer, 16 or 32 bits
Machine
dependent
short : short integer, 16 bits
long : long integer, 32 bits
float : single-precision floating point
double: double-precision floating point
Data types and sizes (cont.)
unsigned char : 8 bits, 0 to 255
signed char : 8 bits, -128 to 127
unsigned int
signed int
compile
char line[1000+1];
String constants
Character arrays = strings
“Hello\n”
H e l l o \n \0
Null character
‘x’ : character
“x” : string contains x \0
String constants (cont.)
Count the string length(up to ‘\0’)
int strlen(char s[])
{
int i;
i=0;
while (s[i] != ‘\0’)
++i;
return i;
}
Enumeration constants
enum months { JAN=1, FEB, MAR, APR, MAY, JUN, JUL
AUG, SEP, OCT, NOV, DEC};
#define JAN 1
#define FEB 2
=> #define MAR 3
Automatically generate …
corresponding constants #define DEC 12
Outline
Variable name
Data types and sizes
Constants
Declarations
Arithmetic operators
Relational and logical operators
Type conversions
Increment and decrement operators
Bitwise operators
Assignment operators and expressions
Conditional expressions
Precedence and order of evaluation
Declaration
int lower; /* …*/
int lower, upper, step; int upper;
char c, line[1000]; int step;
char c;
char line[1000];
Initialize in declarations
char esc = ‘\\’; For automatic variables,
int i = 0; this initialization is done
int limit = MAXLINE + 1; when the function is entered
float eps = 1.0e-5;
Declaration (cont.)
Qualifier const
const double e = 2.71828182845905;
const char msg[] = “warning: “;
! (NOT)
if( ! valid) if (valid == 0)
Outline
Variable name
Data types and sizes
Constants
Declarations
Arithmetic operators
Relational and logical operators
Type conversions
Increment and decrement operators
Bitwise operators
Assignment operators and expressions
Conditional expressions
Precedence and order of evaluation
Type conversion
When operator has operands of
different types
Rule
Convert a “narrow” operand into a “wider”
one without losing information
Type conversion : example 1
char to int
int atoi(char s[])
{
int i, n;
n = 0;
for (i=0; s[i] >= ‘0’ && s[i] <= ‘9’; ++i)
n = 10 * n + (s[i] – ‘0’);
return n;
}
Example 1 Example 2
int i; float x;
char c; int i;
i = c; x = i;
c = i; i = x;
Type conversion: argument in
function calls and cast
Cast: forced type conversion
(type-name) expression
Example: int n;
…
* Compiler issue
Outline
Variable name
Data types and sizes
Constants
Declarations
Arithmetic operators
Relational and logical operators
Type conversions
Increment and decrement operators
Bitwise operators
Assignment operators and expressions
Conditional expressions
Precedence and order of evaluation
Increment and decrement
operators
Increment
x=n++; postfix (increment after…)
x=++n; prefix (increment before…)
i = j = 0; t ggyyggy\0
while ( s[i] != ‘\0’)
i++;
j
while( (s[i++] = t[j++]) != ‘\0’)
;
}
Outline
Variable name
Data types and sizes
Constants
Declarations
Arithmetic operators
Relational and logical operators
Type conversions
Increment and decrement operators
Bitwise operators
Assignment operators and expressions
Conditional expressions
Precedence and order of evaluation
Bitwise operators
6 bitwise operators
& bitwise AND
| bitwise inclusive OR
10100110
^ bitwise exclusive OR …
<< left shift 00100101
>> right shift
~ one’s complement
Apply to integral operands
char, short, int, long
Bitwise operators (cont.)
AND: used to mask some bits
n = n & 0177
01 111 111
NOT: ~
x = x & ~077
Bitwise operators: example
/* getbits: get n bits from position p */
unsigned getbits (unsigned x, int p, int n)
{
return (x >> (p+1-n) & ~(~0 << n));
}
Ex. getbits(x, 4, 3)
76543210
76543210 00000111
11111000
11111111
Outline
Variable name
Data types and sizes
Constants
Declarations
Arithmetic operators
Relational and logical operators
Type conversions
Increment and decrement operators
Bitwise operators
Assignment operators and expressions
Conditional expressions
Precedence and order of evaluation
Assignment operators
Example:
i = i+2; i += 2;
General form
expr1 = (expr1) op (expr2)
expr1 op= expr2
op can be any binary operators
Example:
x *= y+1 x = x * (y+1);
Assignment operators:
example
/* bitcount: count number of 1-bits in x */
int bitcount ( unsigned x )
{
int b;
General form:
expr1 ? expr2 : expr3;
Conditional expression (cont.)
Advantage: it generates compact code
Example: print 10 array elements per line
for ( i=0; i < n; i++)
printf(“%6d%c”, a[i], (i%10 == 9 || i == n-1)? ‘\n’ : ‘ ’);
Example: 複數形
Printf(“You have %d item%s.\n”, n, n == 1 ? “” : “s”);
Outline
Variable name
Data types and sizes
Constants
Declarations
Arithmetic operators
Relational and logical operators
Type conversions
Increment and decrement operators
Bitwise operators
Assignment operators and expressions
Conditional expressions
Precedence and order of evaluation
Precedence of operators
() [] -> .
! ~ ++ -- + - * & (type) sizeof
* / %
+ -
If you are not sure
<< >> of anything,
< <= > >= Use brackets!!!
== !=
&
^
|
&&
||
?:
= += -= *= /= %= &= ^= |= <<= >>=
,
Order of evaluation
NO specified order of evaluation except
&&, ||, ?:
x = f() + g(); Not sure f or g is evaluated first
++n;
printf(“%d %d\n”, n, power(2, n));