+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.

Slides:



Advertisements
Similar presentations
printf() Documentation info:
Advertisements

Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2013 Lecture 6: printf() formatting.
The Print Formatting Statement … named printf. 2 Introduction to printf statements print and println statements don’t allow us to easily format output.
CS1061 C Programming Lecture 16: Formatted I/0 A. O’Riordan, 2004.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
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()
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.
Declarations/Data Types/Statements. Assignments Due – Homework 1 Reading – Chapter 2 – Lab 1 – due Monday.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
 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",
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
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.
11 Chapter 3 DECISION STRUCTURES CONT’D. 22 FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS We can use the DecimalFormat class to control.
 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 Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
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.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
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.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
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.
DATA TYPE AND DISPLAY Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Basic I/O in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Stream Model of I/O header file: A stream provides a connection.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
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.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
1 float Data Type Data type that can hold numbers with decimal values – e.g. 3.14, 98.6 Floats can be used to represent many values: –Money (but see warning.
Numbers in ‘C’ Two general categories: Integers Floats
Conversion Check your class notes and given examples at class.
Formatted Input and Output
Lesson #2 Introduction to C.
Lesson #2 Introduction to C.
ECE Application Programming
INC 161 , CPE 100 Computer Programming
Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA
TMF1414 Introduction to Programming
ICS103 Programming in C Lecture 3: Introduction to C (2)
Input and Output Lecture 4.
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
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.
Formatted Input/Output
INPUT & OUTPUT scanf & printf.
Conversion Check your class notes and given examples at class.
Formatted Input/Output
Conversion Check your class notes and given examples at class.
Lesson #2 Introduction to C.
WEEK-2.
Formatted Input/Output
Presentation transcript:

+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on your machine, you may need 1 byte for characters, 2 bytes for integers and 4 bytes for float. You also know that integers are in fact a subset of float & as such you may decide to use float type for all numerical values. Although this is mathematically correct however you would not have an efficient memory management here. You could have used 2-byte for integers but you used 4-byte instead! 1

+ Placeholders in I/O Statements Placeholders (or conversion specifiers) will substitute the value of the variable inside the output string (printf). You must absolutely match the placeholder with the variable type. %lf: long floating point (double). %d: decimal (int). %c: character (char). \n: a special character meaning that a new line will be inserted. 2

+ Integer placeholders %d is the default integer placeholder. When used it will simply display the value as is without any padding. To add padding, to have columns for example, we need formatted placeholders. %nd will reserve n places to display the number. Justification will be to the right. The negative sign takes one place. If the value is 17 and %4d is used, then it will display 2 spaces followed by 17 on the screen. __17 These are spaces, not underscores. 3

+ Integer placeholders  The number is always displayed in its entirety, even when the format is too narrow. With and a %3d placeholder, you would see -1234, therefore using 5 spaces instead of the 3 requested.  A negative number change the justification to the left of the field. With a value of and a %-8d placeholder, you will get -1234___. 3 trailing blanks 4

+ Double placeholders  By default the %lf (or %f) placeholder displays the number with 6 decimal digits and no padding (may vary depending of computer system).  The formatted double placeholder has this format:  %w.dlf, where w is the total width of the field (including sign and decimal point) and d the number of decimal digits.  If the value is 4.56 and the placeholder is %6.3lf  then the display will be _ leading blank 5

+ Double placeholders  With a double formatted, you always get the requested number of decimal digits (even if the field is not wide enough).  You also always (like the integer placeholder) get all the significant numbers.  However, if there are more decimal precision in the value than in the placeholder, the value is truncated and rounded- up if need be. 6

+ Double placeholders If the value is and the placeholder is %7.3lf, then you will get 3 decimal digits as requested plus the significant numbers: If the value is and the placeholder is %8.3lf, then you will get 3 decimal digits as requested plus the significant numbers and padding: _ See the rounding-up effect. A %8.7lf for a value of will produce a display of Note: The internal value is unaffected by the placeholder, only its screen appearance. 7

+ Double placeholders By default the %lf (or %f) placeholder displays the number with 6 decimal digits and no padding (may vary depending of computer system). The value is 5.87 and the placeholder is %6.3lf then the display will be b5.870 If the value is and the placeholder is %7.3lf,then you will have 3 decimal digits and the display will be: If the value is and the placeholder is %8.3lf,then you will have: b ROUNDING-UP effect. Also you could have TRUNCATE. A %8.7lf will produce: Note!!!!!! The internal value is UNAFFECTED by the placeholder, only its screen appearance.

+ PLEASE CHECK CLASS EXAMPLES FOR PLACEHOLDERS 9

+ Simple Assignment Operator (=) o Assign a value to a variable o Does not mean equality, it means assignment Var1='a'; /* Var1  a */ Var2=15; /* Var2  15 */ Var3=27.62; /* Var3  */ 10

+ Initializing Variable Giving a value to the variable at the time that the variable is declared. char Var1='X'; int Var2=1095; float Var3= ; 11

+ Scanf() Another way to fill a variable is to ask the user for its value. We do that with the scanf statement. printf(“Enter your score:”); Scanf(“%d”, & score); 12

+ /* The = operator puts the value on the right */ /* into the variable on the left */  #include  int main (void){ /* declarations */  int a, b, c, d, e;  a = 10; /* fill variable a */  a = 22; /* modify variable a few times*/  a = 10 + a;  a = a + a + 2;  a = 4 + a;  b = a; /* a few more assignments */  c = b = 7;  c = 10 + a;  d = a + a + 2;  e = 22 + a;  a = a - b + c; /* the final values are... */  printf ("a:%4d\n b:%4d\n c:%4d\n d:%4d\n e:%4d\n", a, b, c, d, e); return (0); } 13

+ Example Programs Visit this website for more c.ihypress.ca 14

+ /* Numeric Placeholders */ #include int main (void) { /* declarations */ int a; double x; /* executable statements */ a = 1000; x = ; printf ("%d\n", a); printf ("%3d\n", a); printf ("%4d\n", a); printf ("%5d\n", a); printf ("\n"); printf ("%f\n", x); printf ("%15f\n", x); printf ("%15.4f\n", x); printf ("%18.2f\n", x); printf ("%12.0f\n", x); return (0); }

+ Answer

Format specifiers for PRINTF( ) functions SpecifierPurpose %d, %iSigned decimal integer %fSigned floating point %lfSigned double %eSigned floating point using e notation %cA single character %sStrings 17

+