ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C

Similar documents
ECE 2400 Computer Systems Programming Fall 2018 Topic 2: C Recursion

1 Lexical Considerations

Flow Control. CSC215 Lecture

Introduction. C provides two styles of flow control:

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Chapter 4. Flow of Control

CS1100 Introduction to Programming

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Typescript on LLVM Language Reference Manual

YOLOP Language Reference Manual

Programming for Engineers Iteration

UNIT- 3 Introduction to C++

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Expression and Operator

Lexical Considerations

Lexical Considerations

egrapher Language Reference Manual

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Operators. Java operators are classified into three categories:

Computers Programming Course 6. Iulian Năstac

Chapter 17. Fundamental Concepts Expressed in JavaScript

Mirage. Language Reference Manual. Image drawn using Mirage 1.1. Columbia University COMS W4115 Programming Languages and Translators Fall 2006

Flow Language Reference Manual

Introduction to C Programming

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

CHAD Language Reference Manual

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR. VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS

GAWK Language Reference Manual

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

ECE 2400 Computer Systems Programming Fall 2018 Topic 8: Complexity Analysis

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Intermediate Code Generation

Language Reference Manual simplicity

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

C: How to Program. Week /Mar/05

Course Outline Introduction to C-Programming

Chapter 5: Control Structures

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers

(Not Quite) Minijava

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

BoredGames Language Reference Manual A Language for Board Games. Brandon Kessler (bpk2107) and Kristen Wise (kew2132)

CHIL CSS HTML Integrated Language

Operators & Expressions

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

FRAC: Language Reference Manual

Java Notes. 10th ICSE. Saravanan Ganesh

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

Decaf Language Reference Manual

A Simple Syntax-Directed Translator

Computer Programming I - Unit 5 Lecture page 1 of 14

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Crayon (.cry) Language Reference Manual. Naman Agrawal (na2603) Vaidehi Dalmia (vd2302) Ganesh Ravichandran (gr2483) David Smart (ds3361)

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

SSOL Language Reference Manual

Computer Programming : C++

Information Science 1

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Review of the C Programming Language

Part I Part 1 Expressions

Fundamental of Programming (C)

DEMO A Language for Practice Implementation Comp 506, Spring 2018

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Decision Making in C

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

The SPL Programming Language Reference Manual

Introduction to Programming Using Java (98-388)

CS201 Latest Solved MCQs

Language Reference Manual

The pixelman Language Reference Manual. Anthony Chan, Teresa Choe, Gabriel Kramer-Garcia, Brian Tsau

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

G Programming Languages - Fall 2012

Using Boolean Expressions. Multiway Branches. More about C++ Loop Statements. Designing Loops. In this chapter, you will learn about:

Chapter 2 - Introduction to C Programming

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

6. Operatoren. 7. Safe Programming: Assertions. Table of Operators. Table of Operators - Explanations. Tabular overview of all relevant operators

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Decaf Language Reference

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Introduction to C/C++ Lecture 3 - Program Flow Control

CGS 3066: Spring 2015 JavaScript Reference

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

CS313D: ADVANCED PROGRAMMING LANGUAGE

Dept. of CSE, IIT KGP

A Short Summary of Javali

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

This book is licensed under a Creative Commons Attribution 3.0 License

Transcription:

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C School of Electrical and Computer Engineering Cornell University revision: 2018-09-03-15-59 1 Statements, Syntax, Semantics, State 3 2 Variables, Literals, Operators, Expressions 4 2.1. Variables.................................. 5 2.2. Literals................................... 5 2.3. Operators................................. 6 2.4. Expressions................................ 7 2.5. Simple C Programs............................ 8 3 Blocks, Scope, Name Binding 9 3.1. Blocks.................................... 9 3.2. Scope.................................... 10 3.3. Name Binding............................... 10 4 Functions 12 4.1. Function Definition............................ 12 4.2. Function Call............................... 13 4.3. The printf Function........................... 16 1

5 Conditional Statements 17 5.1. Boolean Operators............................ 17 5.2. if/else Conditional Statements.................... 19 5.3. switch/case Conditional Statements................. 22 6 Iteration Statements 25 6.1. while Loops................................ 25 6.2. for Loops................................. 27 7 Syntactic Sugar 29 Copyright 2018 Christopher Batten. All rights reserved. This handout was prepared by Prof. Christopher Batten at Cornell University for ECE 2400 / ENGRD 2140 Computer Systems Programming. Download and use of this handout is permitted for individual educational non-commercial purposes only. Redistribution either in part or in whole via both commercial or non-commercial means requires written permission. 2

