Standard I/O Lesson Outline

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

Pointer Lesson 2 CS1313 Spring Pointer Lesson 2 Outline 1.Pointer Lesson 2 Outline 2.Pass by Reference Bad Example 3.Pass by Reference Good Example.
Constants Lesson CS1313 Spring Constants Lesson Outline 1.Constants Lesson Outline 2.What is a Constant? 3.The Difference Between a Variable and.
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.
Computer Programming w/ Eng. Applications
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.
Arithmetic Expressions Lesson #1 CS1313 Spring Arithmetic Expressions Lesson #1 Outline 1.Arithmetic Expressions Lesson #1 Outline 2.A Less Simple.
I/O with scanf()/printf() I/O stands for “Input/Output” In CSE 142, you will use: –scanf() (reading from the keyboard) –printf() for output (printing to.
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
C Introduction Lesson CS1313 Spring C Introduction Lesson Outline 1.C Introduction Lesson Outline 2. hello_world.c 3.C Character Set 4.C is Case.
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.
C Introduction Lesson CS1313 Spring C Introduction Lesson Outline 1.C Introduction Lesson Outline 2. hello_world.c 3.C Character Set 4.C is Case.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
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.
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.
LAB 2 : PROBLEM SOLVING TECHNIQUES, ALGORITHM: PSEUDO CODE AND FLOWCHART LAB INTRODUCTION Prepared by: Cik Noor Syazana bt Arshad.
Array Lesson 1 CS1313 Spring Array Lesson 1 Outline 1.Array Lesson 1 Outline 2.Mean of a List of Numbers 3.mean: Declarations 4.mean: Greeting,
Yu Yuanming CSCI2100B Data Structures Tutorial 3
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output.
Numeric Data Types Lesson CS1313 Spring Numeric Data Types Outline 1.Numeric Data Types Outline 2.Data Types 3.Integers in Mathematics 4.Integers.
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Array Lesson 2 CS1313 Spring Array Lesson 2 Outline 1.Array Lesson 2 Outline 2.Reading Array Values Using for Loop #1 3.Reading Array Values Using.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
C Programming Lecture 5 : Basic standard I/O Lecture notes : courtesy of Ohio Supercomputing Center, science and technolgy support.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 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.
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.
Asking the USER for values to use in a software 1 Input.
Variables & Constants Lesson 2 Outline
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Asking the USER for values to use in a software 1 Input.
Lecture 6: Output 1.Presenting results in a professional manner 2.semicolon, disp(), fprintf() 3.Placeholders 4.Special characters 5.Format-modifiers 1.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Arithmetic Expressions Lesson #2 Outline 1.Arithmetic Expressions Lesson #2 Outline 2.Named.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Fun fun ’til daddy takes your C turbo away! A C program is organized into one or more modules (source files created by a programmer), within which are.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Sudeshna Sarkar, IIT Kharagpur 1 I/O in C + Misc Lecture –
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Lecture2.
Lesson #2 Introduction to C.
Lesson #2 Introduction to C.
© 2016 Pearson Education, Ltd. All rights reserved.
I/O in C + Misc Lecture Sudeshna Sarkar, IIT Kharagpur.
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.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Lecture3.
CS150 Introduction to Computer Science 1
Arithmetic Expressions Lesson #2 Outline
Lesson #2 Introduction to C.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Introduction to C Programming
Arithmetic Expressions Lesson #1 Outline
Presentation transcript:

Standard I/O Lesson Outline Output via printf Placeholders Placeholders for Various Data Types Mixing Literal Text and Variables’ Values #1 Mixing Literal Text and Variables’ Values #2 Placeholder & Variable in Same Statement Placeholder/Variable Same Statement: Example Input via scanf Input via scanf: Ampersand Before Variable Input via scanf Example Input via scanf Example’s Flowchart Reading Multiple Variables with a Single scanf Multiple Variables per scanf Example #1 Multiple Variables per scanf Example #2 printf vs scanf Programming Exercise Standard I/O Lesson CS1313 Spring 2009

In C, we output to standard output using a printf statement: Output via printf In C, we output to standard output using a printf statement: printf("This will be output to stdout.\n"); A printf statement can output a string literal, but it can also output the value of a variable, a literal constant or a named constant: printf("%d", number_of_students); The statement above outputs to stdout (the terminal screen) the value of a variable named number_of_students of type int (presumably declared previously in the program that contains this printf statement). Standard I/O Lesson CS1313 Spring 2009

Placeholders printf("%d", number_of_students); The statement above outputs the value of a variable named number_of_students of type int (declared previously in the program that contains this printf statement). The %d is known as a placeholder: it holds the place of the value of the variable that we actually want to output. Standard I/O Lesson CS1313 Spring 2009

Placeholders for Various Data Types int: %d printf("%d", number_of_students); float: %f printf("%f", pi); char: %c printf("%c", middle_initial); Standard I/O Lesson CS1313 Spring 2009

Mixing Literal Text and Variables’ Values #1 We now know that we can output a string literal: printf("This will be output to stdout.\n"); We also know that we can output the value of a variable: printf("%d", number_of_students); Not surprisingly, we can mix and match the two: printf(" on your %d income.\n", tax_year); We can even mix and match while outputting the values of multiple variables of various data types: printf("The %d federal income tax on $%f\n", tax_year, income); Standard I/O Lesson CS1313 Spring 2009

Mixing Literal Text and Variables’ Values #2 We can mix and match literal text and variables’ values while outputting the values of multiple variables of various data types: printf("The %d federal income tax on $%f\n", tax_year, income); This statement means: Output to stdout (the terminal screen) the literal text "The ", and then the value of the int variable named tax_year, and then the literal text " federal income tax on $", and then the value of the float variable named income, and then a newline. Standard I/O Lesson CS1313 Spring 2009

Placeholder & Variable in Same Statement When you use a placeholder inside the string literal of a printf statement, the variable whose place is being held by the placeholder MUST MUST MUST be in the same printf statement as the placeholder. Putting the placeholder in one printf statement and the variable in a different printf statement is BAD BAD BAD! /* These printfs are GOOD GOOD GOOD! */ printf("f1=%f, ", f1); printf("i1=%d, GOOD!\n", i1); /* These printfs are BAD BAD BAD! */ printf("f2=%f, i2=%d, "); printf("BAD!\n", f2, i2); Standard I/O Lesson CS1313 Spring 2009

Placeholder/Variable Same Statement: Example % cat placeholder.c #include <stdio.h> int main () { /* main */ float f1, f2; int i1, i2; f1 = 3.75; f2 = 5.25; i1 = 6; i2 = 8; /* These printfs are GOOD GOOD GOOD! */ printf("f1=%f, ", f1); printf("i1=%d, GOOD!\n", i1); /* These printfs are BAD BAD BAD! */ printf("f2=%f, i2=%d, "); printf("BAD!\n", f2, i2); /* This printf is GOOD GOOD GOOD! */ printf("f2=%f, i2=%d, GOOD!\n", f2, i2); } /* main */ % gcc -o placeholder placeholder.c % placeholder f1=3.750000, i1=6, GOOD! f2=3.750000, i2=134513662, BAD! f2=5.250000, i2=8, GOOD! Standard I/O Lesson CS1313 Spring 2009

scanf("%d", &height_in_cm); Input via scanf The printf statement outputs to stdout (the terminal screen). Likewise, the scanf statement inputs from stdin (a user typing at the keyboard). The scanf statement has a somewhat strange syntax: scanf("%d", &height_in_cm); This statement says: input from stdin (a user typing at the keyboard) an int value and place it into the memory location associated with the int variable named height_in_cm. Standard I/O Lesson CS1313 Spring 2009

Input via scanf: Ampersand Before Variable The scanf statement has a somewhat strange syntax: scanf("%d", &height_in_cm); Notice the ampersand & before the name of the variable that you’re inputting into. For now, you must simply ACCEPT THIS ON FAITH. Standard I/O Lesson CS1313 Spring 2009

Input via scanf Example % cat read_variable.c #include <stdio.h> int main () { /* main */ int height_in_cm; printf("What’s my height in centimeters?\n"); scanf("%d", &height_in_cm); printf("My height is %d cm.\n", height_in_cm); } /* main */ % gcc -o read_variable read_variable.c % read_variable What’s my height in centimeters? 160 My height is 160 cm. Standard I/O Lesson CS1313 Spring 2009

Input via scanf Example’s Flowchart printf("What’s my height in centimeters?\n"); scanf("%d", &height_in_cm); printf("My height is %d cm.\n", height_in_cm); Start Prompt for height in cm. Input height in cm. Output height in cm. End Standard I/O Lesson CS1313 Spring 2009

Reading Multiple Variables with a Single scanf C allows inputting multiple variables per scanf statement. At runtime, when the user types in the input values, they can separate the individual input values by blank spaces, and/or by tabs, and/or by carriage returns (newlines). Blank spaces, tabs and carriage returns, as a group, are known as white space. Standard I/O Lesson CS1313 Spring 2009

Multiple Variables per scanf Example #1 #include <stdio.h> int main () { /* main */ float average_height_in_m; int number_of_silly_people, number_of_toys; char middle_initial; printf("How many silly people are there in CS1313,\n"); printf(" and what’s their average height in meters?\n"); scanf("%d %f", &number_of_silly_people, &average_height_in_m); printf("There are %d silly people\n", number_of_silly_people); printf(" with an average height of %f m.\n", average_height_in_m); printf("How many toys do I have, and\n"); printf(" what is my middle initial?\n"); scanf("%d %c", &number_of_toys, &middle_initial); printf("I have %d toys.\n", number_of_toys); printf("My middle initial is %c.\n", middle_initial); } /* main */ Standard I/O Lesson CS1313 Spring 2009

Multiple Variables per scanf Example #2 % gcc -o read_list read_list.c % read_list How many silly people are there in CS1313, and what’s their average height in meters? 7 1.75 There are 7 silly people with an average height of 1.750000 m. How many toys do I have, and what is my middle initial? 43 J I have 43 toys. My middle initial is J. Standard I/O Lesson CS1313 Spring 2009

printf vs scanf printf scanf outputs to stdout CAN (and typically does) contain literal text as well as placeholders typically DOES end with a newline variable names after the string literal CANNOT be preceded by & scanf inputs from stdin CANNOT contain literal text, other than spaces to separate the placeholders (which are REQUIRED) CANNOT contain a newline variable names after the string literal MUST be preceded by & Standard I/O Lesson CS1313 Spring 2009

Prompts the user for their age in years. Programming Exercise Create a program that: Greets the user. Prompts the user for their age in years. Inputs the user’s age in years. Outputs the user’s age in years. Begin by drawing a flowchart, and then write the program. The program does not have to have comments. The data type for the age variable must be appropriate. Standard I/O Lesson CS1313 Spring 2009