Computer Science

C Arithmetic Operations

C Arithmetic Operations are mathematical operations that can be performed on numerical data types in the C programming language. These operations include addition, subtraction, multiplication, division, and modulus. They are used to manipulate numerical data and perform calculations in C programs.

Written by Perlego with AI-assistance

6 Key excerpts on "C Arithmetic Operations"

  • Book cover image for: C++ Programming
    eBook - PDF

    C++ Programming

    Program Design Including Data Structures

    We will discuss assignment statements in detail later in this chapter. Arithmetic Operators, Operator Precedence, and Expressions One of the most important uses of a computer is its ability to calculate. You can use the standard arithmetic operators to manipulate integral and floating-point data types. There are five arithmetic operators: Arithmetic Operators: + (addition), -(subtraction or negation), * (multiplication), / (division), % (mod, (modulus or remainder)) These operators work as follows: ? You can use the operators + , -, * , and / with both integral and floating-point data types. These operators work with integral and floating-point data the same way as you learned in a college algebra course. ? When you use / with the integral data type, it gives the quotient in ordinary division. That is, integral division truncates any fractional part; there is no rounding. ? You use % with only the integral data type, to find the remainder in ordinary division. Example 2-3 shows how the operators / and % work with the integral data types. EXAMPLE 2-3 Arithmetic Expression Result Description 5 / 2 2 In the division 5 / 2 , the quotient is 2 and the remainder is 1 . Therefore, 5 / 2 with the integral operands evaluates to the quotient, which is 2. 14 / 7 2 In the division 14 / 7 , the quotient is 2 . 34 % 5 4 In the division 34 / 5 , the quotient is 6 and the remainder is 4 . Therefore, 34 % 5 evaluates to the remainder, which is 4 . 4 % 6 4 In the division 4 / 6 , the quotient is 0 and the remainder is 4 . Therefore, 4 % 6 evaluates to the remainder, which is 4 . In the following example, we illustrate how to use the operators / and % with integral data types. Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 44 | Chapter 2: Basic Elements of C++ EXAMPLE 2-4 Given length in inches, we write a program that determines and outputs the equivalent length in feet and (remaining) inches.
  • Book cover image for: Digital Arithmetic
    • Miloš D. Ercegovac, Tomás Lang, Miloš D. Ercegovac(Authors)
    • 2003(Publication Date)
    • Morgan Kaufmann
      (Publisher)
    CHAPTER I Review of Basic Number Representations and Arithmetic Algorithms In this chapter we briefly review basic number representations and algorithms used in digital arithmetic. The treatment is very concise; readers that need a more detailed review should consult some of the references listed at the end of the chapter. More advanced algorithms as well as the implementations are the topic of later chapters. 1.1 Digital Arithmetic and Arithmetic Units Digital arithmetic encompasses the study of number representations, algorithms for operations on numbers, implementations of arithmetic units in hardware, and their use in general-purpose and application-specific systems. An arithmetic unit (processor) is a system that performs operations on num- bers. We limit ourselves to the most common cases in which these numbers are 1. fixed-point numbers 9 integersI = {-N,..., N} 9 rational numbers of the form x = a/2f (binary rationals), a ~ I and f positive integer floating-point numbers x • b E, x rational number, b the integer base, and E integer exponent. The floating-point numbers approximate real numbers and facilitate computations over a wide dynamic range. Collectively, we refer to these numbers as DA (digital arithmetic) numbers. . 4 c HA P T E R I Review of Basic Number Representations and Arithmetic Algorithms An arithmetic processor operates on one, two, or more operands depending on the operation. The operands are characterized by a representation and a set of values as defined in the next section. The operation is selected from an allowable set, which usually includes addition, subtraction, multiplication, division, square root, change of sign, comparison, and so on. The results can be DA numbers, logical variables (conditions), and/or singularity conditions (exceptions). Logical results occur for operations such as comparison, check for zero, and the like.
  • Book cover image for: Microprocessor Engineering
    1 Arithmetic 1.1 Introduction A microprocessor-based computing system has to have a numerical computation capability, and consequently it must be able to implement the four basic arithmetic processes of addition, subtraction, multiplication and division. Human beings perform arithmetic operations using the decimal number system, but, by comparison, a microprocessor is inherently a binary machine and its numerical calculations are executed using a binary number system. Additionally, the hexadecimal system is used in program listings, so the engineer who proposes to use this kind of machine must be familiar with a variety of different number systems. It is also true that arithmetic processes executed by a digital machine are not by any means identical to the pencil and paper methods normally employed by man. For example, the process of subtraction in this kind of machine is carried out as an addition, and this involves the use of complement arithmetic. Again, a frequent requirement is that the output of a microprocessor should be a decimal display, for obvious reasons. Since the machine works in binary, a way has to be found to represent decimal numbers in terms of binary digits and this involves the use of binary coded decimal. Methods then have to be devised so that arithmetic operations in the machine can be performed using binary coded decimal, and techniques for converting from pure binary to binary coded decimal and vice versa are also required. The purpose of this chapter is to describe the various number systems in common usage in microprocessor systems and to develop methods for implementing the various arithmetic opera- tions that have to be performed. 1 2 Arithmetic 1.2 Number systems The number system most familiar to man is the decimal system.
  • Book cover image for: Algebra Essentials
    No longer available |Learn more
    C H A P T E R 2 ARITHMETIC OPERATIONS T his chapter reviews the mathematical operations of addition, subtraction, multiplication, and division of numbers and introduces exponentiation and root extraction. It is expected that most of the material here will be familiar to you. Nevertheless, arithmetic operations are presented in this chapter in such a way so that algebraic generalization can be easily derived from them and applied into algebraic equations. 2.1 SYMBOLICAL EXPRESSION The study of algebra begins by interpreting the meaning of its symbols. The use of symbols or letters denotes arbitrary numbers and frees us from the use of a long list of cases. For example, suppose that + = + = + = 1 0 1, 2 0 2, 1 2 0 1 2 ,..., Since numbers are infinite, it is an impossible task to list all cases. Instead, the use of an abstract system to represent the statement above saves space. Instead of listing all the possible cases for infinite numbers, you simply replace a number with an arbitrary symbol such as x. Where x is a number, then x + 0 = x, without knowing the exact value of the arbitrary number x. Normally the words increase, increment, and augment, are associated with addition. Thus, if x is an unknown number, the expression “a number increased by seven” is translated into symbols as x + 7. Later in this chapter, this expression can also be equivallenty writtened as 7 + x. Similarly, the words decrease, decrement, diminish, and difference are associated with subtrac- tion. Thus if x is an unknown number, the expression “a number decreased by seven” is translated into symbols as x − 7. Note that this expression is different than “seven decreased by a certain number” or the expression 7 − x, which is discussed later in this chapter. Multiplication is commonly associated with the word product. Thus if x is an unknown number, the expression “the product of a certain number and seven” is translated into symbols as 7x.
  • Book cover image for: Mathematics for Multimedia
    • Mladen Victor Wickerhauser(Author)
    • 2003(Publication Date)
    • Academic Press
      (Publisher)
    Chapter 1 Numbers and Arithmetic Processing, display, and communication of digital information, that is, information encoded as numbers, is accomplished by arithmetic with various kinds of numbers. Such computations are performed with algorithms, which are sequences of opera- tions with numbers such as addition, multiplication, and reading and writing digits. Only finite algorithms can be used: these are procedures in which • Every operation can be performed in a finite time; • The algorithm is guaranteed to stop after a finite number of operations. For an algorithm to be finite, its arithmetic operations can only be carried out to a finite degree of precision. In reality, a computer can keep only a small number of digits for each number because memory, processing and data communication are costly resources. But this usually poses no problems since the digital information of multimedia signals is itself of low precision. For example, a CD-quality digital sound recording consists of a sequence of numbers measuring the electrical output of a microphone at sequential times, with a precision of 5 decimal digits or less per measurement. Images from typical scanners are even less precise, consisting of arrays of numbers measuring light intensity to 3 decimal digits. Physical measure- ment is always imprecise, so these low precisions cannot be improved much. But the result is that computation for multimedia signal processing can be done with low precision arithmetic. Most computers distinguish between integers and floating-point numbers or floats, which are approximations to real numbers. Either class is suitable for rep- resenting finite-precision information, but floats are somewhat more convenient for computation because the memory needed to store one of them is independent of its magnitude. All computers have a fixed range of representable values for both integers and floats, and have efficient circuitry for arithmetic with numbers in those ranges.
  • Book cover image for: Computer Simulation in Physics and Engineering
    • Martin Oliver Steinhauser(Author)
    • 2012(Publication Date)
    • De Gruyter
      (Publisher)
    It is poor programming practice to mix types in arithmetic expressions. In other words, the two operands operated on by the addition, subtraction, multiplication, or division operators should both be either of type int or type double . The value of an expression can be converted to a different datatype by prefixing the name of the desired datatype, enclosed in parentheses. This type of construction is known as a cast . Thus, to convert an integer variable j into a floating point variable with the same value, one would write (double) j Finally, to avoid mixing datatypes when dividing a floating point variable f by an integer variable i , one would write f / (double) i Of course, the result of this operation would be of type double . The operators within C are grouped hierarchically according to their precedence (i.e., their order of eval-uation). Amongst the arithmetic operators, and = have precedence over C and . In other words, when evaluating expressions, C performs multiplication and division operations with higher priority than addition and subtraction operations. Of course, Section 2.2 First steps in C 105 the rules of precedence can always be bypassed by judicious use of parentheses. Thus, the expression a -b / c + d is equivalent to the unambiguous expression a -(b / c) + d since division takes precedence over addition and subtraction. The distinguishing feature of unary operators is that they only act on single ope-rands. The most common unary operator is the unary minus , which occurs when a numerical constant, variable, or expression is preceded by a minus sign. Note that the unary minus is distinctly different from the arithmetic operator ( ), which denotes subtraction, since the latter operator acts on two separate operands. The two other common unary operators are the increment operator , CC , and the decrement oper-ator , .
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.