Input and Output: I/O Finish reading chapters 1 and 2 of the text

Slides:



Advertisements
Similar presentations
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Advertisements

Introduction to C Programming
Software Development Method & C Language Elements H&K Chapter 1-2
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
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.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Introduction to C Language
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.
A First Book of ANSI C Fourth Edition
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Scanf Reads in information from the keyboard Examples –scanf(“%d”, &number); –scanf(“%d%d”, &value1, &value2); WARNINGS! –Don’t forget the & (address of)
M-1 University of Washington Computer Programming I Program Style © 2000 UW CSE.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Khalid Rasheed Shaikh Computer Programming Theory 1.
Overview of C. C—a high-level programming language developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories. We will discuss: –the elements of a.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
9/29/99B-1 CSE / ENGR 142 Programming I Variables, Values, and Types © 1998 UW CSE.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CISC105 – General Computer Science Class 2 – 6/7/2006.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Introduction to C #include int main () { printf(“Welcome to CS 1621!\n”); }
A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
C Language Elements Preprocessor Directives # (sign for preprocessor directive commands) #include Standard header file (.h) Library.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Lecture2.
Bill Tucker Austin Community College COSC 1315
Numbers in ‘C’ Two general categories: Integers Floats
CSCE 206 Structured Programming in C
Lesson #2 Introduction to C.
Lesson #2 Introduction to C.
CSE 220 – C Programming C Fundamentals.
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
Input/output.
What's a Computer? Monitor Disk Main mouse Memory Keyboard Network
Revision Lecture
Chapter 2 Overview of C.
Chapter 3: I/O Management
ICS103 Programming in C Lecture 3: Introduction to C (2)
CSE / ENGR 142 Programming I
Lecture2.
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.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Introduction to CS Your First C Programs
Course websites CS201 page link at my website: Lecture slides
UMBC CMSC 104 – Section 01, Fall 2016
Lecture3.
Lesson #2 Introduction to C.
WEEK-2.
Unit 3: Variables in Java
Variables in C Topics Naming Variables Declaring Variables
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Input and Output: I/O Finish reading chapters 1 and 2 of the text Data into the Memory e.g. from the keyboard (via scanf) assignment statement ... Output: Data out of the Memory e.g. printing displaying on a monitor Examples: printf(“Enter the distance in miles”); scanf(“%lf”, &miles); printf(“This is %f kms\n”,1.609*miles); output input 142 D -1

Display Input and Output Use printf and scanf printf(“control string”,list of expressions); output format what to output input format what to input scanf(“control string”,&variable list); do not forget for printf placeholders (%d,...) int i; double pi; i=2; pi = 3.14; printf(“%d times %f is %f\n”,i,pi,i*pi); newline must match in number, order and type 2 times 3.140000 is 6.280000 142 D -2

Formatting Output to control the output format in printf double val; printf(“%4.2f”,val); to control the output format in printf 3.14 4 characters (2 after the decimal point) _ can have scientific or decimal notation e (or E) f 4.28e+01 42.8 _ other placeholders %i or %d for an int %c for a char check manual or text 142 D -3

printf(“pay, hours, and age? ”); scanf(“%lf%lf%i”,&pay,&hours,&age); for scanf double pay, hours; int age; printf(“pay, hours, and age? ”); scanf(“%lf%lf%i”,&pay,&hours,&age); placeholders must match in number, order and type lf for a double i for an int Notes When reading numbers (e.g double, int…), scanf skips spaces, tabs and newlines: '\t','\n' user can type spaces, tabs, newlines between numbers But NOT skipped when reading char scanf(“%c”,&letter); Try it out and learn from your mistakes! 142 D -4

Initializing variables giving something a value for the first time double radius; printf(“Radius of the circle? ”); scanf(“%lf”,&radius); We have seen: or double radius; radius = 2.14; or double radius = 2.14; Potential source of errors: forgetting to initialize a variable the value of the variable is random double pay, hours; pay = 26.5 * hours; the result is unpredictable! 142 D-5

Errors when programming C O M P I L E R L I N K E R executable program 010 110 .obj file .c file your program library (ANSI) header (stdio.h) debugger syntax errors: e.g. omitting ; check warnings e.g. variables not initialized link errors: I can’t find the functions you want! e.g. using printf without #include <stdio.h> run time errors: error, e.g. _ bad input (divide by 0, forgot & in scanf) _ bad program logic 142D-6

Programming Style A program is a document: The compiler reads some of it (comments are ignored) Some people have to read ALL of it (update, corrections…) A program MUST be easy to read by another programmer Use comments, spacing, indentation, meaningful names 142D-7

Identifiers Identifiers: names used throughout the program for variables and functions (see later, like printf, scanf…) Reminder: _ contains letters, underscores, numbers _ do not begin with a number or underscore _ not a key word, e.g. double _case sensitive (var and Var are different variables) _ all capital lettered names are for #define constants (see next slide) _ use meaningful names that say what the variable is for. NOT m but miles 142D-8

#define allows you to define constants. Think of it as a text replacement command (but it can do more). #define PI 3.14159 no ; no = everywhere we write PI, the preprocessor will write 3.14159. Then, the file is sent to the compiler. Style convention: use only uppercase letters for these constants. 142D-9

Other examples #define LENGTH 100 #define WIDTH 150 #define HALF_PERIMETER (WIDTH+LENGTH) parentheses are important (without, can lead to computational errors) Centralize changes No magic numbers avoid accidental assignments to constants PI=3.141492; /* Error */ 142D-10

Clarity Avoid obscure statements: NO: x = (y=x) + 1; YES: y = x; x = x + 1; Be clever for a GOOD reason and say why in a comment int i; i = 1; … /* toggle the value of i between 0 and 1*/ i = (i+1)%2; 142D-11