1. Statements, Syntax, Semantics, State Before you can learn to write, you must learn to read! This is true for foreign languages and programming languages. 1. Statements, Syntax, Semantics, State Program Execution Arrow State Statement Statement Syntax Statement Semantics Program Sequence of statements It is raining outside. Should I use an umbrella? Statement Sentence It is raining outside. Syntax Semantics State Sentence grammar Sentence meaning Memory of prior statements punctuation; I is a pronoun; is uses present tense rain is water condensed from the atmosphere, outside means in the outdoors remember that it is raining outside when considering umbrella 3

2. Variables, Literals, Operators, Expressions 2.1. Variables An example English program 1 Create box named x. 2 Put value 3 into box named x. 3 Create box named y. 4 Put value 2 into box named y. 5 Create box named z. 6 Put x + y into box named z. stmt x y z 1 2 3 4 5 6 2. Variables, Literals, Operators, Expressions A variable is a box (in the computer s memory) which stores a value; variables have names and are used for state A literal is a value written exactly as it is meant to be interpreted; a literal is not a name, it is the value itself An operator is a symbol with special semantics to operate on variables and literals An expression is a combination of variables, literals, and operators which evaluates to a new value 4

2. Variables, Literals, Operators, Expressions 2.1. Variables 2.1. Variables A variable is a box (in the computer s memory) which stores a value An identifier is used to name a variable A type specifies the kind of values that can be stored in a variable 1 int my_variable; 2 int MY_VARIABLE; 3 int variable_0; 4 int 0_variable; 5 int variable$1; A variable declaration statement creates a new variable Statements in C must end with a semicolon 2.2. Literals A literal is a value written exactly as it is meant to be interpreted A variable is a name that can represent different values A constant variable is a name that can represents the same value A literal is not a name but the value itself Example integer literals 13 literally the number 13 in base 10-13 literally the number -13 in base 10 0x13 literally the number 13 in base 16 (i.e., 19 in base 10) 0xdeadbeef literally a large number in base 16 5

2. Variables, Literals, Operators, Expressions 2.3. Operators 2.3. Operators The assignment operator (=) assigns a new value to a variable An assignment statement combines the assignment operator with a left-hand side (LHS) and a right-hand side (RHS) The LHS specifies the variable to change The RHS specifies the new value, possibly using a literal 1 int my_variable; 2 my_variable = 42; A variable declaration statement and an assignment statement can be combined into a single initialization statement 1 int my_variable = 42; Other operators are provided for arithmetic functions such as addition (+), subtraction (-), multiplication (*), division (/), and modulus (%) 6

2. Variables, Literals, Operators, Expressions 2.4. Expressions 2.4. Expressions An expression is a combination of variables, literals, and operators which evaluates to a new value 1 3 + 4 2 3 + 4 * 2 + 7 3 3 * 4 / 2 * 6 Operator precedence is a set of rules describing in what order we should apply a sequence of operators in an expression Category Operator Associativity Multiplicative * / % left to right Additive + - left to right Assignment = right to left Be explicit use parenthesis! 7

2. Variables, Literals, Operators, Expressions 2.5. Simple C Programs 2.5. Simple C Programs We can compose assignment and initialization statements which use variables, literals, operators, and expressions to create a simple C program. Translating our English program into a C program stack 01 int x; 02 x = 3; 03 int y; 04 y = 2; 05 int z; 06 z = x + y; An empty box in a state diagram means the variable contains an undefined value Draw a state diagram corresponding to the execution of this program stack 01 int x = 3; 02 int y = 2; 03 int z = x + y * 5; 04 y = x + y * x + y; 8

3. Blocks, Scope, Name Binding 3.1. Blocks 3. Blocks, Scope, Name Binding Blocks, scope, and name binding help provide syntax and semantics for managing more complex programs 3.1. Blocks A block is a compound statement Curly braces are used to open and close a block ({}); the trailing semicolon may be (should be) omitted Blocks are critical for defining functions, conditional statements, and iteration statements 1 { int x = 2; int y = x; }; 2 { int z = 3; z = z + 1; } 9

