Starting Chapter 2 K&R. Read ahead.. Call by Value vs Call by Reference Simple variables passed as function parameters in C are actually only copies on.

Slides:



Advertisements
Similar presentations
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Advertisements

Homework Starting Chapter 2 K&R. Read ahead. Questions?
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Assembly Language and Computer Architecture Using C++ and Java
Assembly Language and Computer Architecture Using C++ and Java
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
Data types and variables
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Chapter 2 Data Types, Declarations, and Displays
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
String Escape Sequences
Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 –HW3 is posted Questions?
Objectives You should be able to describe: Data Types
CPS120: Introduction to Computer Science Lecture 8.
Types, Operators and Expressions CSE 2031 Fall /5/2015 3:59 PM.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
1 Homework / Exam Finish up K&R Chapters 3 & 4 Starting K&R Chapter 5 Next Class HW4 due next class Go over HW3 solutions.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
 All C programs are made up of functions that perform operations on variables.  In this lecture we examine variables  Variables are the basic building.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 12 Variables and Operators
Chap. 2. Types, Operators, and Expressions
Tokens in C Keywords Identifiers Constants
Object Oriented Programming
Multiple variables can be created in one declaration
What to bring: iCard, pens/pencils (They provide the scratch paper)
Chapter 12 Variables and Operators
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
CS 240 – Lecture 5 Scope of Variables, The Stack, Automatic Variables, Global Variables, Constant Type.
Lectures on Numerical Methods
Homework Homework Continue Reading K&R Chapter 2 Questions?
Your questions from last session
Expressions and Assignment
Chapter 3 Operators and Expressions
Homework Starting Chapter 2 K&R. Read ahead. Questions?
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Homework Homework Questions? Continue Reading K&R Chapter 2
Chapter 12 Variables and Operators
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

Starting Chapter 2 K&R. Read ahead.

Call by Value vs Call by Reference Simple variables passed as function parameters in C are actually only copies on the stack. (Note: Stack pointer is a register) foo(i, j); void foo(int i, int j) { } Stack Pointer Before call and after return Unused Stack Pointer After call and before return Unusedij Return Data Stack Data Stack Data Copy of Value

This is known as Call by Value. Unlike some languages, which have call by reference You can't change arguments in original location within the function -- just change the stack copy To make changes, you must pass pointers to original variables. See next slide. You’re not responsible for pointers yet, but start getting used to them! Call by Value vs Call by Reference

Pointers as Arguments Pointer variables passed as function parameters in C are still only value on the stack but can be used to access original location indirectly foo(&i, &j); void foo(int *i, int *j) { } Stack Pointer Before call and after return Unused Stack Pointer After call and before return Unused &i&j Return Data Stack Data Stack Data Point to values

Pointers as Arguments The following doesn’t work!!! void exchgint (int a, int b) { int dummy; dummy = a; a = b; b = dummy; }

Pointers as Arguments Must be done with pointers!!! void exchgint (int *pa, int *pb) { int dummy; dummy = *pa; *pa = *pb; *pb = dummy; }

Pointers as Arguments An array name is automatically passed as a pointer You don't need to create a pointer yourself with the “address of” operator (&) int array1[10], array2[10]; foo(array1, array2); Unless you are passing a pointer to a specific array element (Remember printf in visitype?) foo(&array1[4*i], &array2[4*j]);

Scope of Variables – Local Automatic Each local variable of a function (inside { }) comes into existence only when it is called disappears when a return is performed. Local variables are said to be automatic. Memory is allocated on the stack after the calling sequence argument values Undefined (i.e. garbage) value unless explicitly initialized in the source code Initialization is done each time the function or block is entered (K&R P.85). Stack Pointer After local variables are allocated Stack Data Unusedij Return Data Local Variables

Scope of Variables – Local Static A local alternative to automatic is known as static. A static variable declared in a function is preserved in memory. Still local - can only be used inside { }. It is guaranteed to be initialized to zero if it is not initialized otherwise. If the static variable is initialized, it is done once before the program starts execution (K&R P.85). e.g., the seed of a random number generator so it will have memory from one invocation to the next and not always give the same random number. int rand( ){ static int seed = 1; /* initialize to 1 in the beginning and remember value between calls to rand */ }

Scope of Variables – Local Static Good for implementing a “Finite State Machine” –They allow a function to preserve its state value(s) But, be careful using local static variables!! –A function may behave differently when it is called with different values preserved in local static variables –Makes testing of a function more complex because you need to test its behavior with all possible values of any local static variables –Complex sequence of calls to function may be required to drive local static variables into desired test “state(s)”

