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.

Slides:



Advertisements
Similar presentations
Introduction to C Systems Programming Concepts. Introduction to C A simple C Program A simple C Program –Variable Declarations –printf ( ) Compiling and.
Advertisements

Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Kernighan/Ritchie: Kelley/Pohl:
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.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
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.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Introduction to C Systems Programming. Systems Programming: Introduction to C 2 Systems Programming: 2 Introduction to C  A ‘C’ Program –Variable Declarations.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
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,
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
Introduction to C Language
Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 –HW3 is posted Questions?
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
1 Homework HW6 due class 22 K&R 6.6 K&R 5.7 – 5.9 (skipped earlier) Finishing up K&R Chapter 6.
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
Lecture12. Outline Binary representation of integer numbers Operations on bits –The Bitwise AND Operator –The Bitwise Inclusive-OR Operator –The Bitwise.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
Computer Programming Control Structure
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
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.
Comments, Conditional Statements Continued, and Loops Engineering 1D04, Teaching Session 4.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
The Cast Operator The cast operator converts explicitly from one data type of an expression to another. For example, if x is of type int, the value of.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
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.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
CprE 185: Intro to Problem Solving (using C)
Chapter 12 Variables and Operators
Chap. 2. Types, Operators, and Expressions
Week 3 - Friday CS222.
Multiple variables can be created in one declaration
Conversions of the type of the value of an expression
Unit 2 Programming.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
More about Numerical Computation
Homework Homework Continue Reading K&R Chapter 2 Questions?
Homework Any Questions?.
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Homework Starting K&R Chapter 5 Good tutorial on pointers
Assignment Operators Topics Increment and Decrement Operators
Homework Reading Programming Assignments Finish K&R Chapter 1
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Chap 7. Advanced Control Statements in Java
Assignment Operators Topics Increment and Decrement Operators
Review of Previous Lesson
Variables in C Topics Naming Variables Declaring Variables
Homework Homework Questions? Continue Reading K&R Chapter 2
Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token.
Presentation transcript:

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 Chapter 3 very quickly. Not a lot is new. Read K&R through Chapter 4.2. Questions?

2 Convert Upper Case to Lower int lower(int c) /* this is function tolower(int c) if #include (see K&R, App. B2) */ { if (c >= 'A' && c <= 'Z') return c – ‘A’ + ‘a’; /* c - 'A' is letter number... */ /*...where ‘A’ = 0, so c -'A' + 'a' is corresponding lower case letter */ else return c; }

3 Declarations and Initialization Section 2.4. int x, a[20]; /* external vars, initialized to zero */ int w = 37, v[3] = {1, 2, 3}; /* happens once */ int main (void) {... /* contains frequent calls to func */ } void func (void) /* entered 1000 times per second */ { int y, b[20]; /* automatic -- contains junk on entry */ int s = 37, t[3] = {1, 2, 3}; /* happens on each entry */ }

4 C language Characteristics C language never does a possibly significant amount of work without this being specified by the programmer Initialization is not done unless programmer codes it in a way that causes the initialization to be done

5 Casts K&R Sect 2.7 char c1, c2 = 15; int j = 2379; float g = 12.1; printf ("%d\n", c2 + j); /* what type, what printed ? */ ( int, = 2394 ) c1 = j; /* what type, value of c1? */ ( char, 2379 % 256 = 75 ) printf ("%d\n", c1 + c2); /* what type, value printed? */ ( char, 90 )

6 Casts K&R Sect 2.7 printf ("%d\n", (char) j + c2); /* value? */ ( 90 ) printf ("%9.1f\n", j + g); /* type, value? */ ( float, = ) printf ("%d\n", j + (int) g); /* type, value? */ ( int, = 2391 )

7 Increment / Decrement Operators Section 2.8. Both ++n and n++ have effect of n = n + 1; Both --n and n-- have effect of n = n – 1; With ++n or --n in an expression –n is incremented/decremented BEFORE being used With n++ or n-- in an expression –value is used THEN n is incremented/decremented int arr[4] = {1, 2, 3, 4}, n = 2; printf("%d\n", arr[n++]); /* values printed? */ printf("%d\n", arr[--n]); printf("%d\n", arr[++n]);

8 Increment / Decrement Operators Precedence, pg. 53, specifies the binding order, not the temporal order. Consider two statements: n = 5; m = n-- + 7; In second statement, binding is: m = ((n--) + 7); But value of n-- is evaluated last Uses value 5 for n in evaluation of the expression The value of n is not decremented to 4 until the ENTIRE EXPRESSION has been evaluated (in this case, the right side of assignment statement) Temporal order of execution is not specified by binding order of operators in precedence table

9 Increment / Decrement Operators In fact (experiment), hard to predict sometimes int n = 3; n = (n++)*(n++); /* bad practice */ Do we get 3*3+1+1 = 11? It might be different on different machines and compilers, so don't do this! When we did this with “gcc” last semester, we got 11! With “gcc” on our systems now, we get 9! printf ( "%d %d\n", n, n++); /* unclear what is printed */ Which expression is evaluated first? Second expression is evaluated first, so we get 10, 9!

10 Bit-wise Operators Section 2.9 We’ve already covered these, but they can be difficult when using them for first time Bit-wise Operators &bit-wise AND |bit-wise inclusive OR ^bit-wise exclusive OR ~one’s complement <<left shift >>right shift

11 Bit-wise Operators Masking is the term used for selectively setting some of the bits of a variable to zero & is used to turn off bits where “mask” bit is zero n = n & 0xff resets all except 8 LSBs to zero | is used to turn on bits where “mask” bit is one N = n | 0xff sets all LSBs to one

12 Bit-wise Operators Other tricks with bit-wise operators ^ can be used to zero any value n = n ^ n sets value of n to zero ~ can be used to get the negative of any value n = ~n + 1 sets value of n to –n (2’s compliment)

13 Get a Group of Bits Get a bit field of n from position p in value (put in least significant bits with zeros above) unsigned getbits(unsigned x, int p, int n) { return (x >> (p+1-n)) & ~(~0 << n); } Position p n Bits

14 Assignment Operators Section i += 3 means i = i + 3; –We can also use other operators int i = 3; i += 3; /* value now? */ ( 6) i <<= 2; /* value now? */ (24) i |= 0x02; /* value now? */ (24 = 0x18. 0x18 | 0x02 = 0x1a)

15 K&R Exercise 2-9 x &= (x-1) sets the rightmost 1-bit to a 0 char x = 0xa4 has bits x = & x x = & x x = & x x = Bits all counted What about char x = 0?

16 Conditional Expressions Section 2.11 Example: To implement z = max(a, b) if (a > b) z = a; else z = b; As long as both sides of if statement set only one variable value, it can be written: z = (a > b)? a: b;

17 Conditional Expressions Can also nest conditionals: z = (a > b)? a: ((x < y)? b: 0); Can include conditions inside an expression (e.g. to print EOL every 10 th value or at end): for (i = 0; i < n; i++) printf(“%6d%c”, a[i], (i%10 == 9 || i == n-1) ? ‘\n’ : ‘ ’);