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.

Slides:



Advertisements
Similar presentations
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Advertisements

CSCI 171 Presentation 11 Pointers. Pointer Basics.
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
Introduction to C Programming CE Lecture 2 Basics of C programming.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
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.
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
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
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.
Basic Input - Output. Output functions  printf() – is a library function that displays information on-screen. The statement can display a simple text.
© 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.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
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.
Input & Output: Console
Yu Yuanming CSCI2100B Data Structures Tutorial 3
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester King Saud University College of Applied studies and Community Service Csc
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
CSCI 171 Presentation 2. Program Components main() #include Variable Definition Function Prototype Program Statements Function Call Function Definition.
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
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.
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.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
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.
Program Development Cycle 1.Edit program 2.Compile program - translates it from C to machine language 3. Run/execute your program. 4. If not satisfied,
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
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.
Lecture 3: Getting Started & Input / Output (I/O)
C Formatted Input/Output
Formatted Input and Output
User Interaction and Variables
Zhang Hongyi CSCI2100B Data Structures Tutorial 3
Formatted Input/Output
INC 161 , CPE 100 Computer Programming
TMF1414 Introduction to Programming
Introduction to C CSE 2031 Fall /3/ :33 AM.
Formatted Input/Output
Input and Output Lecture 4.
Formatted Input/Output
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Introduction to CS Your First C Programs
Chapter 4 Managing Input and Output Operations
Formatted Input/Output
Formatted Input/Output
CSCE 206 Lab Structured Programming in C
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Getting Started With Coding
Introduction to C Programming
Presentation transcript:

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 of x is 12 Two variables are passed to the printf function –the information inclosed in quotes –the name of the variable (x)

3 components of the format string printf(“\nThe value of x is %d”, x); Literal text - displayed exactly as it appears –The value of x is Escape sequence - \x (x is a specific char) –\n Conversion specifier - %x (x is a specific char) –tells printf how to interpret the variables –%d

Common Escape Sequences SequenceMeaning \aBeep \bBackspace \nNewline \tHorizontal tab \\Backslash

The printf conversion specifiers Specifier MeaningTypes %c Single Characterchar %d Signed integerint, short, long %f Floating point numberfloat, double %.xf Truncated floatfloat, double %s Character stringchar arrays %u Unsigned numberunsigned int, unsigned short unsigned long

Example Conversion Specifiers int x = 4, z = 5; float y = ; printf(“x = %d, y = %f \n”, x, y); printf(“y truncated to 2 places: %.2f \n”, y); printf(“x + z = %d”, (x + z)); Output: x = 4, y = y truncated to 2 places: 3.16 x + z = 9

Using the printf statement Remember to include stdio.h file –#include –Code Warrior automatically includes stdio.h in the main file Put one statement per printf –don’t overuse \n –remember to put \n at end if necessary

The puts( ) function Used to display text to the screen Can use escape sequences New line character automatically inserted at end of puts statement Must include stdio.h file Cannot use conversion specifiers –cannot display numeric variables

Using the puts ( ) function puts( ) is similar to printf( ) puts(“Hello World”); is equivalent to printf(“Hello World”); puts ( ) enters a carriage control at end puts(“Hello”); puts(“World”); Output: Hello World

The scanf ( ) function Allows user to enter numeric input Assigns data to one or more variables Uses same conversion specifiers as printf() Must include stdio.h file & is address-of operator (covered in CIS 131) scanf(“%d”, &x);

Example of scanf ( ) Sample uses of scanf( ) int x; float y; scanf(“%d”, &x); scanf(“%d%f”, &x, &y);

Data input scanf( ) users whitespace to separate input Example: All the following response to scanf(“%d%f”, &x, &y) will work as expected

Using the scanf ( ) function Do not forget to use the address-of operator (ampersand - &) Use printf( ) or puts( ) in conjunction with scanf( ) –clarifies to user what input is required