CMSC 104, Version 8/061L14AssignmentOps.ppt Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips Reading Section.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

Programming and Data Structure
Introduction to the Omega Server CSE Overview Intro to Omega Basic Unix Command Files Directories Printing C and C++ compilers GNU Debugger.
Gnu Debugger (GDB) Topics Overview Quick Reference Card Readings: Quick Reference Card February 7, 2012 CSCE 212Honors Computer Organization.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
1 Shortcuts for Lazy Programmers! Topics Increment and Decrement Operators Assignment Operators.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Basic Elements of C++ Chapter 2.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
CSC 107 – Programming For Science. The Week’s Goal.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
National Chung Cheng University,Taiwan,R.O.C eCos demo using x86 PCs I-Hung Lin Date:2004/4/29.
1 SEEM3460 Tutorial Compiling and Debugging C programs.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class.
Debugging 1/6/2016. Debugging 1/6/2016 Debugging  Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a program.
Minimal standard C program int main(void) { return 0 ; }
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Об отладке программ МО ВВС ИВМ и МГ СО РАН Городничев Максим Александрович.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
The ++ and -- expressions. The ++ and -- operators You guessed it: The ++ and -- are operators that return a value.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CSE 1320 Basics Dr. Sajib Datta
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
CSE 332: C++ expressions Expressions: Operators and Operands Operators obey arity, associativity, and precedence int result = 2 * 3 + 5; // assigns 11.
Windows Programming Lecture 03. Pointers and Arrays.
Gnu Debugger (GDB) Topics Overview Quick Reference Card Readings: Quick Reference Card February 4, 2010 CSCE 212Honors Computer Organization.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
UMBC CMSC 104 – Section 01, Fall 2016
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 2 Assignment and Interactive Input
Objectives Identify the built-in data types in C++
CSCE 212Honors Computer Organization
Data Conversion & Scanner Class
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
Lecture 3 Expressions Richard Gesick.
Introduction to C Topics Compilation Using the gcc Compiler
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
Assignment Operators Topics Increment and Decrement Operators
Homework Reading Programming Assignments Finish K&R Chapter 1
Data Types and Expressions
Introduction to C Topics Compilation Using the gcc Compiler
More Loops Topics Relational Operators Logical Operators for Loops.
Assignment Operators Topics Increment and Decrement Operators
Variables in C Topics Naming Variables Declaring Variables
Assignment Operators Topics Increment and Decrement Operators
Introduction to C Programming
Chapter 15 Debugging.
CSCE 212Honors Computer Organization
Debugging.
Assignment Operators Topics Increment and Decrement Operators
Functions, Part 2 of 42 Topics Functions That Return a Value
Assignment Operators Topics Increment and Decrement Operators
Data Types and Expressions
Assignment Operators Topics Increment and Decrement Operators
Data Types and Expressions
Chapter 15 Debugging.
Presentation transcript:

CMSC 104, Version 8/061L14AssignmentOps.ppt Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips Reading Section 5.3

CMSC 104, Version 8/062L14AssignmentOps.ppt Increment and Decrement Operators The increment operator ++ The decrement operator -- Precedence: lower than (), but higher than * / and % Associativity: right to left Increment and decrement operators can only be applied to variables, not to constants or expressions

CMSC 104, Version 8/063L14AssignmentOps.ppt Increment Operator If we want to add one to a variable, we can say: count = count + 1 ; Programs often contain statements that increment variables, so to save on typing, C provides these shortcuts: count++ ; OR ++count ; Both do the same thing. They change the value of count by adding one to it.

CMSC 104, Version 8/064L14AssignmentOps.ppt Post increment Operator The position of the ++ determines when the value is incremented. If the ++ is after the variable, then the incrementing is done last (a post increment). int amount, count ; count = 3 ; amount = 2 * count++ ; amount gets the value of 2 * 3, which is 6, and then 1 gets added to count. So, after executing the last line, amount is 6 and count is 4.

CMSC 104, Version 8/065L14AssignmentOps.ppt Preincrement Operator If the ++ is before the variable, then the incrementing is done first (a preincrement). int amount, count ; count = 3 ; amount = 2 * ++count ; 1 gets added to count first, then amount gets the value of 2 * 4, which is 8. So, after executing the last line, amount is 8 and count is 4.

CMSC 104, Version 8/066L14AssignmentOps.ppt Code Example Using ++ #include int main ( void ) { int i = 1 ; /* count from 1 to 10 */ while ( i < 11 ) { printf (“%d ”, i) ; i++ ; /* same as ++i */ } return 0 ; }

CMSC 104, Version 8/067L14AssignmentOps.ppt Decrement Operator If we want to subtract one from a variable, we can say: count = count - 1 ; Programs often contain statements that decrement variables, so to save on typing, C provides these shortcuts: count-- ; OR --count ; Both do the same thing. They change the value of count by subtracting one from it.

CMSC 104, Version 8/068L14AssignmentOps.ppt Post decrement Operator The position of the -- determines when the value is decremented. If the -- is after the variable, then the decrementing is done last (a post decrement). int amount, count ; count = 3 ; amount = 2 * count-- ; amount gets the value of 2 * 3, which is 6, and then 1 gets subtracted from count. So, after executing the last line, amount is 6 and count is 2.

