55
Data types
main( ) { int a, b, c, sum;
a = 1; b = 2; c = 3;
sum = a + b + c;
printf("sum is %d", sum);
}
main( ) { int a, b;
float
c, sum;
a = 1; b = 2; c = 3.5;
sum = a + b + c;
printf("sum is %f", sum);
}

56
Numeric data types
l
char
l
Individual characters
(Range 127 to -128)
l
int
l
Integers
l
Short
(-65536 to 65535) or
l
Long
-2,147,483,648 to 2,147,483,647
l
float
l
Real numbers
3.4 E +/- 38
(32 bits long)
l
double
l
Real numbers with double precision
3.4 E +/- 308
(64 bits long)
l
Modifiers
l
Short (16 bit), long
(32bit)
l
Control
the range of numbers
l
signed, unsigned
l
for integers and whole
numbers respectively

57
Arithmetic Operators
l
Symbol
Operation
Usage
l
*
multiply
x * y
l
/
divide
x / y
l
%
modulo
x % y
l
+
addition
x + y
l
-
subtraction
x - y
l
All associate left to right.
l
* / %
have higher precedence than
+ -
.

58
Special Operators: ++ and --
l
Changes value of variable before (or after)
its value is used in an expression.
l
Symbol
Operation
Usage
l
++
postincrement
x++
l
--
postdecrement
x--
l
++
preincrement
++x
l
<=
predecrement
--x
l
Pre
:
Increment/decrement variable
before
using its value.
l
Post
: Increment/decrement variable
after
using its value.

@2012
Badri Nath
Computer Architecture
211
59
Examples
l
#include <stdio.h>
#include <stdlib.h>
int main()
{
char weight[4];
int w;
w=140;
printf("Here is what you weigh now: %i\n",w);
w--;
printf("w--: %i\n",w);
w++;
printf(
“
++w: %i\n",w);
printf (
“
pre DECR
%i \n
”
, --w);
printf (
“
post INCR %i \n
”
, w++);
printf (
“
value of w %i \n
”
, w);
return(0);
}
#include<stdio.h>
main( )
{
int i = 3, j = 4,k;
k = i++ + --j;
printf("i = %d, j = %d, k = %d",i,j,k);
}

60
Relational Operators
l
Symbol
Operation
Usage
l
>
greater than
x > y
l
>=
greater than or equal
x >= y
l
<
less than
x < y
l
<=
less than or equal
x <= y
l
==
equal
x == y
l
!=
not equal
x != y
l
Result is 1 (TRUE) or 0 (FALSE).
l
Note
: Don't confuse equality (==) with assignment (=).

61
Logic Operators
l
Symbol
Operation
Usage
l
!
logical NOT
!x
l
&&
logical AND
x && y
l
||
logical OR
x || y
l
Treats entire variable (or value) as TRUE
(non-zero) or FALSE (zero).
l
Result is 1 (TRUE) or 0 (FALSE).

62
Bit operators
l
In C, there are operators that work on bits of a word
l
&
logical AND
l
|
inclusive OR
l
~
NOT
l
Example
l
x = 8
y = 7
l
x& y
l
x | y
l
! X
l
E.g., 72 & 184 = 8 ; 72 | 184 = 248 ;

@2012
Badri Nath
Computer Architecture
211
63
Variable Declarations
l
Variables are used as names for
data items.
l
Each variable has a
type
,
which tells the compiler how the
data is to be interpreted (and how
much space it needs, etc.).
l
int
counter;
l
int
startPoint;
l
Float pi=3.14;
l
int
is a predefined
integer type in C.
l
#include <stdio.h>
int main()
{
int pints=1;
float price = 1.45;
printf("You want %d
pint.\n",pints);
printf("That be $
%f, please.\n
“
, price);
return(0);
}

64
Control Structures
l
Same control structures as Java. Same syntax
l
Conditional
l
if
l
if-else
l
switch
l
Iteration
l
while
l
for
l
do-while
l
also has the
break
and
continue
expressions.

65
Control Structures
l
Same control structures as Java. Same syntax
l
Conditional
l
if
l
if-else
l
switch
l
Iteration
l
while
l
for
l
do-while
l
also has the
break
and
continue
expressions.
