Operators and Control Flow. CS449 Fall 2017

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

C: How to Program. Week /Mar/05

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

Lecture 3. More About C

Fundamental of Programming (C)

Should you know scanf and printf?

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Chapter 2 - Introduction to C Programming

Programming for Engineers Introduction to C

Full file at

Reserved Words and Identifiers

Work relative to other classes

Stream Model of I/O. Basic I/O in C

UNIT- 3 Introduction to C++

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

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

BASIC ELEMENTS OF A COMPUTER PROGRAM

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

C - Basic Introduction

Fundamentals of Programming Session 4

A Fast Review of C Essentials Part I

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Introduction to C Language

Visual C# Instructor s Manual Table of Contents

Variables and literals

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Fundamental of Programming (C)

Introduction to C Programming

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

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

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Fundamentals of Programming. Lecture 3: Introduction to C Programming

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Computers Programming Course 6. Iulian Năstac

Full file at C How to Program, 6/e Multiple Choice Test Bank

Fundamentals of Programming

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo

Data Types and Variables in C language

Lexical Considerations

Fundamentals of Programming

Basics of Programming

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

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

Chapter 12 Variables and Operators

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

File Handling in C. EECS 2031 Fall October 27, 2014

Lexical Considerations

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

The C++ Language. Arizona State University 1

1 Lexical Considerations

Course Outline Introduction to C-Programming

Chapter 2. Lexical Elements & Operators

ANSI C Programming Simple Programs

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Unit 4. Input/Output Functions

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

UIC. C Programming Primer. Bharathidasan University

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

Continued from previous lecture

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

Computer System and programming in C

Objectives. In this chapter, you will:

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Basics of Java Programming

C Programming

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

DEPARTMENT OF MATHS, MJ COLLEGE

C++ Programming: From Problem Analysis to Program Design, Third Edition

Chapter 2 Basic Elements of C++

Chapter 5: Control Structures

C++ character set Letters:- A-Z, a-z Digits:- 0 to 9 Special Symbols:- space + - / ( ) [ ] =! = < >, $ # ; :? & White Spaces:- Blank Space, Horizontal

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Chapter 1 & 2 Introduction to C Language

Introduction. C provides two styles of flow control:

2. Numbers In, Numbers Out

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

UNIT 3 OPERATORS. [Marks- 12]

printf( Please enter another number: ); scanf( %d, &num2);

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Chapter 12 Variables and Operators

Programming and Data Structures

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

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

Chapter 2: Using Data

Structured Program Development

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Basic Elements of C. Staff Incharge: S.Sasirekha

Java Notes. 10th ICSE. Saravanan Ganesh

Transcription:

Operators and Control Flow CS449 Fall 2017

Running Example #include <stdio.h> /* header file */ int main() { int grade, count, total, average; /* declaramons */ count = 0; /* inimalizamon */ total = 0; /* inimalizamon */ while(1) { prinp("enter grade: "); /* prompt */ scanf("%d", &grade); /* read input */ if(grade < 0) break; /* break out of loop */ else total = total + grade; count = count + 1; average = total / count; prinp("average score is %d\n", average); return 0; >>./a.out Enter grade: 100 Enter grade: 90 Enter grade: -1 Average score is 95

Comments #include <stdio.h> /* header file */ int main() { int grade, count, total, average; /* declara,ons */ count = 0; /* ini,aliza,on */ total = 0; /* ini,aliza,on */ while(1) { prinp("enter grade: "); /* prompt */ scanf("%d", &grade); /* read input */ if(grade < 0) break; /* break out of loop */ else total = total + grade; count = count + 1; average = total / count; prinp("average score is %d\n", average); return 0; - Annotates code for be^er readability - Ignored by compiler (not part of program) - Syntax: - /* some string */ - // some string

