Operands and Operators - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Operands and Operators

Description:

char used to declare variables for storing character (textual) data, e.g. A' ... printf('tIts circumference is %.2f mmn', circ) ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 15
Provided by: taz9
Category:

less

Transcript and Presenter's Notes

Title: Operands and Operators


1
Operands and Operators
  • data types
  • operators

2
Basic Data Types
  • char used to declare variables for storing
    character (textual) data, e.g. A
  • int used to declare variables that store integer
    values, e.g. 100
  • float used to declare variables that store real
    (floating point) values, e.g. 0.12345
  • double used to declare variables that store
    double precision values, e.g 3.1415926536
  • Quantifiers such as long, short, signed and
    unsigned could be applied to these basic data
    types
  • Complex data types include enumerations, arrays,
    structures, unions, lists, stacks, queues, trees
  • Pointers are a special data type that can hold a
    memory address

3
Data
  • Variables
  • global variables
  • include ltstdio.hgt
  • int num100 / declaration of global
    variable /
  • int main()
  • printf("Global value is d", num)
  • return 0
  • local variables
  • include ltstdio.hgt
  • int num100 / declaration of global
    variable /
  • int main()
  • int num10 / declaration of local variable /
  • printf(Local value is d", num)
  • return 0

4
Data
  • attributes of Variables or identifiers
  • name
  • type
  • size
  • value
  • other attributes include storage class, storage
    duration, scope and linkage

int total int total 0
5
Data
  • Constants are declared using const keyword

include ltstdio.hgt main()
const float PI 3.141593 int
diameter float radius, circ, area printf("\nE
nter the diameter of a ") printf(" circle in
mm ") scanf("d", diameter) circ
(float) PI diameter radius (float) diameter
/ 2 area (float) PI (radius
radius) printf("\tIts circumference is .2f
mm\n", circ) printf("\tAnd its area is .2f
sq.mm\n ", area) return 0
Symbolic Constants are defined in the
preprocessor area of the program They are
declared using define CONSTANT_NAME Value
6
Operators
  • Arithmetic Operators
  • Assignment
  • Addition
  • Subtraction -
  • Multiplication
  • Division /
  • Modulus
  • Parenthesis () is used as in algebra
  • variable expression
  • Relational or comparison operators
  • gt gt greater than
  • lt lt less than
  • gt greater than or equal to
  • lt lesser than or equal to
  • if (condition)
  • statement
  • Equality Operators
  • equivalent
  • ? ! not equal

7
Operators
  • Logical Operators
  • AND
  • OR
  • ! NOT
  • if (condition1 AND condition2)
  • statement
  • Unary Operators
  • unary minus
  • sizeof(data_type)
  • (type) variable_name
  • Increment
  • Decrement Operators
  • i i 1 is equivalent to i
  • i i - 1 is equivalent to --i
  • prefix form variable is changed before
    expression is evaluated
  • i --i
  • postfix form variable is changed after
    expression is evaluated
  • i i--

8
Rules
  • Precedence
  • order of evaluation
  • Associativity
  • direction of evaluation

Precedence
9
L-Value R-Value
  • L-values
  • Expressions that can appear on left side of
    equation
  • Can be changed (i.e., variables)
  • e.g. x 4
  • R-values
  • Only appear on right side of equation
  • Constants, such as numbers (i.e. cannot write 4
    x)
  • L-values can be used as R-values, but NOT vice
    versa

10
Confusing Equality () and Assignment ()
Operators
  • Example
  • if ( payCode 4 )
  • printf ("You get a bonus!\n)
  • If paycode is 4, bonus given
  • If was replaced with
  • if ( payCode 4 ) printf ("You get a
    bonus!\n)
  • Paycode set to 4 (no matter what it was before)
  • Statement is true (since 4 is non-zero)
  • Bonus given in every case

11
Operators - demo
Output Addition 9 Division 0 Modulus
4 Postfix increment 4 Now a is 5 Prefix
increment 6 Now a is 6 Postfix decrement 5
Now b is 4 Prefix decrement 3 Now b is 3
  • include ltstdio.hgt
  • int main ()
  • int a4, b5, result
  • result a b
  • printf("Addition d\n", result)
  • result a / b
  • printf("Division d\n", result)
  • result a b
  • printf("Modulus d\t\n", result)
  • printf("Postfix increment d\n", a)
    printf("Now a is d\n", a)
  • printf("Prefix increment d\n", a)
    printf("Now a is d\n", a)
  • printf("Postfix decrement d\n", b--)
    printf("Now b is d\n", b)
  • printf("Prefix decrement d\n", --b)
    printf("Now b is d\n", b)
  • return 0

12
Type Conversion
  • C allows for conversions between the basic types,
    implicitly or explicitly
  • Explicit conversion uses the cast operator
  • Example 2
  • int i
  • short int j1000
  • ijj / wrong!!! /
  • i(int)j (int)j / correct /

Example 1 int x10 float y,z3.14 y(float) x
/ y10.0 / x(int) z / x3
/ x(int) (-z) / x-3 -- rounded
approaching zero /
13
Implicit Conversion
  • If the compiler expects one type at a position,
    but another type is provided, then implicit
    conversion occurs.
  • Conversion during assignments
  • char c'a'
  • int i
  • ic / i is assigned by the ascii of a /
  • Arithmetic conversion if two operands of a
    binary operator are not the same type, implicit
    conversion occurs
  • int i5 , j1
  • float x1.0 , y
  • y x / i / y 1.0 / 5.0 /
  • y j / i / y 1 / 5 so y 0 /
  • y (float) j / i / y 1.0 / 5 /
  • / The cast operator has a higher precedence /

14
C Promotion Rules
  • The C compiler uses the following diagram to
    promote the types if two operands do not agree in
    expressions

long double
double
float
unsigned long int
long int
unsigned int
int
char
Write a Comment
User Comments (0)
About PowerShow.com