DOC PREVIEW
MIT 16 070 - Variables and Operators

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Fesq, 2/20/01 1 16.070Variables and Operators2/20/01 Lecture #6 16.070• Variables, their characteristics and their uses• Operators, their characteristics and their usesFesq, 2/20/01 2 16.070Variables• Variables enable you to associate a name with a memory location• Variables hold values upon which a program acts• Variables have 3 characteristics! Type indicates properties of values contained in variable-- int, char, double! Identifier -- symbolic, "descriptive" name− Allows programmer to refer to values symbolically, by name vs by storagelocation− Most high-level languages have rules for choosing variable names− Letters (upper vs lower case), numbers, special characters (e.g., "_")! Scope - implicitly indicates to compiler where variable is accessible withinprogramFesq, 2/20/01 3 16.070Variable Declaration• All variables (in C) must be defined prior to use! Format:<type> <identifier>;! Examples:int thrust_command; /*expected range: >0 */declares variable called thrust_commandthat will contain an integer valuefloat meters_per_second; /*expected range: -3.2e4 to 3.2e4 */declares variable calledmeters_per_secondthat will contain a float value• Compiler reserves memory locations for variables at compile time• Operating system creates memory locations for each variable andensures that values are appropriately stored in themFesq, 2/20/01 4 16.070Variable Initialization• Set up initial value of variable• Without initialization, memory location contains garbage valueint thrust_command; 100 01100010 • Compile-time initialization! Included in variable declaration! At run time, OS assigns reserved location to variable and places definedvalue in memory locationint thrust_command = 0; 100 00000000 • Run-time initializationint thrust_command; 100 01010010 thrust_command =0; 100 00000000Fesq, 2/20/01 5 16.070Constant Variables• Constants are variables whose values do not and can not change duringexecution of program• Value specified during variable declaration - Compile-timeinitialization! Defined by the const qualifier before the type specifierconst double PI = 3.14159;…area = PI * radius * radius;circumference = 2 * PI * radius;• Why use constants?! Readability! Maintenance - easier to changeFesq, 2/20/01 6 16.070Variable Scope• Region of program where variable is accessible• Specified implicitly by the place in the code where declaration occurs• Global variables are known to several functions! Declared at front of file, outside of functions, outside of all blocks {}! Visible to, and usable by, all functions! Can be accessed and modified from any block within the program! Retain storage throughout duration of program• Local variables are known only to the functions containing them! Declared at the beginning of a block {}! Accessible only within the block in which they are declared! Lose storage and value after block execution completesFesq, 2/20/01 7 16.070Global and Local Variables• Pros and cons for using global vs local variablesPro ConGlobal Variables• Can produce faster code • Increase program'scomplexity• Difficult to maintain - lookin every source file to see ifvariable is used• Potential conflict betweenmodules• Undermines modularityLocal Variables• Interfaces between modulesclearly defined• In some languages (e.g.,Ada), variable use specified- input/output/both• Parameter lists can be long• Rule of thumb: use global variables only when timing warrants itFesq, 2/20/01 8 16.070Global and Local Variables• char user_input; /* global variable */int main(void) void move_robot(void){{… … /* move robot in direction */user_input = print_menu(void); … /* specified by passed */move_robot (user_input); … /* argument */……}}Fesq, 2/20/01 9 16.070A View of Memory• Memory organization when a program is runningX000 …Memory-mapped I/O and DMA___________________________________________________Program Areacontains executable code of program- OS- applications software- constants___________________________________________________Global Data Section___________________________________________________Run-Time StackFor context saving and automatic variables___________________________________________________…Fesq, 2/20/01 10 16.070Operators• Operators are tokens that result in computation or action when appliedto variables or other elements in an expression! Assignment! Arithmetic! Relational! Logical (Chapter 9)! Bitwise manipulation (listed in Appendix B)Fesq, 2/20/01 11 16.070Assignment Operator• Arithmetic Expression consists of operators and operands1) 8 2) 5 + x 3) steps = 10• Arithmetic Expressions do not always change values in memory! What effect will this expression have?(a + b) * c;! What effect will this expression have?thrust = (a + b) * c;! Assignment operator '=' does not mean "equals"!! '=' assigns the value that is calculated on RHS to variable on LHSi = i + 1;Fesq, 2/20/01 12 16.070Arithmetic Operators• C uses operators (~40) to represent arithmetic operations (seeAppendix B)• Operators act on operands and result in computation or action! Unary, requires one operand♦ + Plus, has no effect on result ♦ ++ Increment♦ - Minus, reverses the sign of the operand ♦ -- Decrement! Binary, requires two operands (integer or float)♦ + Addition, adds the two operands♦ - Subtraction, subtracts the second operand from the first♦ * Multiplication, multiplies the two operands♦ / Division, divides the first operand by the second operand♦ % Modulus, equals remainder of first operand divided by second. Operands mustbe integersFesq, 2/20/01 13 16.070Increment Operator (Analogous comments for Decrement)• Increment operator increments value of operand by 1counter++ " counter = counter + 1• Operand must be variable 10++ ??• Pre-increment/Prefix mode - increment operand and then use the resultcounter = 3;printf ("%d", ++counter); # 4printf ("%d", counter); # 4• Post-increment/Suffix mode - use operand and then increment operandcounter = 3;printf ("%d", counter++); # 3printf ("%d", counter); # 4Fesq, 2/20/01 14 16.070Side Effects of Increment and Decrement Operators• Increment/Decrement operators cause side effects: change the value ofa variable as well as result in a value• Not always possible to predict order in which side effects occur!


View Full Document

MIT 16 070 - Variables and Operators

Documents in this Course
optim

optim

20 pages

Load more
Download Variables and Operators
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Variables and Operators and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Variables and Operators 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?