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.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

1 A Simple C Program /* Take a number multiply it by 10 and display it */ #include main() { int number, result; printf("Type in a number \n"); scanf("%d",
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Standard I/O Lesson Outline
Standard I/O Lesson CS1313 Spring Standard I/O Lesson Outline 1.Standard I/O Lesson Outline 2.Output via printf 3.Placeholders 4.Placeholders for.
Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
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.
Topic 2 – Introduction to the C Programming Language.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
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.
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.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Basic Input/Output and Variables Ethan Cerami New York
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
© 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.
1 IPC144 Session 11 The C Programming Language. 2 Objectives To format a #define statement correctly To use a #define statement in a C program To construct.
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 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
PHYS 2020 Basic C An introduction to writing simple but useful programs in C In these lectures I will take you through the basics of C, but you will need.
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.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
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.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
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.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
+ 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.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
C Formatted Input/Output
ICS103 Programming in C Lecture 3: Introduction to C (2)
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.
Engr 0012 (04-1) LecNotes
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.
Chapter 2 - Introduction to C Programming
INPUT & OUTPUT scanf & printf.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Conversion Check your class notes and given examples at class.
Lesson #2 Introduction to C.
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Presentation transcript:

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 file that contains functions needed for input and output operations.  #include  Includes the necessary printf and scanf functions.  The printf function is used to output text to the screen.  The scanf function is used to get input from the user.

 The printf function  Syntax: printf (format string);  Example: printf (“This is a line of text”);  The format string is enclosed by double quotes “” and may contain any literal text, placeholders, and format control characters.  Literal text is interpreted literally by the compiler and doesn’t cause the program to perform any action.  Format control characters cause the program to perform and action like moving to the next line. i.Example: printf (“Move to the next line \n”); Format control character to create a new line.

 The printf function continued…  The printf function can also take another form, which is used to display the contents of one or more variables.  Syntax: printf (format string, output list);  The output list consists of a list of variables or values that will replace the corresponding placeholder in the format string.  A placeholder always begins with the symbol % and is used to mark the display position for a variable’s value.  Example: printf (“The distance traveled is %d”, kms); printf (“The distance traveled is %d and cost of trip is %d”, kms, cost); Placeholder character to insert the value of variable kms.

 The scanf function  Syntax: scanf (format string, input list);  Example: scanf (“%d”, &miles);  The format string is enclosed by double quotes “” and contains the data types of the input to read in. You need to use format specifiers to specify the data types.  The input list is a list of variables that will get values. The order of the variables in this list must correspond with the order of the format specifiers in the format string. i.The first variable in the input list will get the value of the first format specifier in the format string, the second variable in the input list gets the value of the second format specifier in the format string, etc. ii.Each variable must be declared before using them in the input list and you must put the ampersand (&) character in front of the variable’s name.

 Placeholders – Format Specifiers %c – placeholder used for a char in printf and scanf. %d – placeholder used for an int in printf and scanf. %f – placeholder used for a double in printf. %lf – placeholder used for a long double in printf and scanf.  Special Characters – Format Control Characters \n – used to create a newline. \t – used to add a tab space to the output line. \\ – used to display a backslash in the output. \” – used to display a double quote output.

#include int main ( ) { int quantity; double price; printf (“Enter the quantity and price > “); scanf (“%d%lf”, &quantity, &price); return (0); }

#include int main ( ) { char first; char middle; char last; printf (“Enter the initials for your first, middle, and last names > “); scanf (“%c%c%c”, &first, &middle, &last); printf(“Hello there %c%c%c and welcome to this program.”, first, middle, last); return (0); }

 In C, the output of all numbers will be in their normal notation unless you specify the format.  Format specifiers can have modifiers to control the total number of columns and decimal places to display.  Syntax: field_width.decimal_places  Example: %5.2f%5.2f %8.3f%8.3f %5d%5d

 Example 1: printf (“%6.2f”, ); will display in 6 columns with 2 decimal places and 1 decimal point as ##4.97  Example 2: printf (“%.1f”, ); will display to 1 decimal place and 1 decimal point as 5.0  Example 3: printf (“%5.1f”, 25.3); will display in 5 columns with 1 decimal places and 1 decimal point as #25.3

 Formatting Values of Type int: printf (“ The average of all exams is %5d”, average); ValueFormatOutput Display 234%5d %5d -234 %6d -234 %1d-234 Note: C will expand the field width if it is too small for the integer value. *** From Table 2.11 on page 85 in our textbook.

 Formatting Values of Type double: printf (“ The average of all exams is %5.2f”, average); ValueFormatOutput Display %5.2f %4.2f %3.1f %5.1f % %.4f *** See Table 2.13 on page 87 in our textbook.