Variable DeclaraMons #include <stdio.h> /* header file */ int main() { int grade, count, total, average; /* declaramons */ count = 0; /* inimalizamon */ total = 0; /* inimalizamon */ while(1) { prinp("enter grade: "); /* prompt */ scanf("%d", &grade); /* read input */ if(grade < 0) break; /* break out of loop */ else total = total + grade; count = count + 1; average = total / count; prinp("average score is %d\n", average); return 0; - Syntax: <type> <variable name> - Compiler allocates a memory loca,on for the variable - Also, declares the type of the variable - Type tells compiler the amount of memory to allocate for each variable - Must come before use of variable (for readability: at beginning of funcmon) - Variable names: consist of le^ers, digits, and underscores, case sensimve

Constants #include <stdio.h> /* header file */ int main() { int grade, count, total, average; /* declaramons */ count = 0; /* inimalizamon */ total = 0; /* inimalizamon */ while(1) { prinp("enter grade: "); /* prompt */ scanf("%d", &grade); /* read input */ if(grade < 0) break; /* break out of loop */ else total = total + grade; count = count + 1; average = total / count; prinp("average score is %d\n", average); return 0; - Values that stay constant - Not associated with a memory locamon - Numeric constants - Decimal: 0, 1, 2 - Octal: 012, 01776 (prefixed with 0) - Hexadecimal: 0xf5, 0xdeadbeef (prefixed with 0x) - Character constants - Single character: a, b, 1 (single quotes) - Character string: abc, 123 (double quotes)

Operators #include <stdio.h> /* header file */ int main() { int grade, count, total, average; /* declaramons */ count = 0; /* inimalizamon */ total = 0; /* inimalizamon */ while(1) { prinp("enter grade: "); /* prompt */ scanf("%d", &grade); /* read input */ if(grade < 0) break; /* break out of loop */ else total = total + grade; count = count + 1; average = total / count; prinp("average score is %d\n", average); return 0; - Performs the actual computamon - Assignment : Stores value into locamon - = - ArithmeMc : Operates on numbers - +, -, *, /, % - Logical : Operates on boolean values - &&,,! - Comparison : Compares two numbers - ==,!=, <. >, <=, >= - Index : Operates on arrays - [] - Bitwise : Bitwise operamons on numbers - &,, ^, ~, <<, >> - Reference / Dereference - &, *

Reference / Dereference Operators Pointer: Variable that stores a memory address Reference operator: &x Value: address of variable x Type: pointer to x Dereference operator: *p Value: memory loca,on pointed to by p Type: base type of pointer p What s the value of x at the end? int x = 0; // x is of type int int *p = 0; // p is of type int * p = &x; // assign address of x to p *p = 13; // assign 13 to location // pointed to by p Address Memory Name 1000 0 x 1001 0 p Address Memory Name 1000 13 x 1001 1000 p

Reference / Dereference Quiz Address Memory Name 0000 0001 5 x 0002 0003 0004 0005 10 0006...... If x is declared as char x; What is x? A) Value: 5, Type: char What is &x? A) Value: 1, Type: char * What is *x? A) Illegal! (x is not an address) If x is declared as char *x; What is x? A) Value: 5, Type: char * What is &x? A) Value: 1, Type: char ** What is *x? A) Value: 10, Type: char

Rules for Reference / Dereference Rules for Reference Operator & Operand: a variable (i.e. name of a memory locamon) &(100) is illegal // numerical constant does not have address Result: address of memory locamon &x = 100 is illegal // cannot assign number to another number Can the following expression be legal &(&x)? Rules for Deference Operator * Operand: address of memory locamon *( (int *) 100 ) is legal // (int *) 100 is a legal address Result: memory locamon E.g. *x = 100 is legal // *x is a locamon that can be assigned to Can the following expression be legal *(*x)?

Bitwise Operators Java also has them but more o en used in C System programs o en store mulmple values (typically flags) in a single variable. Why? To save memory. How? By shi ing data in and out. Examples Get value of 5 th bit: flag = (x >> 5) & 1 /* SHIFT x right 5 Mmes, AND result with 1 */ Set 5 th bit to 1: x = x (1 << 5) /* SHIFT 1 le 5 Mmes, OR result with x */ Set 5 th bit to 0: x = x & ~(1 << 5) /* SHIFT 1 le 5 Mmes, NOT the result, then AND with x */

