Formatted Input/Output

Slides:



Advertisements
Similar presentations
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Advertisements

Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Declarations/Data Types/Statements. Assignments Due – Homework 1 Reading – Chapter 2 – Lab 1 – due Monday.
1 CSE1301 Computer Programming: Lecture 9 Input/Output.
Basic Input/Output and Variables Ethan Cerami New York
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester King Saud University College of Applied studies and Community Service Csc
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
CSCI 130 Basic Input and Output Chapter 9. The printf ( ) function Printf(“\nThe value of x is %d”, x); Displayed to screen (assume x = 12): –The value.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
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
CSCE 206 Structured Programming in C
Formatted Input and Output
Zhang Hongyi CSCI2100B Data Structures Tutorial 3
Strings (Continued) Chapter 13
Chapter 9 C Formatted Input/Output
Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
Input/output.
Chapter 2 Overview of C.
Chapter 3: I/O Management
ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 18 I/O in C.
Introduction to C CSE 2031 Fall /3/ :33 AM.
Formatted Input/Output
OUTPUT STATEMENTS GC 201.
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.
Formatted Input/Output
CSI 121 Structured Programming Language Lecture 7: Input/Output
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 2 - Introduction to C Programming
Formatted Input/Output
Chapter 2 - Introduction to C Programming
INPUT & OUTPUT scanf & printf.
CSCE 206 Lab Structured Programming in C
Chapter 2 - Introduction to C Programming
A First Book of ANSI C Fourth Edition
Chapter 8 The Loops By: Mr. Baha Hanene.
Chapter 2 - Introduction to C Programming
Chapter 4 Managing Input and Output Operations
Formatted Input/Output
Formatted Input/Output
Formatted Input/Output
Chapter 2 - Introduction to C Programming
EECE.2160 ECE Application Programming
Introduction to C EECS May 2019.
CSCE 206 Lab Structured Programming in C
Introduction to C Programming
CSCE 206 Lab Structured Programming in C
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Getting Started With Coding
Presentation transcript:

Formatted Input/Output Chapter 3 Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved.

Escape Sequences The \n code that used in format strings is called an escape sequence. Escape sequences enable strings to contain nonprinting (control) characters and characters that have a special meaning (such as "). A partial list of escape sequences: Alert (bell) \a Backspace \b New line \n Horizontal tab \t Copyright © 2008 W. W. Norton & Company. All rights reserved.

Escape Sequences A string may contain any number of escape sequences: printf("Item\tUnit\tPurchase\n\tPrice\tDate\n"); Executing this statement prints a two-line heading: Item Unit Purchase Price Date Copyright © 2008 W. W. Norton & Company. All rights reserved.

Escape Sequences Another common escape sequence is \", which represents the " character: printf("\"Hello!\""); /* prints "Hello!" */ To print a single \ character, put two \ characters in the string: printf("\\"); /* prints one \ character */ Copyright © 2008 W. W. Norton & Company. All rights reserved.

Specifiers http://www.cplusplus.com/reference/cstdio/printf/ specifier Output Example d or i Signed decimal integer 392 f Decimal floating point, lowercase 392.65 e Scientific notation (mantissa/exponent), lowercase 3.9265e+2 E Scientific notation (mantissa/exponent), uppercase 3.9265E+2 c Character a s String of characters sample p Pointer address b8000000 Copyright © 2008 W. W. Norton & Company. All rights reserved.

Confusing printf with scanf Incorrectly assuming that scanf format strings should resemble printf format strings is another common error. Consider the following call of scanf: scanf("%d, %d", &i, &j); scanf will first look for an integer in the input, which it stores in the variable i. scanf will then try to match a comma with the next input character. If the next input character is a space, not a comma, scanf will terminate without reading a value for j. Copyright © 2008 W. W. Norton & Company. All rights reserved.

Program: Adding Fractions The addfrac.c program prompts the user to enter two fractions and then displays their sum. Sample program output: Enter first fraction: 5/6 Enter second fraction: 3/4 The sum is 38/24 Copyright © 2008 W. W. Norton & Company. All rights reserved.

addfrac.c /* Adds two fractions */ #include <stdio.h>   #include <stdio.h> int main(void) { int num1, denom1, num2, denom2, result_num, result_denom; printf("Enter first fraction: "); scanf("%d/%d", &num1, &denom1); printf("Enter second fraction: "); scanf("%d/%d", &num2, &denom2); result_num = num1 * denom2 + num2 *denom1; result_denom = denom1 * denom2; printf("The sum is %d/%d\n",result_num, result_denom) return 0; } Copyright © 2008 W. W. Norton & Company. All rights reserved.