Spring 2005, Gülcihan Özdemir Dağ Lecture 4, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring 2005. Lecture 4 Outline 4.0 Reading.

Slides:



Advertisements
Similar presentations
STANDARD INPUT/OUTPUT Lecture 9. Motivation Standard I/O refers to the places where most data is either read from, the keyboard, or written to, the video.
Advertisements

Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Outline 2.1 Introduction 2.2 Basics of C Programs
CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
CS1061 C Programming Lecture 17: Steams and Character I/O A. O’Riordan, 2004.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
1 CSE1301 Computer Programming: Lecture 9 Input/Output.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
CMSC 104, Version 8/061L15Switch.ppt The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Files Programs and data are stored on disk in structures called files Examples Turbo C++ - binary file Word binary file lab1.c - text file lab1.data.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
Computer programming Lecture 4. Lecture 4: Outline Making Decisions [chap 6 – Kochan] –The if Statement –The if-else Construct –Logical Operators –Boolean.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Spring 2005, Gülcihan Özdemir Dağ Lecture 5, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 5 Outline 5.0 Revisiting.
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
Spring 2005, Gülcihan Özdemir Dağ Lecture 8, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 8 Outline 8.1 Declaring.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
CMSC 104, Version 9/011 The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading Section 4.7,
Computer Programming Control Structure
CSE1301 Computer Programming: Lecture 6 Input/Output.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Lecture 5: Layers of Control. Nested while Loops Problem Multiplying two numbers and outputting the result only if they are both less than 5. (i.e. Start.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 5.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
C Formatted Input/Output
Introduction to C CSE 2031 Fall /3/ :33 AM.
CSE1320 Files in C Dr. Sajib Datta
CSE1320 Files in C Dr. Sajib Datta
By: Syed Shahrukh Haider
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.
The C “switch” Statement
The C “switch” Statement
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Structured Program
The switch Statement Topics Multiple Selection switch Statement
The switch Statement Topics Multiple Selection switch Statement
Module 12 Input and Output
Introduction to C EECS May 2019.
The switch Statement Topics Multiple Selection switch Statement
UMBC CMSC 104 – Section 01, Fall 2016
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Presentation transcript:

Spring 2005, Gülcihan Özdemir Dağ Lecture 4, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 4 Outline 4.0 Reading from and Writing to Standard I/O 4. 1 The getc() function 4. 2 The getchar() function 4. 3 The putc() function 4.4 The putchar() function 4. 5 Switch-Case (and break) 4. 6 Assignment Operators 4. 7 Increment and Decrement Operators

Spring 2005, Gülcihan Özdemir Dağ Lecture 4, Page Reading from and Writing to Standard I/O In this lesson we are going to learn more about printf(), as well as about the following functions, which are necessary to receive the input from the user or print the output to the screen: –The getc() function –The putc() function –The getchar() function –The putchar() function For the time being just memorize the followings: –stdin–The standard input for reading (usually keyboard) –stdout–The standard output for writing. (usually monitor) –stderr–The standard error for writing error messages. (always monitor)

Spring 2005, Gülcihan Özdemir Dağ Lecture 4, Page getc( ) function Using the getc() Function –The getc() function reads the next character from a file stream, and returns the character as an integer. The syntax for the getc() function is –# include –int getc(FILE *stream); EXMAPLE #include main() { int ch; printf("Please type in one character:\n"); ch = getc( stdin ); printf("The character you just entered is: %c\n", ch); return 0; }

Spring 2005, Gülcihan Özdemir Dağ Lecture 4, Page Using the getchar() Function The C language provides another function, getchar(), to perform a similar operation to getc(). More precisely, the getchar() function is equivalent to getc(stdin). The syntax for the getchar() function is #include int getchar(void); EXMAPLE #include main() { int ch1, ch2; printf("Please type in two characters together:\n"); ch1 = getc( stdin ); ch2 = getchar( ); printf("The first character you just entered is: %c\n", ch1); printf("The second character you just entered is: %c\n", ch2); return 0; }

Spring 2005, Gülcihan Özdemir Dağ Lecture 4, Page Using the putc() Function Printing the Output on the Screen Besides getc() and getchar() for reading, the C language also provides two functions, putc() and putchar(), for writing. The putc() function writes a character to the specified file stream, which, in our case, is the standard output pointing to your screen. The syntax for the putc() function is –#include –int putc(int c, FILE *stream); EXAMPLE #include main() { int ch; ch = 65; /* the numeric value of A */ printf("The character that has numeric value of 65 is:\n"); putc(ch, stdout); return 0; }

Spring 2005, Gülcihan Özdemir Dağ Lecture 4, Page Another Function for Writing: putchar() Like putc(), putchar() can also be used to put a character on the screen. The only difference between the two functions is that putchar() needs only one argument to contain the character. You don't need to specify the file stream, because the standard output (stdout) is the default file stream to putchar(). The syntax for the putchar() function is –#include –int putchar(int c); EXAMPLE #include main() { putchar(65); putchar(10); putchar(66); putchar(10); putchar(67); putchar(10); return 0; }

Spring 2005, Gülcihan Özdemir Dağ Lecture 4, Page 7 Revisiting the printf() Function The printf() function is the first C library function you used in this course to print out messages on the screen. printf() is a very important function in C, so it's worth it to spend more time on it. The syntax for the printf() function is –#include –int printf(const char *format-string,...); Here const char *format-string is the first argument that contains the format specifier(s);... indicates the expression section that contains the expression(s) to be formatted according to the format specifiers. The number of expressions is determined by the number of the format

Spring 2005, Gülcihan Özdemir Dağ Lecture 4, Page 8 Format Specifiers The following are all the format specifiers that can be used in printf(): %c The character format specifier. %d The integer format specifier. %i The integer format specifier (same as %d). %f The floating-point format specifier. %e The scientific notation format specifier (note the lowercase e). %E The scientific notation format specifier (note the uppercase E).%g Uses %f or %e, whichever result is shorter. %G Uses %f or %E, whichever result is shorter. %o The unsigned octal format specifier. %s The string format specifier. %u The unsigned integer format specifier. %x The unsigned hexadecimal format specifier (note the lowercase x). %X The unsigned hexadecimal format specifier (note the uppercase X). %p Displays the corresponding argument that is a pointer. %n Records the number of characters written so far. % Outputs a percent sign (%).

Spring 2005, Gülcihan Özdemir Dağ Lecture 4, Page Switch-Case The nested if statements will become very complex if there are many decisions that need to be made, however. Sometimes, the programmer will have problems following the complex if statements. Fortunately there is another statement in C, the switch statement, that you can use to make unlimited decisions or choices based on the value of a conditional expression and specified cases. The general form of the switch statement is switch (expression) { case expression1: statement1; case expression2: statement2;... default: statement-default; }

Spring 2005, Gülcihan Özdemir Dağ Lecture 4, Page 10 Example: Using the switch statement #include main(){ int day; printf("Please enter a single digit for a day\n"); printf("(within the range of 1 to 3):\n"); day = getchar(); switch (day){ case `1': printf("Day 1\n"); case `2': printf("Day 2\n"); case `3': printf("Day 3\n"); default: ; } return 0; } /* End of main */

Spring 2005, Gülcihan Özdemir Dağ Lecture 4, Page 11 Use the switch-break statement #include main(){ int day; printf("Please enter a single digit for a day\n"); printf("(within the range of 1 to 7):\n"); day = getchar(); switch (day){ case '1': printf("Day 1 is Sunday.\n"); break; case '2': printf("Day 2 is Monday.\n"); break; case '3': printf("Day 3 is Tuesday.\n"); break; case '4': printf("Day 4 is Wednesday.\n"); break; case '5': printf("Day 5 is Thursday.\n"); break; case '6': printf("Day 6 is Friday.\n"); break; case '7': printf("Day 7 is Saturday.\n"); break; default: printf("The digit is not within the range of 1 to 7.\n"); break; } return 0; }

Spring 2005, Gülcihan Özdemir Dağ Lecture 4, Page Assignment Operators Assignment operators abbreviate assignment expressions c = c + 3; can be abbreviated as c += 3; using the addition assignment operator Statements of the form variable = variable operator expression; can be rewritten as variable operator= expression; Examples of other assignment operators: d -= 4 (d = d - 4) e *= 5 (e = e * 5) f /= 3 (f = f / 3) g %= 9 (g = g % 9)

Spring 2005, Gülcihan Özdemir Dağ Lecture 4, Page Increment and Decrement Operators Increment operator ( ++ ) –Can be used instead of c+=1 Decrement operator ( -- ) –Can be used instead of c-=1 Preincrement –Operator is used before the variable ( ++c or --c ) –Variable is changed before the expression it is in is evaluated Postincrement –Operator is used after the variable ( c++ or c-- ) –Expression executes before the variable is changed

Spring 2005, Gülcihan Özdemir Dağ Lecture 4, Page Increment and Decrement Operators If c equals 5, then printf( "%d", ++c ); –Prints 6 printf( "%d", c++ ); –Prints 5 –In either case, c now has the value of 6 When variable not in an expression –Pre-incrementing and post-incrementing have the same effect ++c; printf( “%d”, c ); –Has the same effect as c++; printf( “%d”, c );