Scope of Variables - External Alternative to local is known as external Look at maxline program on page 32 of K&R Variables declared ahead of/outside of all { }’s are available to all functions within the program load These are sometimes called “global variables” Can be used to pass data between functions Values are preserved like static local variables It is guaranteed to be initialized to zero If the external variable is initialized, it is done once before the program starts execution. Not a good idea to use them – Why not?

Scope of Variables - External Global variables are not a good idea because: –They can be accessed from anywhere. If their value gets corrupted, it is NOT easy to figure out what code in which function caused the corruption. –They make the functions dependent on their external environment instead of being able to stand alone using arguments to get their input values and a return value to pass back an output value. Software architecture/design standards for most projects will prohibit use of “global variables” or severely restrict their use. Don’t use them!!

Scope of Variables – External Static Another alternative known as external static No good examples that I see in K&R Static variables declared ahead of/outside of all { }’s are available to all functions within this file only These are very similar to OOP “class variables” Can be used to pass data between functions in file only Values are preserved like static local variables It is guaranteed to be initialized to zero If the external static variable is initialized, it is done once before the program starts execution. These are more acceptable than global variables

Examples of Scopes of Variables These examples are from p.342: “C Programming for Scientists and Engineers with Applications” by Rama Reddy and Carol Ziegler, Jones and Bartlett 2010.

Scope of Variables – Example #1 #include int main(){ int a=10; printf(“a=%d\n”, a); { int b=5; printf(“b=%d\n”, b); { int c=40; printf(“c=%d\n”,c); } return 0; } Scope of c Scope of b Scope of a

Scope of Variables – Example #2 #include int main(){ int x=10; printf(“x=%d\n”, x); { int x=5; printf(“x=%d\n”, x); { int x=40; printf(“x=%d\n”,x); } x=x+4; printf(“x=%d\n”,x); } x=x+15; printf(“x=%d\n”,x); return 0; } x=40 x=5+4x=10+15 If the same variable is defined inside and outside the block, the name inside the block will be referenced if the block is being executed

Scope of Variables – Example #3 #include void func1(void); void func2(void); void func3(void); int main(){ int x=20; printf(“x=%d\n”, x); func1(); x=x+10; printf(“x=%d\n”, x); func2(); x=x+40; printf(“x=%d\n”,x); func3(); return 0; } int x; void func1(void){ x=5; printf(“In func1 x=%d\n”,x); return; } void func2(void){ int x=0; printf(“In func2 x=%d\n”, x); return; } void func3(void){ printf(“In func3 x=%d\n”, x); return ; } Scope of 1 st x Scope of 2 nd x Scope of 3 rd x

Scope of Variables – Example #4 #include void func1(void); void func2(void); int main(){ int x=5; printf(“x=%d\n”, x); func1(); x=x+5; printf(“x=%d\n”, x); func2(); x=x+5; printf(“x=%d\n”,x); return 0; } int x; void func1(void){ x=6; printf(“In func1 x=%d\n”,x); } void func2(void){ x=x+10; printf(“In func2 x=%d\n”, x); } Scope of x External storage class definition

Scope of Variables – Example #5 #include void func1(void); void func2(void); int main(){ extern int x; x=1; printf(“x=%d\n”, x); func1(); x=x+6; printf(“x=%d\n”, x); func2(); x=x+7; printf(“x=%d\n”,x); return 0; } int x; void func1(void){ printf(“In func1 x=%d\n”,x); x=5; } void func2(void){ x=x+10; printf(“In func2 x=%d\n”, x); } Scope of x File 2 File

Scope of Variables – Example #6 #include void func1(void); void func2(void); int main(){ extern int x; x=1; printf(“x=%d\n”, x); func1(); x=x+6; printf(“x=%d\n”, x); func2(); x=x+7; printf(“x=%d\n”,x); return 0; } int x; void func1(void){ x=5; printf(“In func1 x=%d\n”,x); } void func2(void){ int x=10; printf(“In func2 x=%d\n”, x); } Scope of x File 1 File

Data Types Finished K&R Chapter 1 / Now starting Chapter 2 Variable Names Data Types: –char –int –short int –long int –float –double

Variable / Symbolic Constant Names Rules for generating names: –All letters and digits are allowed –Uppercase and lower case letters are different –First character must be a letter –Underscore _ counts as a letter (useful for improving readability) –But, don’t begin variable names with _ (reserved for library routines)