Shortcut Operators (SyntacMc Sugar) Shortcut operators exist strictly for convenience Shortcut assignment operators Shorthand for a computamon and an assignment Ex) x += 10 is equivalent to x = x + 10; +=, -=, *=, /=, %=, &=, =, ^=, <<=, >>= Increment (++) and decrement (--) operators Adds or subtracts 1 from a variable Post-increment (x++) vs. Pre-increment (++x) Both increment x by 1 Difference in return value of expression: If x was originally 10, x++ returns 10, ++x returns 11

Operator Precedence Type Operators Associa,vity highest () le to right unary + - ++ --! * right to le mulmplicamve * / % le to right addimve + - le to right relamonal < <= > >= le to right equality ==!= le to right logical and && le to right logical or le to right assignment = += -= *= /= right to le When not sure, use parenthesis! 2000 PrenMce Hall, Inc. All rights reserved.

Control Statements #include <stdio.h> /* header file */ int main() { int grade, count, total, average; /* declaramons */ count = 0; /* inimalizamon */ total = 0; /* inimalizamon */ while(1) { prinp("enter grade: "); /* prompt */ scanf("%d", &grade); /* read input */ if(grade < 0) break; /* break out of loop */ else total = total + grade; count = count + 1; average = total / count; prinp("average score is %d\n", average); return 0; - Changes the control flow of the program (what code to execute next) - IteraMon statement - Loops over statement while condimon is samsfied - Statement: line ending with semicolon or compound statement (a.k.a block) inside braces - while, do/while, for - SelecMon statement - Selects one among mulmple statements to execute - if/else, switch/case - Jump statement - Jumps to specific locamon in code - break, conmnue, return, goto

