Arithmetic in C

Arithmetic in C

ARITHMETIC IN C Operators PROGRAM output data input data Keyboard Processing

Screen Next step to focus on is the process (operations) 1 ARITHMETIC IN C 1992-2013 by Pearson Education, Inc. All Rights Reserved. 1 ARITHMETIC IN C (Cont.)

Most C programs perform calculations using the C arithmetic operators (Fig. 2.9). Note the use of various special symbols not used in algebra. The asterisk (*) indicates multiplication and the percent sign (%) denotes the remainder operator, which is introduced below.

In algebra, to multiply a times b, we simply place these singleletter variable names side by side as in ab. In C, however, if we were to do this, ab would be interpreted as a single, two-letter name (or identifier). Therefore, C requires that multiplication be explicitly denoted by using the * operator as in a * b. The arithmetic operators are all binary operators. For example, the expression 3 + 7 contains the binary operator + and the operands 3 and 7. 1992-2013 by Pearson Education, Inc. All Rights Reserved. 1.1 ARITHMETIC IN C (Cont.) Integer Division and the Remainder Operator

Integer division yields an integer result. For example, the expression 7 / 4 evaluates to 1 and the expression 17 / 5 evaluates to 3. C provides the remainder operator, %, which yields the remainder after integer division. The remainder operator is an integer operator that can be used only with integer operands. The expression x % y yields the remainder after x is divided by y. Thus, 7 % 4 yields 3 and 17 % 5 yields 2. 1992-2013 by Pearson Education, Inc. All Rights Reserved.

1992-2013 by Pearson Education, Inc. All Rights Reserved. 1.2 ARITHMETIC IN C (Cont.) Parentheses for Grouping Subexpressions Parentheses are used in C expressions in the same manner as in algebraic expressions. For example, to multiply a times the quantity b + c we write a*(b+c) 1992-2013 by Pearson Education, Inc. All Rights

Reserved. 1.3 ARITHMETIC IN C (Cont.) Rules of Operator Precedence C applies the operators in arithmetic expressions in a precise sequence determined by the following rules of operator precedence, which are generally the same as those in algebra: Operators in expressions contained within pairs of parentheses are evaluated first. Parentheses are said to be at the highest level of precedence. In cases of

nested, or embedded, parentheses, such as ( ( a + b ) + c ) the operators in the innermost pair of parentheses are applied first. 1992-2013 by Pearson Education, Inc. All Rights Reserved. 1.3 ARITHMETIC IN C (Cont.) Multiplication,

division and remainder operations are applied next. If an expression contains several multiplication, division and remainder operations, evaluation proceeds from left to right. Multiplication, division and remainder are said to be on the same level of precedence. Addition and subtraction operations are evaluated next. If an expression contains several addition and subtraction operations, evaluation proceeds from left to right. Addition and subtraction also have the same level of precedence, which is lower than the precedence of the multiplication, division and remainder operations. The assignment operator (=) is evaluated last.

1992-2013 by Pearson Education, Inc. All Rights Reserved. 1.3 ARITHMETIC IN C (Cont.) The rules of operator precedence specify the order C uses to evaluate expressions. When we say evaluation proceeds from left to right, were referring to the associativity of the operators. Figure 2.10 summarizes these rules of operator

precedence for the operators weve seen so far. 1992-2013 by Pearson Education, Inc. All Rights Reserved. 1992-2013 by Pearson Education, Inc. All Rights Reserved. 1.3 ARITHMETIC IN C (Cont.)

EX: What is the result of the following expression? In c , the expression is: 2*5*5+3*5+7 Figure 2.11 illustrates the order in which the operators are applied. 1992-2013 by Pearson Education, Inc. All Rights Reserved.

1992-2013 by Pearson Education, Inc. All Rights Reserved. 2. RELATIONAL AND EQUALITY OPERATORS LOWER HIGHER 1992-2013 by Pearson Education, Inc. All Rights Reserved. 2. RELATIONAL AND EQUALITY

OPERATORS (CONT.) The relational operators used in the C language are shown in Fig. 2.12. The relational operators all have the same level of precedence and they associate left to right.

The equality operators have a lower level of precedence than the relational operators and they also associate left to right. In C, a condition may actually be any expression that generates a zero (false) or nonzero (true) value. 1992-2013 by Pearson Education, Inc. All Rights Reserved. 1992-2013 by Pearson Education, Inc. All Rights Reserved. 1992-2013 by Pearson Education, Inc. All Rights

Reserved. 3. A SAMPLE PROGRAM 1992-2013 by Pearson Education, Inc. All Rights Reserved. 3. A SAMPLE PROGRAM (Contd) 1992-2013 by Pearson Education, Inc. All Rights Reserved. 3. A SAMPLE PROGRAM (Contd)

The program uses scanf (line 15) to input two numbers. Each conversion specifier has a corresponding argument in which a value will be stored. The first %d converts a value to be stored in the variable num1, and the second %d converts a value to be stored in variable num2.

1992-2013 by Pearson Education, Inc. All Rights Reserved. 1992-2013 by Pearson Education, Inc. All Rights Reserved. 4. RULES OF PRECEDENCE REVISITED

Figure 2.14 lists from highest to lowest the precedence of the operators introduced in this chapter. Operators are shown top to bottom in decreasing order of precedence. The equals sign is also an operator. 1992-2013 by Pearson Education, Inc. All Rights Reserved. 1992-2013 by Pearson Education, Inc. All Rights Reserved.

1992-2013 by Pearson Education, Inc. All Rights Reserved. THE ASSIGNMENT OPERATOR Assignment is used to assign the values for the variables in C programs. Assignment operator stores a value in memory. The syntax is leftSide