Variable / Symbolic Constant Names (cont’d) Rules for generating names: –Internal - at least 31 characters are significant –External – e.g. function names: only count on 6 characters and a single case –Avoid using C keywords (if, else, int, float, etc.) –Choose variable names that are related to the purpose or use of the variable

Data Types char (character) char has 8 bits (stored in one byte in memory) unsigned: 0 ≤ char ≤ ≤ char ≤ Overflow at 255( = 0) Underflow at 0(0 – 1 = 255) signed (if supported in the implementation): -2 7 ≤ char ≤ ≤ char ≤ Overflow at 127 ( = -128) Underflow at –128 (-128 – 1 = 127)

Data Types int (integer on our machines) int has 32 bits (stored in four sequential bytes in memory) unsigned: 0 ≤ char ≤ x ≤ char ≤ 0xffffffff Overflow at ( = 0) Underflow at 0(0 – 1 = ) signed: ≤ char ≤ x ≤ char ≤ 0x7fffffff Overflow at ( = – ) Underflow at – ( – 1 = )

Data Types short int (short integer on our machines) short int has 16 bits (stored in two sequential bytes in memory) unsigned: 0 ≤ char ≤ x0000 ≤ char ≤ 0xffff Overflow at 65535( = 0) Underflow at 0(0 – 1 = 65535) signed: ≤ char ≤ x8000 ≤ char ≤ 0x7fff Overflow at ( = –32768) Underflow at –32768 ( – 1 = 32767)

Data Types long int (long integer on our machines – same as int) long int has 32 bits (stored in four sequential bytes in memory) unsigned: 0 ≤ char ≤ x ≤ char ≤ 0xffff ffff Overflow at ( = 0) Underflow at 0(0 – 1 = ) signed: ≤ char ≤ x ≤ char ≤ 0x7fff ffff Overflow at ( = – ) Underflow at – ( – 1 = )

Data Types float –32-bits (stored in four sequential bytes in memory) –based on the IEEE 754 floating point standard + 1.f x 2 e 18 bits23 bits signexponent efraction f

Data Types float, double –Both represent numbers with fractional parts –“double” is a “float” with more precision –Don't do much with float or double in this course –Implementation is machine dependent: for our machine: float is 4 bytes double is 8 bytes Without a co-processor to do floating point computations, it takes a lot of computation time to do them in software. –Not often used in real time, embedded systems. –A cost versus performance tradeoff!

Numbering Systems Binary (no symbol to represent binary numbers) –Learn binary numbers using this game Octal (Octal Constant is written 0dd…) OCTALBINARY OCTAL BINARY Note: Can’t write a decimal value with a leading 0 digit – will be interpreted as octal zero

Numbering Systems Hex (Hex Constant is written 0xdd…) HEXBIN.HEXBIN.HEXBIN.HEXBIN C D A1010 E B1011 F1111 NOTE: Memorize these translations DO NOT convert between binary and Hex or Octal by converting to decimal and back! Much harder!! Learn to group binary digits by 3 (for octal) or 4 (for hex) to convert

Examples of the Number System Decimal Octal Hex > > 0x1f > > 0x80

Numbering Systems char Data Type Constants ‘a’integer value in ASCII code for letter a ‘0’integer value in ASCII code for number 0 ‘\b’integer value in ASCII code for backspace ‘\ooo’octal value 000 – 377 (0 – 255 decimal) ‘\xhh’hex value 0x00 – 0xff (0 – 255 decimal) examples ‘a’ = 0x61 ‘0’ = 0x30 ‘\127’ = 0x57 ‘\x2b’ = 0x2b

Numbering Systems Other Data Type Constants 1234int 1234Llong int 1234ULunsigned long int 1234.double (because of decimal point) Ffloat (because of the suffix) 1234e-2double (because of exponent)

Converting Decimal to Binary Divide the decimal number successively by 2 and write down the remainder in binary form: e.g LSB (after divide by 2, remainder =1) 580 (after divide by 2, remainder =0) 291 (after divide by 2, remainder =1) 140 (after divide by 2, remainder =0) 71 (after divide by 2, remainder =1) 31 (after divide by 2, remainder =1) 11 (after divide by 2, remainder =1) 0 0 MSB Read UP and add any leading 0’s: odd even

Converting Decimal to Hex Method 1: –Convert the decimal number to binary and group the binary digits in groups of 4 e.g   Method 2 (shown in HW3): –Divide the decimal number successively by 16 and write down the remainder in hex form: LSB (after divide by 16, remainder =5) 7 7 MSB (after divide by 16, remainder =7) –Read UP and add any leading 0’s: 0x75