IteraMon Statements <while loop> i = 0; while (i < 10) { prinp( %d, i); i = i + 1; <do while loop> i = 0; do { prinp( %d, i); i = i + 1; while (i < 10); <for loop> for(i = 0; i < 10; i = i + 1) { prinp( %d, i); >>./a.out 0 1 2 3 4 5 6 7 8 9

SelecMon Statements <if statement> score = 75; if (score >= 90) prinp( A ); else if (score >= 80) prinp( B ); else if (score >= 70) prinp( C ); else if (score >= 60) prinp( D ); else prinp( F ); >>./a.out C char grade = A ; switch (grade) { case A : <switch statement> prinp( Excellent\n ); case B : case C : case D : prinp( Pass\n ); break; case F : prinp( Fail\n ); break; default: prinp( Unknown\n ); break; >>./a.out Excellent Pass

Jump Statements <break> i = 0; while (i < 10) { if (i > 5) break; prinp( %d, i); i = i + 1; >>./a.out 0 1 2 3 4 5 i = 0; <conmnue> while (i < 10) { if (i > 5) { i = i + 1; con,nue; prinp( %d, i); i = i + 1; <goto> i = 0; while (i < 10) { if (i > 5) goto label; prinp( %d, i); i = i + 1; label:

PrinP and Scanf #include <stdio.h> /* header file */ int main() { int grade, count, total, average; /* declaramons */ count = 0; /* inimalizamon */ total = 0; /* inimalizamon */ while(1) { prinr("enter grade: "); /* prompt */ scanf("%d", &grade); /* read input */ if(grade < 0) break; /* break out of loop */ else total = total + grade; count = count + 1; average = total / count; prinr("average score is %d\n", average); return 0; - Include stdio.h header file to use - Part of standard C library - We will learn what that means soon - For now, just know it has already been implemented for you - PrinP outputs text to the stdout stream, which is typically connected to the text console (e.g. your monitor) - Scanf gets input from the stdin stream, which is typically connected to your keyboard

PrinP int prinp(const char* format,...) Return value: number of characters printed : List of values you want to print, separated by commas Format: a string that specifies how to format values Can contain escape characters and format specifiers E.g. sum=%d\n Escape characters: Used to denote characters that would confuse the compiler parser if used directly \n newline ( go to the next line) \r return ( go to the beginning of the line ) \t tab character \' single quote (character ' ) \" double quote (character " )

PrinP Format Specifiers Format specifier: %[opmonal flags]specifier Specifiers d or i: Signed decimal integer u: Unsigned decimal integer o: Unsigned octal x, X: Unsigned hexadecimal (lowercase, uppercase) c: Character s: Character String p: Pointer address f, F: Decimal floamng point (lowercase, uppercase) e, E: ScienMfic notamon (manmssa + exponent) g, G: Use shortest representamon between f and e

PrinP Format OpMonal Flags Format specifier: %[padding][width][.precision][length]specifier Padding: 0: Pads numbers with 0s instead of spaces Width: minimum number of characters printed (if value is shorter, it is padded with spaces or 0s depending on padding) Precision: for real numbers, number of digits to be printed a er the decimal point Length: length of data type to be printed (none): int hh: char h: short l: long ll: long long

PrinP Examples #include <stdio.h> int main() { prinp ("Characters: %c %c \n", 'a', 65); prinp ("Preceding with blanks: %10d \n", 1977); prinp ("Preceding with zeros: %010d \n", 1977); prinp ("Some different radices: %d %x %o \n", 100, 100, 100); prinp ("floats: %.2f %08.4f %E \n", 3.1416, 3.1416, 3.1416); prinp ("%s \n", "A string"); return 0; >>./a.out Characters: a A Preceding with blanks: 1977 Preceding with zeros: 0000001977 Some different radices: 100 64 144 floats: 3.14 003.1416 3.141600E+000 A string

Scanf int scanf(const char* format,...) Return value: number of input items successfully matched and consumed : List of variables you want to scan into Format: idenmcal to prinp except that now it specifies the format of the input stream. If input does not match format (e.g. a string is given in place of a number specifier), not consumed Example scanf( %d, &x); <= input abcd : Failure! scanf( %x, &x); <= input abcd : Success! (x will contain hexadecimal value 0xabcd)

PiPall 1: IniMalizaMon What s wrong with the following code? int sum, i; for(i = 0; i < 10; ++i) { sum += i; Sum was not inimalized to 0. Java does this automamcally for you. C does not.

PiPall 2: The equality operator What s wrong with the following code? if (x = 10) {... The equality operator == should be used instead of the assignment operator = Will not even compile in Java but is legal in C! = operator returns the assigned value In example, assignment expression has value 10

PiPall 3: The increment operator The following code wants to print 1 9. What s wrong with it? i = 0; while(i++ < 10) { prinp( %d\n, i); Should have used pre-increment (++i) rather than post-increment

PiPall 4: Malformed switch/case What s wrong with the following code? switch(x) { case 0: prinp( x == 0\n ); case 1: prinp( x == 1\n ); Always remember to put breaks appropriately Always make a habit of pu ng in a default clause

PiPall 5: Type conversion C standard: When operamons happen between two different types, the less precise type is converted to the more precise type What s wrong with the following code? int x = 1, y = 2; float z = x / y; A) Problem: x / y results in an integer division SoluMon: float z = (float) x / y; /* x to float */ Or this code? int x = -1; unsigned int y = 1; if(x > y) print( x is larger than y.\n ); A) Problem: x is implicitly converted to an unsigned int SoluMon: if(x > (int) y) ; Always cast explicitly when not certain.

PiPall 6: Scanf and & What s wrong with the following code? printf( Enter: ); scanf( %d, num); printf( You typed: %d, num); Scanf takes pointers as arguments, which it dereferences to update the memory locamon SoluMon: printf( Enter: ); scanf( %d, &num); printf( You typed: %d, num); >>./a.out Enter: 10 SegmentaMon fault (core dumped) >>./a.out Enter: 10 You typed: 10

PiPall 7: Disappearing prinp What would happen? Will Here print? int *p = NULL; // p initialized to null pointer printf( Here ); // debug output *p = 10; // CRASH due to null pointer write Most likely, no. Stdout stream is by default buffered at line granularity (for efficiency reasons) C standard library stores prinp output into a memory buffer and flushes the buffer to screen only when a new line is encountered What if program crashes before buffer gets chance to be flushed? SoluMons a^ach newline to the end of string (e.g. Here\n ) fflush(stdout); immediately a er prinp will flush the buffer setbuf(stdout, NULL); to remove all buffering