CMSC 104, Version 8/069L14AssignmentOps.ppt Predecrement Operator If the -- is before the variable, then the decrementing is done first (a predecrement). int amount, count ; count = 3 ; amount = 2 * --count ; 1 gets subtracted from count first, then amount gets the value of 2 * 2, which is 4. So, after executing the last line, amount is 4 and count is 2.

CMSC 104, Version 8/0610L14AssignmentOps.ppt A Hand Trace Example int answer, value = 4 ; Code Value Answer 4garbage value = value + 1 ; value++ ; ++value ; answer = 2 * value++ ; answer = ++value / 2 ; value-- ; --value ; answer = --value * 2 ; answer = value-- / 3 ;

CMSC 104, Version 8/0611L14AssignmentOps.ppt Practice Given int a = 1, b = 2, c = 3 ; What is the value of this expression? ++a * b - c-- What are the new values of a, b, and c?

CMSC 104, Version 8/0612L14AssignmentOps.ppt More Practice Given int a = 1, b = 2, c = 3, d = 4 ; What is the value of this expression? ++b / c + a * d++ What are the new values of a, b, c, and d?

CMSC 104, Version 8/0613L14AssignmentOps.ppt Assignment Operators = +=-= *= /=%= Statement Equivalent Statement a = a + 2 ;a += 2 ; a = a - 3 ;a -= 3 ; a = a * 2 ;a *= 2 ; a = a / 4 ; a /= 4 ; a = a % 2 ;a %= 2 ; b = b + ( c + 2 ) ;b += c + 2 ; d = d * ( e - 5 ) ;d *= e - 5 ;

CMSC 104, Version 8/0614L14AssignmentOps.ppt Precedence Operator Associativity ( ) left to right ! & right to left * / % left to right + - left to right >= left to right == != left to right && left to right || left to right = += -= *= /= %= right to left

CMSC 104, Version 8/0615L14AssignmentOps.ppt Good Programming Practices Always use parenthesis when you have more than two operators!

CMSC 104, Version 8/0616L14AssignmentOps.ppt Practice with Assignment Operators int i = 1, j = 2, k = 3, m = 4 ; Expression Value i += j + k j *= k = m + 5 k -= m /= j * 2

CMSC 104, Version 8/0617L14AssignmentOps.ppt Code Example Using /= and ++ Counting the Digits in an Integer #include int main ( void ) { int num, temp, digits = 0 ; temp = num = 4327 ; while ( temp > 0 ) { printf (“%d\n”, temp) ; temp /= 10 ; digits++ ; } printf (“There are %d digits in %d.\n”, digits, num) ; return 0 ; }

CMSC 104, Version 8/0618L14AssignmentOps.ppt Debugging Tips Trace your code by hand (a hand trace), keeping track of the value of each variable. Insert temporary printf() statements so you can see what your program is doing. o Confirm that the correct value(s) has been read in. o Check the results of arithmetic computations immediately after they are performed.

CMSC 104, Version 8/0619L14AssignmentOps.ppt gdb gdb is the Gnu source code debugger. You must compile your program with: o gcc –Wall –ansi –g myprog.c Run the debugger: o gdb a.out

CMSC 104, Version 8/0620L14AssignmentOps.ppt myprog.c #include int main( void ) { int a; float b; a = 7; a = a * 2; a = a - 3; b = 3.1; b = b * 2; b = b + 3.1; printf( "a = %d b = %.2f\n", a, b ); return 0; }

CMSC 104, Version 8/0621L14AssignmentOps.ppt gdb ~/gdb]$ gcc -g -Wall -ansi myprog.c ~/gdb]$ gdb a.out GNU gdb Red Hat Linux (6.1post rh) Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1". (gdb)

CMSC 104, Version 8/0622L14AssignmentOps.ppt gdb Commands break – set a breakpoint o break main run – execute the program in gdb o set the breakpoint before using this command step – execute until another line is reached x – examine memory at the name of a variable o x/1wd &a -- for int a o x/1wf &b -- for float b

CMSC 104, Version 8/0623L14AssignmentOps.ppt Example (gdb) break *main Breakpoint 1 at 0x : file myprog.c, line 4. (gdb) run Starting program: /afs/umbc.edu/users/b/u/burt/home/gdb/a.out Breakpoint 1, main () at myprog.c:4 4 { (gdb) s 7 a = 7; (gdb) s 8 a = a * 2; (gdb) x/1wd &a 0xbffffc14: 7

CMSC 104, Version 8/0624L14AssignmentOps.ppt Example – cont’d 10 b = 3.1; (gdb) s 11 b = b * 2; (gdb) x/1wf &b 0xbffffc10: (gdb) Notice that the computer stored 3.1 with a minor error! This is a problem inherent when using floating point numbers.

CMSC 104, Version 8/0625L14AssignmentOps.ppt gdb handout See the gdb handout on the course website for complete list of gdb commands.