3. Blocks, Scope, Name Binding 3.2. Scope 3.2. Scope So far we have only had one variable with a given name 1 int x = 1; 2 int x = 2; 3 int y = x; Scope of a variable is the region of code where it is accessible C blocks create new local scopes We can declare new variables that are only in scope in the block We can declare new variables in the local scope with same name as a variable declared in the parent scope (syntax) 1 int x = 1; 2 { 3 int x = 2; 4 int y = x; 5 } 6 int y = x; 3.3. Name Binding Key Question: When we use a variable name, what variable declaration is it referring to? (semantics) Name binding is a set of rules to answer this question by associating a specific variable name to a specific in-scope variable declaration C uses static (lexical) scoping meaning the name binding happens statically at compile time 10

3. Blocks, Scope, Name Binding 3.3. Name Binding Steps for name binding 1. Draw the scope of each variable in source code 2. Draw circle in source code around use of a variable name 3. Determine which variables with that name are in scope 4. Draw line to variable declaration in the inner most enclosing block 5. Draw circle in source code around variable declaration 01 int x = 1; 02 { 03 int x = 2; 04 int y = x; 05 } 06 int y = x; stack 01 int x = 1; 02 { 03 x = 2; 04 { 05 int y = x; 06 int x = 3; 07 x = 4; 08 } 09 x = 5; 10 } 11 int y = x; stack 11

4. Functions 4.1. Function Definition 4. Functions A function gives a name to a parameterized sequence of statements A function definition describes how a function behaves A function call is a new kind of expression to execute a function All code in C programs are inside functions! 4.1. Function Definition 1 rtype function_name( ptype0 pname0, ptype1 pname1,... ) 2 { 3 function_body; 4 } A function name is a unique identifier for the function The function body is the parameterized sequence of statements The parameter list is a list of parameter types and names The return type is the type of the value returned by the function 1 int avg( int x, int y ) 2 { 3 int sum = x + y; 4 int ans = sum / 2; 5 return ans; 6 } 12

4. Functions 4.2. Function Call 1 int main() 2 { 3 int a = 10; 4 int b = 20; 5 int c = ( a + b ) / 2; 6 return 0; 7 } main is special: it is always the first function executed in a program main returns its value to the system The return value is called the exit status for the program Returning zero means success in Linux Returning greater than zero means failure in Linux 4.2. Function Call 1 function_name( pvalue0, pvalue1,... ) To call a function we simply use its name and pass in one value for each parameter in the parameter list surrounded by parenthesis If parameters are expressions, then we must evaluate them before calling the function A function call is itself an expression which evaluates to the value returned by the function Function parameters and local variables declared within a function are effectively in a new block which is called the function s stack frame The value of each parameter is copied into these local variables (call-by-value semantics) 13

4. Functions 4.2. Function Call Steps for calling a function 0. Allocate variable for return value on caller s stack frame 1. Draw called function s stack frame w/ parameter boxes 2. Initialize parameters by evaluating expressions in function call 3. Record location of function call 4. Move execution arrow to first statement in called function 5. Evaluate statements inside the called function 6. At return statement, evaluate its argument, update variable in caller 7. Return execution arrow back to where the function was called 8. Erase the called function s frame 9. Use function s return value as value of function call 01 int avg( int x, int y ) 02 { 03 int sum = x + y; 04 int ans = sum / 2; 05 return ans; 06 } 07 08 int main() 09 { 10 int a = 10; 11 int b = 20; 12 int c = avg( a, b ); 13 return 0; 14 } stack 14

4. Functions 4.2. Function Call Draw a state diagram corresponding to the execution of this program stack 01 int avg( int x, int y ) 02 { 03 int sum = x + y; 04 return sum / 2; 05 } 06 07 int main() 08 { 09 int y = 10; 10 int x = 20; 11 x = avg( avg(y,x), avg(30,40) ); 12 return 0; 13 } 15