= Allways Allwaysititisisaa variable variableidentifier. identifier. rightSide ; ItItisiseither eitheraaliteral literal| | aavariable

variable identifier identifier| |an an expression. expression. Examples: i = 1; first = i; sum = firstNumber + secondNumber;

2.6 THE ASSIGNMENT STATEMENT Integer1 + integer2 = sum 1992-2013 by Pearson Education, Inc. All Rights Dr. Soha S. Zaghloul 26 Copyright Pearson, Inc. 2013. All Rights Reserved. Reserved. 2.8 MEMORY CONCEPTS

Remember in previous example: int integer1 , integer 2 , sum; Variable names such as integer1, integer2 and sum actually correspond to locations in the computers memory.

EX: integer1 = 45 ; integer 2 = 72; sum = 0; is executed, the value is placed into a memory location to which the name integer1 , integer2 , sum , has been assigned. 1992-2013 by Pearson Education, Inc. All Rights Dr. Soha S. Zaghloul 27 Reserved. Copyright Pearson, Inc. 2013. All Rights Reserved.

sum 0 1992-2013 by Pearson Education, Inc. All Rights Dr. Soha S. Zaghloul 28 Copyright Pearson, Inc. 2013. All Rights Reserved. Reserved. 2.8 MEMORY CONCEPTS (Cont.)

Whenever a value is placed in a memory location, the value replaces the previous value in that location; thus, this process is said to be destructive. These locations are not necessarily adjacent in memory. EX:

sum = integer1 + integer2; /* assign total to sum */ that performs the addition also replaces whatever value was stored in sum. 1992-2013 by Pearson Education, Inc. All Rights Dr. Soha S. Zaghloul 29 Reserved. Copyright Pearson, Inc. 2013. All Rights Reserved.

2.8 MEMORY CONCEPTS (Cont.) This occurs when the calculated total of integer1 and integer2 is placed into location sum (destroying the value already in sum). After sum is calculated, memory appears as in Fig. 2.8.

The values of integer1 and integer2 appear exactly as they did before they were used in the calculation. 1992-2013 by Pearson Education, Inc. All Rights Dr. Soha S. Zaghloul 30 Reserved. Copyright Pearson, Inc. 2013. All Rights Reserved. Value of sum has changed after assignment

1992-2013 by Pearson Education, Inc. All Rights Dr. Soha S. Zaghloul 31 Copyright Pearson, Inc. 2013. All Rights Reserved. Reserved. 2.8 MEMORY CONCEPTS (Cont.) They were used, but not destroyed, as the

computer performed the calculation. Thus, when a value is read from a memory location, the process is said to be nondestructive. 1992-2013 by Pearson Education, Inc. All Rights Dr. Soha S. Zaghloul 32 Reserved. Copyright Pearson, Inc. 2013. All Rights Reserved.

Recently Viewed Presentations

  • Neurologic Trauma 8-10 Questions Monti Smith, MSN, RN

    Neurologic Trauma 8-10 Questions Monti Smith, MSN, RN

    Promote balanced activity & rest Keep them busy during the day so they'll be pooped at night and sleep, not wander around Educate on home & community based assistance Amyotrophic Lateral Sclerosis Lou Gehrig's Disease Definition - A disease of...
  • EGEE Middleware

    EGEE Middleware

    Mixed Attributes ¤ Association between one CE and one or more SEs (objectclass: GlueCESEBindGroup) • GlueCESEBindGroupCEUniqueID: unique ID for the CE • GlueCESEBindGroupSEUniqueID: unique ID for the SE Utilisation du Système d'Information Services Grille Matchmaking Le SI est interrogé par...
  • Evan Williams Specialist Education Advisor Who we are

    Evan Williams Specialist Education Advisor Who we are

    Our Priorities. English and Maths. Leadership, Governance and Management. Vocational Education and Training. Our top priorities in our first year are helping the sector improve the quality of teaching, especially in
  • Collaborative Management Environment Transition Business Case

    Collaborative Management Environment Transition Business Case

    Simulation techniques are thorough, but can take hours to run Mathematical methods can be very fast, but make broad assumptions Both require extensive training and validation Agents provide the capability to: Bridge the gap between simulation and Mathematical methods Easy...
  • Troop Leadership Training (TLT) - Mercer Area District Home Page

    Troop Leadership Training (TLT) - Mercer Area District Home Page

    Introduction to Leadership Skills for Troops (ILST) David Mitchko Mercer Area District Commissioner What happened to JLT and TLT ? Troop Leadership Training (TLT#34306) replaced the Scoutmaster Junior Leader Training Kit, which focused on general leadership skills, team building and...
  • Unit 3: Human and Phys Systems Part A-

    Unit 3: Human and Phys Systems Part A-

    Geographic features and human criteria are a couple of ways to define a region. Different kinds of geographic features and ways to define a region are explained in more detail on the slides to follow. ... Van Gogh was a...
  • Warm Up Write about your sense of place

    Warm Up Write about your sense of place

    Functional Region. the product of interactions and movements of various kinds. The 3 Types of Regions. Formal Region (Uniform) Homogenous in physical, political or cultural characteristics. Perceptual Region. reflects feelings and images rather than objective data - primarily in the...
  • General Capabilities

    General Capabilities

    Personal and Social Capability. There are four inter-related organising elements. Self-Awareness - students recognise, understand and label their own emotions, values, strengths and capacities.. Self-Management - students effectively manage and regulate their own emotions and behaviours and persisting in completing...