CSC 107 - Programming for Science Lecture 5: Actual Programming.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
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.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
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:
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include int main ( ) { printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n",
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).
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.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
 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.
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.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Numeric types of C Integers – Signed and unsigned – In a few different sizes – 209 Floating point – In different sizes –
CSC 107 – Programming For Science. The Week’s Goal.
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.
CSC Programming for Science Lecture 4: Beginning Programming.
PHY 107 – Programming For Science. The Week’s Goal.
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.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
CSC Programming for Science Lecture 26: Arrays.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
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.
CSC Programming for Science Lecture 5: Actual Programming.
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Chapter 2 - Introduction to C Programming
Input/output.
TMF1414 Introduction to Programming
Chapter 2 - Introduction to C Programming
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.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 2 - Introduction to C Programming
INPUT & OUTPUT scanf & printf.
Chapter 2 - Introduction to C Programming
Chapter 9 - Formatted Input/Output
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 2 - Introduction to C Programming
Chapter # 2 Part 2 Programs And data
Chapter 2 - Introduction to C Programming
Variables in C Topics Naming Variables Declaring Variables
Introduction to C Programming
Presentation transcript:

CSC Programming for Science Lecture 5: Actual Programming

Today’s Goal At the end of today’s lecture, you should be able to write a reasonable C program  This includes input & output!  We will have a real lab this week!

Variables Variables name memory location for program to store data  Variable’s initial value is unknown  Assignments update data stored at memory location  Variable’s value used whenever program uses variable

Data Types Each variable also has data type  Specifies how program treats variable’s value C defines 6 numeric data types  Integer types: short, int, long  Decimal types: float, double, long double  Does NOT specify ranges for each type char data type holds a character C will only allow certain assignments  Assign an integer to a decimal variable --- easy  Assign decimal to integer variable --- not easy

More Data Types Each numeric data type is actually 2 types  signed (default) and unsigned int i, j, k; unsigned double l, m, n; Unsigned data must be non-negative  Upper range of variable is doubled  Useful for some data: degreesKelvin, timeObservation, weight, homeworkScore C compiler emits warning when mixing signed & unsigned data

Printing Out Results C defines function to print information out  Function is called printf  Information is printed out in window where program is run  Arguments to printf determine what is printed printf’s arguments can be very esoteric  Do not memorize; this is why manuals exist

printf printf(“Hi, Mom\n”); First argument must be in quotes Except for a few magic commands, it prints out what you specify \n  print out newline \t  print out tab \\  print out “\” character \b  sound error bell

Even Better Magic Must tell printf how to format variables C will not complain for wrong specifier, but results are bad Variable TypeSpecifier int%i, %d short%hi, %hd long int%li, %ld unsigned int%u unsigned short%hu unsigned long%lu float%f, %e, %E, %g, %G double%lf, %le, %lE, %lg, %lG long double%Lf, %Le, %LE, %Lg, %LG char%c

Printing Out Data Identifier placed in first argument  Identifier replaced with value of variable, symbolic constant, or literal Values supplied in next arguments  Must be included in order identifiers appear caveat emptor when using function  Compiler does NOT check identifiers or argument  Too many can cause later problems  Too few arguments results in odd printouts

printf Examples double d; int i; unsigned int u; printf(“This is a boring example.\n”); printf(“Prints a double -- %f\n”, d); printf(“Prints 2 digits beyond decimal -- %.2f\n”, d); printf(“Prints an int -- %d\n”, i); printf(“Prints an unsigned int %u\n”, u); printf(“Prints i’s (%d) & u’s (%u) value”, i, u); printf(“i + u = %u\t i + d = %d\n”, i+u, i+d); printf(“Printing a percent sign is 100% hard”);

scanf C defines scanf to get user’s input  Very similar in usage to printf  Sets variables equal to values input on keyboard scanf waits until it has input  Wait could be years if nothing is input  Lines cannot be read until enter is pressed

Arguments to scanf First argument must be in quotes Must state type of data to be read  Uses same identifiers as printf Afterward specify variables to assign  Data types should match (otherwise program results will be odd)  Need to list “&” before each variable name

scanf Examples double d; int i; unsigned int u; scanf(“%u”, &u); scanf(“%f %d\n”, &d, &i); scan(“%f\n%d”, &d, &i); scan f (“%f%\n”, &d); Ampersand (&) before each variable name is vital  Needed to assign variable a new value

Your Turn Get into groups and complete daily activity

For Next Lecture Read through Section 2.6 of book  Do not need to understand all the details  But important knowing what is not understood Start homework assignment for week 3  Covers material from this week’s lectures