4. Functions 4.3. The printf Function 4.3. The printf Function The printf function is provided by the C standard library and can be used to print values to the screen. Here is pseudocode for the printf function definition. 1 printf( format_string, value0, value1,... ) 2 { 3 substitute value0 into format_string 4 substitute value1 into format_string 5... 6 display final format_string on the screen 7 } Here is an example of calling printf. 1 #include <stdio.h> 2 3 int avg( int x, int y ) 4 { 5 int sum = x + y; 6 return sum / 2; 7 } 8 9 int main() 10 { 11 int a = 10; 12 int b = 20; 13 int c = avg( a, b ); 14 printf( "average of %d and %d is %d\n", a, b, c ); 15 return 0; 16 } 16

5. Conditional Statements 5.1. Boolean Operators https://repl.it/@cbatten/ece2400-t01-ex1 Examine the machine instructions via https://godbolt.org 5. Conditional Statements Conditional statements enable programs to make decisions based on the values of their variables Conditional statements enable non-linear forward control flow 5.1. Boolean Operators Boolean operators are used in expressions which evaluate to a boolean value (i.e., true or false) In C, a boolean value is just an integer, where we interpret a value of zero to mean false and any non-zero value to mean true expr1 == expr2 expr1!= expr2 tests if expr1 is equal to expr2 tests if expr1 is not equal to expr2 expr1 < expr2 tests if expr1 is less than to expr2 expr1 <= expr2 tests if expr1 is less than or equal to expr2 expr1 > expr2 tests if expr1 is greater than to expr2 expr1 >= expr2!expr expr1 && expr2 expr1 expr2 tests if expr1 is greater than or equal to expr2 computes the logical NOT of expr computes the logical AND of expr1 and expr2 computes the logical OR of expr1 and expr2 Using these operators in an expression evaluates to either zero (false) or one (true) 17

5. Conditional Statements 5.1. Boolean Operators Logical operators also have a place in the operator precedence table Category Operator Associativity Unary! right to left Multiplicative * / % left to right Additive + - left to right Relational < <= > >= left to right Equality ==!= left to right Logical AND && left to right Logical OR left to right Assignment = right to left 18

5. Conditional Statements 5.2. if/else Conditional Statements 5.2. if/else Conditional Statements 1 if ( conditional_expression ) 2 then_statement; 3 else 4 else_statement; A conditional expression is an expression which returns a boolean The then statement is executed if the conditional expression is true The else statement is executed if the conditional expression is false Recall that blocks are compound statements 1 if ( conditional_expression0 ) 2 then_statement0; 3 else if ( conditional_expression1 ) 4 then_statement1; 5 else 6 else_statement; If the first cond expression is true, execute first then statement If the first cond expression is false, evaluate second cond expression If second cond expression is true, execute second then statement If second cond expression is false, execute else statement 19

5. Conditional Statements 5.2. if/else Conditional Statements 01 int min( int x, int y ) 02 { 03 int z; 04 if ( x < y ) { 05 z = x; 06 } 07 else { 08 z = y; 09 } 10 return z; 11 } 12 13 int main() 14 { 15 min( 5, 9 ); 16 min( 7, 3 ); 17 return 0; 18 } stack 20

5. Conditional Statements 5.2. if/else Conditional Statements 01 int min3( int x, int y, int z ) 02 { 03 if ( x < y ) { 04 if ( x < z ) 05 return x; 06 } 07 else if ( y < z ) { 08 return y; 09 } 10 return z; 11 } 12 13 int main() 14 { 15 min3( 3, 7, 2 ); 16 return 0; 17 } stack 21

5. Conditional Statements 5.3. switch/case Conditional Statements 5.3. switch/case Conditional Statements 1 switch ( selection_expression ) { 2 3 case case_label0: 4 case_statements0; 5 break; 6 7 case case_label1: 8 case_statements1; 9 break; 10 11 case case_label2: 12 case_statements3; 13 break; 14 15 default: 16 default_statements; 17 18 } A selection expression is an expression which returns a value The value is matched against the case labels If there is a match, then corresponding case statements are executed A break statement means to jump to end of switch block If no case labels match then the default statements are executed 22