Converting Binary to Decimal Treat each bit position n that contains a one as adding 2 n to the value. Ignore zeros because they can’t add anything to the value. Bit 0LSB 1 1(= 2 0 ) Bit 10 0 Bit 21 4(= 2 2 ) Bit 31 8(= 2 3 ) Bit 40 0 Bit 5132(= 2 5 ) Bit 60 0 Bit 7MSB0 0 Total45

Converting Hex to Decimal Treat each digit n as adding 16 n to the value. Ignore zeros because they can’t add anything to the value. Digit 0LSB 0 0 Digit (= 2 * 16 1 ) Digit 2b 2816 (= 11 * 16 2 ) Digit 30 0 Digit 40 0 Digit (= 1 * 16 5 ) Digit 60 0 Digit 7MSB0 0 Total

Base for Integer Constants Designating the base for an integer constant If constant begins with either: 0x It is Hex with a-f as Hex digits 0XIt is Hex with A-F as Hex digits Otherwise, if constant begins with 0It is Octal Otherwise, it is decimal

Base for Character Constants Designating the base for a character constant If constant begins with either: ‘\x It is Hex with 0-9 and a-f as Hex digits ‘\XIt is Hex with 0-9 and A-F as Hex digits Otherwise, if constant begins with ‘\0It is Octal Otherwise, it is the ASCII code for a character ‘a’

Constants Character and Integer Constants are Signed! –Sign bit (MSB) not set ‘\x55’ equals an 8-bit quantity when casted to an int, it equals 0000 … x55 equals a 32-bit quantity 0000 … –Sign bit (MSB) set ‘\xaa’ equals an 8-bit quantity when casted to an int, it equals 1111 … xaa equals a 32 bit quantity 0000 … –Note: None of the ASCII codes (x00 - x7f) have sign bit set!

Signed (Default) Behavior Careful mixing modes when initializing variables! int i; (signed behavior is default) char c;(signed behavior is default) i = 0xaa;(== aa) as intended i = ‘\xaa’;(== ffff ffaa) sign extends! c = ‘\xaa’;(== aa) as intended i = c; (==ffff ffaa) sign extends! char int Sign Bit Extension

Unsigned Behavior Careful mixing modes when initializing variables! unsigned int i; (must specify unsigned if wanted) unsigned char c;(must specify unsigned if wanted) i = 0xaa;(== aa) as intended i = ‘\xaa’;(== ffffffaa) char sign extends! c = ‘\xaa’;(== aa) as intended i = c; (== aa) char sign not extended! char int Sign Bit Extension

Example to illustrate signed/unsigned types void copy_characters(void){ char ch; while ((ch =getchar()) != EOF) putchar(ch); } If getchar() == EOF, is this true? What happens if ch is defined as unsigned char? Is this true? Changing the declaration of ch into int ch; makes it work. yes No

One’s Complement Two’s Complement One’s Complement Character Arithmetic ~ is the one’s complement operator ~ flips the value of each bit in the data All zeroes become one, All ones become zero ~ ‘\xaa’ == ‘\x55’ ~ == Number anded with its one’s complement = &

One’s Complement Two’s Complement Two’s Complement Character Arithmetic - is the two’s complement operator -flips the value of each bit in the data and adds 1 It creates the negative of the data value - ‘\x55’ == ‘\xab’ == Number added to its two’s complement = (1) (carry out of MSB is dropped)

Two Special Case Values char 0 (or zero of any length) = = char -2 7 (or -2 n-1 for any length = n) = =

Bit Manipulation Bitwise Operators: ~ one’s complement (unary not) & and | or ^ xor (exclusive or) << left shift >> right shift Lots of possible Exam questions here!

Binary Logic Tables AND NOT OR ADD Carry XOR Operands Results

Bit Manipulation unsigned char n = ‘\xa6’; n ~n (1s complement: flip bits) n | ‘\x65’ turn on bit in result if | on in either operand

Bit Manipulations n & ‘\x65’ turn on bit in result if | on in both operands n ^ ‘\x65’ turn on bit in result if | on in exactly 1 operand

Bit Manipulations n = ‘\x18’; (Remember n is unsigned) n << shift 1 to left (like times 2) n << shift 2 to left (like times 4) n << shift 4 to left (bits disappear off left end) n >> shift 2 to right (like / 4) n >> shift 4 to right (bits disappear off right end) Unsigned Right Shift 0

