0% found this document useful (0 votes)
50 views35 pages

Structure of A C Program

This document summarizes key concepts about expressions in C programming, including the six categories of expressions and how they are evaluated based on precedence and associativity rules. It also discusses side effects in expression evaluation and type conversion. Examples of expressions and programs are provided to demonstrate postfix, prefix, unary, binary, and compound expressions. The objectives are to understand expression evaluation and be able to predict expression results.

Uploaded by

sumalathaprakash
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views35 pages

Structure of A C Program

This document summarizes key concepts about expressions in C programming, including the six categories of expressions and how they are evaluated based on precedence and associativity rules. It also discusses side effects in expression evaluation and type conversion. Examples of expressions and programs are provided to demonstrate postfix, prefix, unary, binary, and compound expressions. The objectives are to understand expression evaluation and be able to predict expression results.

Uploaded by

sumalathaprakash
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 35

Chapter 3

Structure of a C Program
Objectives
❏ To be able to list and describe the six expression categories
❏ To understand the rules of precedence and associativity in
evaluating expressions
❏ To understand the result of side effects in expression evaluation
❏ To be able to predict the results when an expression is evaluated
❏ To understand implicit and explicit type conversion
❏ To understand and use the first four statement types: null,
expression, return, and compound

Computer Science: A Structured Programming Approach Using C 1


3-1 Expressions
An expression is a sequence of operands and operators
that reduces to a single value. Expressions can be
simple or complex. An operator is a syntactical token
that requires an action be taken. An operand is an
object on which an operation is performed; it receives
an operator’s action.
Topics discussed in this section:
Primary Expressions
Postfix Expressions
Prefix Expressions
Unary Expressions
Binary Expressions
Computer Science: A Structured Programming Approach Using C 2
Note
An expression always reduces to a single value.

Computer Science: A Structured Programming Approach Using C 3


FIGURE 3-1 Expression Categories

Computer Science: A Structured Programming Approach Using C 4


FIGURE 3-2 Postfix Expressions

Computer Science: A Structured Programming Approach Using C 5


Note
(a++) has the same effect as (a = a + 1)

Computer Science: A Structured Programming Approach Using C 6


FIGURE 3-3 Result of Postfix a++

Computer Science: A Structured Programming Approach Using C 7


Note
The operand in a postfix expression must be a variable.

Computer Science: A Structured Programming Approach Using C 8


PROGRAM 3-1 Demonstrate Postfix Increment

Computer Science: A Structured Programming Approach Using C 9


PROGRAM 3-1 Demonstrate Postfix Increment (continued)

Computer Science: A Structured Programming Approach Using C 10


FIGURE 3-4 Prefix Expression

Computer Science: A Structured Programming Approach Using C 11


Note
The operand of a prefix expression must be a variable.

Computer Science: A Structured Programming Approach Using C 12


FIGURE 3-5 Result of Prefix ++a

Computer Science: A Structured Programming Approach Using C 13


Note
(++a) has the same effect as (a = a + 1)

Computer Science: A Structured Programming Approach Using C 14


PROGRAM 3-2 Demonstrate Prefix Increment

Computer Science: A Structured Programming Approach Using C 15


PROGRAM 3-2 Demonstrate Prefix Increment (continued)

Computer Science: A Structured Programming Approach Using C 16


Note
If ++ is after the operand, as in a++, the increment takes
place after the expression is evaluated.

If ++ is before the operand, as in ++a, the increment


takes place before the expression is evaluated.

Computer Science: A Structured Programming Approach Using C 17


FIGURE 3-6 Unary Expressions

Computer Science: A Structured Programming Approach Using C 18


Table 3-1 Examples of Unary Plus And Minus Expressions

Computer Science: A Structured Programming Approach Using C 19


FIGURE 3-7 Binary Expressions

Computer Science: A Structured Programming Approach Using C 20


Note
Both operands of the modulo operator (%) must
be integral types.

Computer Science: A Structured Programming Approach Using C 21


PROGRAM 3-3 Binary Expressions

Computer Science: A Structured Programming Approach Using C 22


PROGRAM 3-3 Binary Expressions (continued)

Computer Science: A Structured Programming Approach Using C 23


PROGRAM 3-3 Binary Expressions (continued)

Computer Science: A Structured Programming Approach Using C 24


Note
The left operand in an assignment expression must be a
single variable.

Computer Science: A Structured Programming Approach Using C 25


Table 3-2 Expansion of Compound Expressions

Computer Science: A Structured Programming Approach Using C 26


PROGRAM 3-4 Demonstration of Compound Assignments

Computer Science: A Structured Programming Approach Using C 27


PROGRAM 3-4 Demonstration of Compound Assignments

Computer Science: A Structured Programming Approach Using C 28


PROGRAM 3-4 Demonstration of Compound Assignments

Computer Science: A Structured Programming Approach Using C 29


3-2 Precedence and Associativity

Precedence is used to determine the order in which


different operators in a complex expression are
evaluated. Associativity is used to determine the order
in which operators with the same precedence are
evaluated in a complex expression.

Topics discussed in this section:


Precedence
Associativity

Computer Science: A Structured Programming Approach Using C 30


PROGRAM 3-5 Precedence

Computer Science: A Structured Programming Approach Using C 31


PROGRAM 3-5 Precedence

Computer Science: A Structured Programming Approach Using C 32


Computer Science: A Structured Programming Approach Using C 33
FIGURE 3-8 Left-to-Right Associativity

Computer Science: A Structured Programming Approach Using C 34


FIGURE 3-9 Right-to-Left Associativity

Computer Science: A Structured Programming Approach Using C 35

You might also like