5. Conditional Statements 5.3. switch/case Conditional Statements 01 int days_in_month( int month ) 02 { 03 int x; 04 switch ( month ) 05 { 06 case 1: x = 31; break; 07 case 2: x = 28; break; 08 case 3: x = 31; break; 09 case 4: x = 30; break; 10 case 5: x = 31; break; 11 case 6: x = 30; break; 12 case 7: x = 31; break; 13 case 8: x = 31; break; 14 case 9: x = 30; break; 15 case 10: x = 31; break; 16 case 11: x = 30; break; 17 case 12: x = 31; break; 18 default: x = -1; 19 } 20 return x; 21 } 22 23 int main() 24 { 25 int num_days = days_in_month( 7 ); 26 27 // Indicate error to the system 28 if ( num_days == -1 ) 29 return 1; 30 31 // Indicate success to the system 32 return 0; 33 } stack 23

5. Conditional Statements 5.3. switch/case Conditional Statements Identifying Primes Write a C function that takes one integer input (x) that is between 0 and 9 (inclusive) and returns a boolean output. The function should return true if the input is prime (i.e., 2,3,5,7) and return false if the input is not prime. Use a switch/case conditional statement to explicitly check for primes. int is_prime( int x ) { 24

6. Iteration Statements 6.1. while Loops 6. Iteration Statements Iteration statements enable programs to execute the same code multiple times based on a conditional expression Iteration statements enable backward flow control Two primary kinds of iteration statements: while and for loops 6.1. while Loops 1 while ( conditional_expression ) 2 loop_body; A conditional expression is an expression which returns a boolean The loop body is a statement which is executed as long as conditional expression is true An infinite loop is when conditional expression is never false 25

6. Iteration Statements 6.1. while Loops 01 int gcd( int x, int y ) 02 { 03 while ( y!= 0 ) { 04 if ( x < y ) { 05 int temp = x; 06 x = y; 07 y = temp; 08 } 09 else { 10 x = x - y; 11 } 12 } 13 return x; 14 } 15 16 int main() 17 { 18 gcd(5,15); 19 return 0; 20 } stack stmt x y 4 4 4 4 4 4 26

6. Iteration Statements 6.2. for Loops 6.2. for Loops 1 for ( initialization_stmt; cond_expr; increment_stmt ) 2 loop_body; The initialization statement is executed once before loop executes A conditional expression is an expression which returns a boolean The loop body is a statement which is executed as long as conditional expression is true The increment statement is executed at the end of each iteration 01 int mul( int x, int y ) 02 { 03 int z = 0; 04 for ( int i=0; i<y; i=i+1 ) { 05 z = z + x; 06 } 07 return z; 08 } 09 10 int main() 11 { 12 mul(2,3); 13 return 0; 14 } stack 27

6. Iteration Statements 6.2. for Loops Output a sequence Write a C function that takes one integer input (N) that is non-negative. The C function should output a sequence of integers according to the pattern on the left. N output 0: 0 1: 0 0 2: 0 0 0 3: 0 0 0 3 4: 0 0 0 3 4 5: 0 0 0 3 4 5 6: 0 0 0 3 4 5 6 void print_seq( int N ) { 28

7. Syntactic Sugar 7. Syntactic Sugar Syntactic sugar adds new syntax but not new semantics Syntactic sugar simplifies certain programming patterns Syntactic sugar does not introduce any fundamentally new behavior for loops are syntactic sugar 1 for ( int i = 0; i < y; i = i+1 ) { 2 z = z + x; 3 } 4 5 { 6 int i = 0; 7 while ( i < y ) { 8 z = z + x; 9 i = i + 1; 10 } 11 } Assignment Operators Sugar Semantics x += y; x = x + y; x -= y; x = x - y; x *= y; x = x * y; x /= y; x = x / y; Postfix/Prefix Operators Sugar Semantics x++; x = x + 1; ++x; x = x + 1; x--; x = x - 1; --x; x = x - 1; 29

7. Syntactic Sugar Be careful, the value of ++x is x + 1, but the value of x++ is x. 1 int i = 1; 2 int j = ++i; 3 // i == 2; j == 2 Ternary operator is syntactic sugar 1 int i = 1; 2 int j = i++; 3 // i == 2; j == 1 1 int min( int x, int y ) 2 { 3 if ( x < y ) 4 return x; 5 return y; 6 } 1 int min( int x, int y ) 2 { 3 return ( x < y )? x : y; 4 } Final operator precedence table Category Operator Associativity Postfix a++ a-- left to right Unary! ++a --a right to left Multiplicative * / % left to right Additive + - left to right Relational < <= > >= left to right Equality ==!= left to right Logical AND && left to right Logical OR left to right Assignment = += -= *= /= a?b:c right to left 30