CS201 – Expressions & I/O. Precedence What is the value of 2 + 3 * 4 ? 20 or 14 ? Without parentheses, there are a set of rules to fall back on. Unary.

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

Pemrograman VisualMinggu …2… Page 1 MINGGU Ke Dua Pemrograman Visual Pokok Bahasan: Console Application Tujuan Instruksional Khusus: Mahasiswa dapat menjelaskan.
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Introduction to C Programming
C Language Elements (II) H&K Chapter 2 Instructor – Gokcen Cilingir Cpt S 121 (June 22, 2011) Washington State University.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
The scanf Function The scanf function reads input from the standard input device into one or more variables Example: scanf(“%lf”, &miles); Reads a real.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 CS 201 Introduction to C (2) Debzani Deb. 2 Overview C Arithmetic Expressions Formatting Numbers in Program Output Interactive Mode, Batch Mode, and.
Introduction to C Programming
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Basic Input/Output and Variables Ethan Cerami New York
CHAPTER 3: CORE PROGRAMMING ELEMENTS Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Introduction to programming Language C, Data Types, Variables, Constants. Basics of C –Every program consists of one or more functions and must have main.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Chapter 2 Overview of C Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Chapter 2: Using Data.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester King Saud University College of Applied studies and Community Service Csc
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Chapter 7 Formatted input and output. 7.1 introduction Tax: This result is correct; but it would be better Maybe as $13, Make formatting.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
3 1 Sending Data Using an Online Form CGI/Perl Programming By Diane Zak.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 3: Introduction to C: Input & Output, Assignments, Math functions.
Khalid Rasheed Shaikh Computer Programming Theory 1.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
S CCS 200: I NTERACTIVE I NPUT Lect. Napat Amphaiphan.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
1 ELE118 lecture 3 Dr. Mehmet Demirer Dr. Seniha Esen Yuksel.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
CISC105 – General Computer Science Class 2 – 6/7/2006.
CS 125 Lecture 9 Martin van Bommel. Output printf used to display integers, reals (floats and doubles), and strings printf(” control string ”, exp 1,
Introduction Chapter 1 1/22/16. Check zyBooks Completion Click on the boxes for each section.
A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.
UniMAP Sem I-09/10EKT150: Computer Programming1 Week 2 – Introduction to Computer and Algorithm.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
INTRODUCTION TO PROGRAMING System Development Mansoura October 2015.
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.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
28 Formatted Output.
Chapter 7 Text Input/Output Objectives
Input and Output: I/O Finish reading chapters 1 and 2 of the text
Chapter 7 Text Input/Output Objectives
Chapter 3: I/O Management
Introduction to C CSE 2031 Fall /3/ :33 AM.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Chapter 11 Introduction to Programming in C
INPUT & OUTPUT scanf & printf.
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Introduction to C Programming
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Presentation transcript:

CS201 – Expressions & I/O

Precedence What is the value of * 4 ? 20 or 14 ? Without parentheses, there are a set of rules to fall back on. Unary + - first * / % second Binary + - third So, under these rules, the answer is 14

Use parentheses To avoid problems always use parentheses. 2 + (3 * 4) They’re not needed but they aid readability. (2 + 3) * 4 Here they are needed.

Integer Division What is the value of 2/4? What about 2.0/4.0? When in doubt, try it! output printf(“%d”, 2/4); 0 printf(“%f”, 2/4); printf(“%d”, 2.0/4.0); 0 printf(“%f”, 2.0/4.0);

CS201 – C formatted I/O Internal representation hidden from user. MEMORY CONCEPT SCREEN printf 12 age KEYBOARD 12 j 34 scanf ‘3’ ‘4’ age j

printf & scanf (f = formatted) printf(“formatting codes”, variables); For formatting codes see man 3 printf or look at Table 2.11 – Table 2.13 in text. Use d for integers. %#d % - start of place holder # - field width (optional) d - place holder for integers

%#d (for integers) 61 using %4d would display □□61 □ stands for a displayed blank Note four characters output No decimal point -342 using %4d would display using %4d would display Field width is expanded to fit.

%n.mf (for floating point) float & double % start of place holder n field width (optional) m number of decimal places (optional) f place holder for floating point using %f using %5.2f using %5f * using %5.f □□□ using %5.1f □ using %8.2f □□□61.45 * If the precision is missing, 6 is assumed.

Redirection You can “redirect” the input sent to a scanf or the output sent from a printf using the shell symbols ‘ ’. $ program_name < data_in_file_name Take input from “data_in_file_name” $ program_name > data_out_file_name Send output to “data_out_file_name” $ prog outdata Take input from “indata” AND send output to “outdata”

External Data Files File names can be dynamically chosen using redirection. File names can be “hard coded” in the program. See Figure 2.14 (more in chapter 12)

Common Errors Syntax errors Found at compile time! Usually easy to fix! Run-Time errors Found at run time! Usually harder to fix! Often Logic errors! Undetected errors Most difficult to find and fix!