1 / 23

C/C++ Operators

C/C++ Operators. Binary Operators: Operators Between Two Operands:. Operator. Meaning. Example. Definition. +. Addition. x = 6 + 2;. Add the values on either side of +. -. Subtraction. x = 6 - 2;. Subtract right value from left value. Multiplication. x = 6 * 2;.

hoyt-jarvis
Download Presentation

C/C++ Operators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C/C++ Operators Binary Operators: Operators Between Two Operands: Operator Meaning Example Definition. + Addition x = 6 + 2; Add the values on either side of + - Subtraction x = 6 - 2; Subtract right value from left value Multiplication x = 6 * 2; Subtract right hand side values * / Division x = 6/2; Divide left value by right value % Modulus x = 6 % 2; Remainder of left divided by right What about exponentiation??? Either x = 6 ^ 2 OR x = 6 ** 2 ??? No Such Thing: You must write your own function (Or use an available one) Dividing 2 real numbers yields EXACT results NOTE: Dividing 2 Integers yields TRUNCATED results Why??

  2. C/C++ Operators • Previously, we talked about how integers were stored. • For example, we said the declaration shorts1 = 56, s2 = 17; Asks for two 16-bit integer locations (s1 and s2) and stores the values 56 and 17 in them (respectively) Var/Locs1: 5610 Var/Locs2: 1710 We know that if we divide 56/17 (on a calculator) you would get the value 3.2941176470588235294117647058824 But, how do you store a mantissa as an integer?? (You don’t) 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 OK – But what if I wanted to get the real answer above?? All Integers can be rewritten (as floats (casting)) The opposite is NOT true Decimal to Binary Converter

  3. C/C++ Operators Consider (i.e., enter, build and run) the code below: #include"stdafx.h" #include<iostream> usingnamespacestd; voidmain() { ints1 = 56, s2 = 17; cout << s1 << " divided by " << s2 << " is " << s1/s2 << endl; cout << "Casting " << s1 << " and " << s2 << " and then dividing yields: " << (float)s1/(float)s2 << endl; } The output should appear as:

  4. C/C++ Operators There is one additional binary Operator we need to discuss in more detail: The Modulus operator Add the following line to your program, compile and run: cout << "The remainder of 9/2 is " << 9 % 2 << endl; How is this useful?? There are a number of uses. We are going to take a look at one example: Converting decimal values to binary

  5. C/C++ Operators There is one additional binary Operator we need to discuss in more detail: The Modulus operator Add the following line to your program, compile and run: cout << "The remainder of 9/2 is " << 9 % 2 << endl; How is this useful?? There are a number of uses. We are going to take a look at one example: Converting decimal values to binary

  6. Converting from decimal to binary Dividing any integer by 2 means that the remainder must be either 0 or 1 A few slides ago, we noted that = 1110002 5610 How can we verify this?? When you first learned division, you probably were taught to do it as follows: 28 14 7 1 0 3 2 goes into 3, 1 times. 1 times 3 is 3. Subtracting -3 from 7 leaves a remainder of 1 2 goes into 7, 3 times. 2 times 3 is 6. Subtracting -6 from 7 leaves a remainder of 1 2 goes into 14, 7 times. 2 times 7 is 14. Subtracting -14 from 14 leaves a remainder of 0 2 goes into 28, 14 times. 2 times 14 is 28. Subtracting -28 from 28 leaves a remainder of 0 2 goes into 56, 28 times. 2 times 28 is 56. Subtracting -56 from 56 leaves a remainder of 0 2 goes into 1, 0 times. 0 times 1 is 0. Subtracting 0 from 1 leaves a remainder of 1 2 2 2 2 2 2 1 56 3 28 7 14 -28 -6 -0 -2 -56 -14 0 1 0 0 1 1

  7. Converting from decimal to binary Let’s rewrite this procedure in a simpler form Pass Gathering the remainders from last to first: 2 0 remainder quotient 1 1 1 0 0 0 = 56 0 2 28 How do we do that?? 56 0 1 7 3 1 14 2 2 2 2 0 2 We’re going to have to go over your first abstract data type: An Array 1 3 1 4 1 5 0 Stop when the quotient becomes 0

  8. Arrays An array is an arrangement of similar basic data types Let’s create an array of short integers short binary[15], temp[15], decval = 56, reverse = 0, index = 0, i; Next, we’ll create the array as we did before while (decval > 0) { temp[index] = decval % 2; index++; decval = decval/2; } index--; Now we need to reverse the order of the array i = index; while (reverse <= index) { binary[reverse] = temp[i]; reverse++; i--; } The complete programming (including the printing of the result) is given on the next slide

  9. Arrays #include"stdafx.h" void main() { shortbinary[15], temp[15], decval = 177, reverse = 0, index = 0, i; while (decval > 0) { temp[index] = decval % 2; index++; decval= decval/2; } index--; for (i = 0; i <= index; i++) printf("%d",temp[i]); printf("\n\n"); i = index; while (reverse <= index) { binary[reverse] = temp[i]; printf("%d %d %d\n",i, reverse, binary[reverse]); reverse++; } i--; for (i = 0; i <= index; i++) printf("%d", binary[i]); printf("\n"); } As usual, you should enter the code, compile it, and run it

  10. = x = 3 Assignment Operators: Assigning Values to RAM: Operator Example Definition. = x = 6 + 2; Location x will get the value 6 + 2 ( = 8) += x += 3 Same as x = x + 3: IF x contained the value 4, it now contains the value 7 Same as x = x - 3: IF x contained the value 4, it now contains the value 1 *= x *= 3 Same as x = x * 3: IF x contained the value 4, it now contains the value 12 /= x /= 3 Same as x = x / 3: IF x contained the value 4: IF x is a float (real), it now contains 1.33 IF x is an integer, it now contains the value 1 %= x %= 3 Same as x = x % 3: IF x contained the value 4, it now contains the value 1 (3 goes into 4 Once with ONE left over)

  11. Unary Operators: Operators on a single Operand: Operator Meaning Example Definition. - Minus Sign x = -2; Assign a negative value + Plus Sign x = +2; Assign a Positive value ++ Increment x = ++y; Prefix Notation OR x = y++; Postfix Notation -- Decrement x = --y; Prefix Notation OR x = y--; Postfix Notation Prefix and Postfix Notation?? What’s that all About ??? Consider the following section of c code: int x,y,a,b; a = b = 1; // Initialize the variables (locations) with the value 1 x = 2 * ++a; // PREFIX Notation: First increment a by 1 (a = 2) // Then Multiply by 2 & assign to x (x=2*2) y = 2 * b++; // POSTFIX Notation: First multiply b by 2 & assign to y (y=1*2) // Then increment b by 1 (b = 2) printf("%d %d\n",x,y); // The Output would appear as:4 2

  12. There are two (2) Additional Unary Operators: sizeof Return the size of the operand (in bytes) Consider the following section of c code: int i = 12; float f = -123.45; printf("%d sizeof = %d\n",i,sizeof i); // would produce: 12 sizeof = 2 printf("%7.2fsizeof = %d\n",f,sizeof f); // would produce: -123.45 sizeof = 4 (type) Convert a value to the specified data type Consider the following section of c code: int a = 4; float b = 12.245; printf("%d %ld\n",a,(long) a); // would produce: 44 printf("%d %f\n",a,(float) a); // would produce: 4 4.000000 printf("%f %d\n",b,(int) b); } // would produce: 12.24500012

  13. Relational Operators: Operators on two Operands Yielding a T/F Answer: Operator Meaning Example Definition. < x < 2; False if x contains the value 2 Less Than x <= 2; True if x contains the value 2 <= Less Than OR Equal To = = x = = 2; True if x contains the value 2 Equal To ! = x ! = 2; False if x contains the value 2 Not Equal To > x > 2; False if x contains the value 2 Greater than >= x >= 2; True if x contains the value 2 Greater than OR Equal To False if x contains the value 2 x > 2 && x < 4 && And | | True if x contains the value 2 x = = 2 | | x = = 4 Or ! False if x contains the value 2 Not ! (x = = 2)

  14. Order of Operations: Order Operator 1 ( ) 2 + - ++ -- (unary) ! (Rel) 3 * / % 4 + - (binary) 5 < > <= >= 6 == != && 7 8 | | = 9 Given:int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8; What is the value of the statement: a = (a * ((b + c) % d) / e) - (f * g + 6) * h / 4 + 1;

  15. Given:int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8; - 96 + 1 = - 95 0 - 96 = - 96 384 / 4 = 96 1 / 5 = 0 48 * 8 = 384 1 * 1 = 1 5 % 4 = 1 42 + 6 = 48 2 + 3 = 5 6 * 7 = 42 a = (a * ((b + c) % d) / e) - (f * g + 6) * h / 4 + 1;

  16. Example 2: Given:int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8; Evaluate: ++a * b-- + c % d / e - f-- * g + 6 * h++ / b + h; 2 2 * 2 = 4 3 % 4 = 3 3 / 5 = 0 6 * 7 = 42 6 * 8 = 48 48 / 2 = 24 4 + 0 = 4 4 - 42 = - 38 - 38 + 24 = - 14 - 14 + 8 = - 6 Note that now: a = 2, b = 1, f = 5, h = 9

  17. Example 3: Given: int a = 1, b = 2, c = 3, d = 4; Consider the statement: a > b || b < c && c == 3 || d < 4 && b != c False True False True True False True True True

  18. Conversion specifiers for scanf and printf: Specifier(s) Output Example %c Single Character B %d %i Signed decimal Integer 457 %u Unsigned decimal Integer 7832 %ld %lu Signed/Unsigned long Integer -345 64 %f Signed floating-point, decimal notation -6.576 -4.5e3 2.1E-2 %e %E Signed floating-point, e or E notation -2.1 4.56E4 %g %G Use shorter or %f or %e (%f or %E) Long double floating-pt (also %LE) 7.32 -6.1e4 %Lf %Le %o Unsigned octal notation 4271 %x %X Unsigned hexadecimal notation 4d2a F6B %s Character string Hello %p Pointer (address) 4FF0: 8BC1 %% Print a % sign % Additional information about printf/scanfspecifiers can be found in the Supplementary Materials Link

  19. Additional Precompiler Directives User Defined constants: #define PI = 3.15149 User Defined constants: #define SQUARE (x) x * x // macro example.cpp : Defines the entry point for the console application. #include "stdafxh" #include <iostream> using namespace std; #define PI 3.15149 #define SQUARE(X) X*X void main() { float area, r = SQUARE(3.5); area = PI * r; cout << "The area is:" << area << endl; }

More Related