Bit Manipulations Right shift result may be different if n is signed! –If value of n has sign bit = 0, works same as last slide –If value of n has sign bit = 1, works differently!! char n = ‘\xa5’; (default is signed) n (sign bit is set) n >> (bring in 1’s from left) Bringing in extra copies of the sign bit on the left is also called "sign extension" Signed Right Shift

Bit Manipulations For signed variable, negative value shifted right by 2 or 4 is still a negative value ‘\xa5’ = ‘\xa5’ >> 2 = = ‘\xe9’ Same result as divide by 4 (2 2 = 4) But, this is not true on all machines See K&R pg. 49, lines 8-10.

Bit Manipulations When dividing a negative value –Different rounding rules than for positive value –Remainder must be negative - not positive Note that (-1)/2 = -1 Note that -(1/2) = 0

Forcing Groups of Bits Off Given char n, how to turn off all bits except the least significant 5 bits: n = n & ‘\x1f’ n = ‘\xa5’ n & ‘\x1f’ & turn off all bits except bottom 5 Called "masking" the bits -- only see bits on in result where 1's found in mask value

Forcing Groups of Bits Off x = x & ~077 (octal for a change) Sets least significant 6 bits in x to 0 Even if you don't know size of x (e.g. size of int) ~077 = ~ = of required size Extends itself with 1 bits on left for length of x

Forcing Groups of Bits On Given n, how to turn on the MS two bits (if already on, leave on). n = n | ‘\xc0’ n = '\xa5' n | '\xc0': | turn on MS 2 bits

“Encryption” with Exclusive Or Show that x ^ (x ^ y) == y char y =‘\xa5’ (plain text bits) char x =‘\x69’ (encryption key) x ^ y (cipher text bits) x ^ (x ^ y) (decrypted bits) Same as original value of y!

String Constants String constant: “I am a string.” –An array (a pointer to a string) of char values somewhere ending with NUL = '\0', the char with zero value. –"0" is not same as '0'. The value "0" can't be used in an expression - only in arguments to functions like printf(). Also have string functions: See pg. 241, Appendix B and B3, pg. 249 #include With these definitions, can use: len = strlen(msg); where msg is string in a string array NOTE: Write your own string functions for homework!!

Enumeration Symbolic Constants enum boolean {FALSE, TRUE}; –Enumerated names assigned values starting from 0 FALSE = 0 TRUE = 1 Now can declare a variable of type enum boolean: enum boolean x; x = FALSE; Just a shorthand for creating symbolic constants instead of with #define statements Storage requirement is the same as int

Enumeration Symbolic Constants If you define months as enum type enum months {ERR, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; Debugger might print value 2 as FEB

Code Example of the enum type int main() { enum month {ERR, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; enum month this_month; this_month = FEB; … }

const "const" declaration is like "final" in Java –warns compiler that variable value shouldn't change const char msg[ ] = "Warning:..."; –Commonly used for function arguments int copy(char to[ ], const char from[ ]); If logic of the copy function attempts to modify the “from” string, compiler will give a warning Exact form of warning and actual behavior of the code is installation defined

Operators Arithmetic Operators: +Add -Subtract *Multiply /Divide % Modulo (Remainder after division ) e.g. 5%7 = 5 7%7 = 0 10%7 = 3 Examples: int x, y; x / y truncates (no fractional part) x % y is the remainder when x is divided by y. Always true that: x == y*(x/y) + x%y

Operators Logical Operators: &&logical and ||logical or ! Not Example: … int year; if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) printf( "%d is a leap year\n", year); else printf( "%d is not a leap year\n", year); Why are no inner parentheses needed? See precedence table, P 53. Good questions for exams!!

Precedence and Associativity of Operators ( ) [ ] ->.Left to right ! ~ * & (type) sizeofright to left * / %left to right + -left to right > left to right >=left to right = = !=left to right &left to right ^left to right |left to right &&left to right ||left to right ?:right to left = += -= *= /= %= &= ^= |= >=right to left,left to right Precedence signdereference addresscasting Associativity

Precedence and Associativity Precedence –Operators in the same row have the same precedence –Operators are in order of decreasing precedence Associativity –Determine the order if the operators have the same precedence –e.g x = y +=z -= 4 means x = y +=(z -= 4) x =(y +=(z -= 4))

Relations / Comparisons We call a comparison between two arithmetic expressions a "relation" ae1 =, > ) A relation is evaluated as true or false (1 or 0) based on values of given arithmetic expressions if ( i < lim-1 = = j < k) –What's it mean? –See precedence table P 53 Instead of c != EOF, could write !(c = = EOF)