CSC 107 – Programming For Science. Today’s Goal  Learn C functions to input and output data  Using printf to print out variables and strings  Read.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

CSC Programming for Science Lecture 5: Actual Programming.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Introduction to C Programming
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
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.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
CSC 107 – Programming For Science. Today’s Goal  Learn how arrays normally used in real programs  Why a function returning an array causes bugs  How.
CSC 107 – Programming For Science. Today’s Goal ALL  Understand why ALL I/O is file I/O  Common bugs to avoid when coding with files in C++  Get a.
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.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CSC 107 – Programming For Science. The Week’s Goal.
CSC 107 – Programming For Science. Today’s Goal  Better understand arrays and how they work  Using array variable & its entries  When calling function,
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
CSC Programming for Science Lecture 36: Structures.
PHY 107 – Programming For Science. The Week’s Goal.
Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
CSC Programming for Science Lecture 8: Character Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
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.
CSC Programming for Science Lecture 27: Arrays as Parameters, Searching, & Sorting.
CSC 107 – Programming For Science. Today’s Goal  Discover best uses of structures in a program  How we can mix pointers inside structures  Assigning.
CSC Programming for Science Lecture 18: More Data Files.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
PHY 107 – Programming For Science. Final Exam  Wed., Dec. 16 th from 2:45PM – 4:45PM in OM110  For exam, plan on using full 2 hours  If major problem,
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
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.
CSC Programming for Science Lecture 5: Actual Programming.
Input and Output: I/O Finish reading chapters 1 and 2 of the text
Chapter 2 - Introduction to C Programming
Input/output.
Revision Lecture
Chapter 2 Overview of C.
Chapter 3: I/O Management
Chapter 2 - Introduction to C Programming
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.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
INPUT & OUTPUT scanf & printf.
Chapter 2 - Introduction to C Programming
Format String.
Chapter 2 - Introduction to C Programming
Conversion Check your class notes and given examples at class.
Chapter 2 - Introduction to C Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Introduction to C Programming
Presentation transcript:

CSC 107 – Programming For Science

Today’s Goal  Learn C functions to input and output data  Using printf to print out variables and strings  Read in values typed in using scanf

Variables  Variable gives name to location to store data  Memory location's initial value is unknown  Assignments update memory location with new value  Memory location updated by assignment ONLY  When variable is used in program…  …uses current value at that memory location  Just about everything (interesting) uses variables

printf Print Results w/ printf  Cannot use cin & cout with standard C code  Must instead use the C functions for these actions  printf  printf used to print out data in C program  Like cout, prints results in window where it is run  printf 's arguments determine what is printed  Arguments to printf extremely esoteric  This is why manuals exist; do not memorize

cout vs. printf coutprintf  Simple  Easy-to-use  Clear  Limited need to memorize  Fun  Elegant  Complex  Lots of knobs & buttons  Obscure  Lots to look up  Hateful  Ugly

cout vs. printf coutprintf

Even Better Magic  Must tell printf how to format variables  No error on wrong specifier, just printed wrong Variable TypeSpecifier int%i, %d short%hi, %hd long%li, %ld float%f, %e, %E, %g, %G double%lf, %le, %lE, %lg, %lG long double%Lf, %Le, %LE, %Lg, %LG char%c cString%s

Printing Out Data  1 st argument includes all specifiers  Actual value replaces specifier when printed out  The first argument also can (should) include text  Values supplied in next arguments to the function  Values may include literals, expressions, or variables  Must appear in order of specifiers from the first argument

Printing Out Data  Beware when using function  There will be NO checks on identifiers or argument  Problems later if too many arguments  Printing shows really odd results if too few arguments Nothing forces types to match specifier

printf Examples #include int main() { double d = 1.22; int i = -2; unsigned int u = 7; printf(“This is a boring example.\n”); printf(“Double -- %lf\n”, d); printf(“2 digits after -- %.2lf\n”, d); printf(“Int -- %d\n”, i); printf("%d", i); printf(“Unsigned int -- %u\n”, u); printf(“%d & %u”, i, u); printf(“%u\t %d\n”, i + u, i + d); printf(“100% proof”);

scanf  scanf gets user’s input when working in C  Function that resembles usage of printf  Behaves like cin in C++, but works very differently

cin vs. scanf cinscanf

Arguments to scanf  Must use quotes for 1 st argument  Only for first argument & not the others, however  Use identifiers to state type of data to be read  printf 's identifiers used also in scanf  State variables to assign in next arguments  Data types must match, but no error if they do not  Uses pass-by-pointer, so must pass variable's address

scanf Examples

Will It Compile & Work?

Your Turn  Get into your groups and try this assignment

For Next Lecture  Read Section 16.4 on macros for Monday  How can we write simple, fast little functions?  What is use of #define statements anyway?  What is the purpose of writing a macro function?  Angel has Weekly Assignment #14 due Tuesday  Last programming assignment  Last programming assignment due in 1 week