CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

Size: px
Start display at page:

Download "CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program"

Transcription

1 CMPT 102 Introduction to Scientific Computer Programming Input and Output Janice Regan, CMPT 102, Sept Your first program /* My first C program */ /* make the computer print the string Hello world */ /* connect to any necessary libraries */ #include <stdio.h> /* the main function, implements the algorithm to solve the problem*/ /* the main function may use functions from included libraries */ int main ( ) { printf("hello, world!\n"); } Janice Regan, CMPT 102, Sept

2 General form for printf printf( format string, arg1, arg2,.); The format string (also called a control string) may contain text to be printed and conversion fields (instruction on how to print the values) The control string should have one conversion field for each argument to be printed. If there are too many conversion fields they are printed with unpredictable results If there are not enough conversion fields, the variables without conversion fields will be ignored Janice Regan, CMPT 102, Sept printf: Writing results The format string contains text and conversions Text includes explanations you wish to include in your output, punctuation, and spaces Conversions are instructions giving the format to print each variable in the print statement. The contents of your format string will be printed on the output line Conversion fields will be replaced with the values of the indicated variables using the specified conversion. The conversion type is should be of the same type as the variable Janice Regan, CMPT 102, Sept

3 Example of printf statement /* Print the sum. */ printf("the sum is %12.2f ", sum); The %12.2f is the conversion, all other characters (including blanks) between the " " will be printed directly to the output line The %12.2f will be replaced by the value of the variable sum when it prints to the output line The variable sum will be printed with 2 digits after the decimal place, and using a total of at least 12 characters including the decimal place. If less than 12 digits (9digits decimal 2 digits) are needed the extra digits will be replaced by blanks Janice Regan, CMPT 102, Sept Using the printf statement /* Print the sum. */ printf("the sum is %12.2f ", sum); To print (number preceded by 6 blank spaces) To print (number preceded by 3 blank spaces) To print 4.58 * (no preceding blank spaces) If it requires more digits to print the number than is provided by the conversion the number will be printed in the smallest number of digits possible consistent with the conversion, even if this number is larger than the value specified Janice Regan, CMPT 102, Sept

4 Conversions for printf: 1 A % to indicate the start of the conversion field An optional flag field which may contain left justify + prepend + or space negative numbers printed with a preceding positive numbers printed with a preceding blank ) An optional number indicating minimum field width to be printed (minimum # of characters reserved) An optional period separating the field width from the precision ( not present for integer types) A optional number indicating the precision or the number of digits displayed after the decimal in a floating point number Janice Regan, CMPT 102, Sept Conversions for printf: 2 An h or an l indicating short or long A conversion character d, i, u for integers and unsigned integers f for doubles and floats, c for characters e for exponential format floating point number g for general format, if exponent is <-4 or greater than the precision uses e otherwise uses f Note: to print a % to your output you must put %% in your format statement Janice Regan, CMPT 102, Sept

5 Special characters in printf statements There are other strings of characters, called escape sequences, you can place within your format statement to produce particular results \n newline: move to the next output line \b backspace: move back one space before printing the next character \t horizontal tab \v vertical tab \\ print a \ \? Print a? \" print a \ print a Note that a format statement should not contain the character produced by typing <enter> Janice Regan, CMPT 102, Sept Printing Integers: Examples: 1 int int1 = 2468, int2 = , int3 = -75; long int int4 = L; printf( "%d\n", int1); printf( "%d%d\n", int1, int2); printf( "%+d, %d, %-d, %-d\n", int1, int2, int3, int1); printf( "%8d, % d,\n %8d, %d\n", int1, int2, int3, int4); , , -75, , , -75, Janice Regan, CMPT 102, Sept

6 Printing Integers: Examples: 2 Things to note from Printing Integers: Examples: 1 When there is no space between conversions, there are no spaces between the printed outputs. This is why second line of output looks like one number instead of two Anything you wish to appear in the output that is not the value of a variable must appear in the format statement The number of variables in the variable list must match the number of conversions in the format statement When we specify a minimum field width (like the 8 in the last printf statement) remember that the sign counts as a digit. When you want a new output line use a \n after the last thing (character or conversion) you want on the present line If you put a space after \n you will get a space at the beginning of the next line Remember the quotation marks around the format statement Janice Regan, CMPT 102, Sept Printing Integers: Examples: 3 unsigned int int2 = U, int1 = U; long int int3=-4567l, int4 = L; printf( "%i, %i\n", int1, int2); printf( "%u, %u\n", int1, int2); printf( "%12d, %7d, %10d\n", int3, int3, int4); printf( "%-12d, %+7d, %+10d\n", int3, int3, int4); printf( "% 12d, % 7d, % 10d\n", int3, int3, int4); , , , -4567, , -4567, , -4567, Janice Regan, CMPT 102, Sept

7 Printing Integers: Examples: 4 More things to note Printing from Examples %d and %i both print signed integers %u should be used to print unsigned integers. If %d or %i is used to print an unsigned integer then the correct integer may not be displayed If the unsigned integer is small enough to be represented as a signed integer of the same length the displayed integer will be the same ( as for in the example) If the unsigned integer is too large, for example the integer in the example, then the number printed will not be the value of the unsigned integer An integer that is too large has a 1 in the most significant binary bit, the sign bit for a signed integer. The integer is displayed as a negative number (sign bit 1) rather than the larger unsigned integer. Janice Regan, CMPT 102, Sept Printing Integers: Examples: 5 More things to note Printing from Examples The minimum width of a field can be specified The 7 in %7d A field 7 characters wide will be used to print the integer, the field will be padded (on the left) with blanks if necessary. The field may be wider than 7 characters if the number requires it Numbers can be left justified. The in %-12d This means any padding with blanks happens on the right rather than on the left. The number is printed with the first digit in the leftmost position You can choose to display + on a positive number and on a negative number (Use the + in %+7d or in %+12d ) You can leave a space for a sign, but display it only if it -. (Use the space as in % 7d ) Janice Regan, CMPT 102, Sept

8 Printing float and double variables float flt1 = 1.001, flt2 = , flt3 = -4.5E12; float flt4 = 66.54E-08; printf( "%f, %f, %f\n", flt1, flt2, flt3); printf( "%5.3f, %9.3f\n", flt1, flt2); printf( "%e, %+e, \n %8.4e, %+12.3e\n", flt3, flt4, flt3, flt4); printf( "%g, %g, %g, %g\n", flt1, flt2, flt3, flt4); , , , e+12, e-07, e+12, e , , -4.5e+12, 6.654e-07 Janice Regan, CMPT 102, Sept Printing float and double variables 2 Float and double variables can be printed as decimal numbers mmmm.pppppp using the %f conversion the precision or the number of digits after the decimal point can be specified. If it is not specified it will default to 6. The.3 in %5.3f (3 digits follow the decimal point) is an example of specifying the precision The width of the field includes the decimal point and sign Float and double variables can also be printed in exponential form, mmmm.ppppppe±kk The precision works the same as for the decimal format The.4 in %8.4e (4 digits follow the decimal point) is an example of specifying the precision The width of the field includes the decimal point, the E, the sign and the sign and digits of the exponent Janice Regan, CMPT 102, Sept

9 Printing Long Double Numbers long double flt5 = L; printf( "%Lf\n ", flt5); printf( "%f\n ", flt5); printf( "%Le\n", flt5); printf( "%e\n", flt5); e e-18 l Janice Regan, CMPT 102, Sept Print functions printf( format string, arg1, arg2, ); Prints to the standard output fprintf( file, format string, arg1, arg2,.); Prints to a file sprintf( buff, format string, arg1, arg2,.); Prints to a buffer Janice Regan, CMPT 102, Sept

10 General form for printf printf( format string, arg1, arg2,.); The format string (also called a control string) may contain text to be printed and conversion fields (instruction on how to print the values) The control string should have one conversion field for each argument to be printed. If there are too many conversion fields they are printed with unpredictable results If there are not enough conversion fields, the variables without conversion fields will be ignored Janice Regan, CMPT 102, Sept Sending your output to a file printf sends your output to the standard output device, usually the monitor screen There are two approaches to sending output to a file Redirection of the output Writing output directly to the file using fprintf The second approach gives more flexibility You can write part of the output to a file and part to your screen (output to file, prompts to screen) You can write different parts of your output to different files (Summary output to one file, detailed output to another) fprintf requires that you identify the file the data is to be written into Janice Regan, CMPT 102, Sept

11 Redirection of output All information that would normally be written to the standard output (screen) will be written into file outfile instead./myprog > outfile File outfile will be created if it does not exist, then the output will be written into file outfile The command will fail if outfile already exists./myprog >> outfile File outfile will be created if it does not exist, then the output will be written into file outfile If file outfile already exists the output will be appended to the end of file outfile Janice Regan, CMPT 102, Sept General form for printf fprintf( filep, format string, arg1, arg2,.); The format string (also called a control string) may contain text to be printed and conversion fields (instruction on how to print the values) The control string should have one conversion field for each argument to be printed. The filep identifies the file into which the data is to be written The file pointer, filep, has a special type. FILE The file pointer provides a reference to the desired file To associate the file pointer with the file, the file must be opened Janice Regan, CMPT 102, Sept

12 The fopen function To open any file we need to know If we will be reading from the file " r" If we will be writing to the file (overwriting existing contents of the file) "w" If we will be appending our output to the end of an existing file. "a The name (filepath) of the file we wish to ope filep = fopen("filename", "w") The value of the variable filep is of type FILE *, a reference to a file. The variable filep is used to reference the file once it has been opened. Janice Regan, CMPT 102, Sept Using the fprintf statement: 1 When you declare variables you must also declare a variable of type FILE which will be used to reference the file within your program FILE * DataFileExp1 Before you can read data from a file you must open it using fopen DataFileExp1 = fopen("filename", "w"); This connects the variable DataFileExp1, known as a file pointer (it points to or references the file) to the file with name filename Janice Regan, CMPT 102, Sept

13 Using the fprintf statement: 2 Once the file is opened you can read data fprintf(datafileexp1, format string, arg1, ); This will write arguments arg1, (with the format specified in the format string) to the file pointed to by DataFileExp1 (filename). When you are finished writing to the file you must close the file filename fclose(datafileexp1); This assures that the last of your data is written into the file (not left in a buffer) Janice Regan, CMPT 102, Sept Precision of representations in C short maximum: int maximum: long maximum: float precision digits: 6 float maximum exponent: 38 float maximum: e+38 double precision digits: 15 double maximum exponent: 308 double maximum: e+308 long precision digits: 18 long maximum exponent: 4932 long maximum: e+4932 For the gcc compiler in cygwin Janice Regan, CMPT 102, Sept

14 General form for scanf scanf( format string, &arg1, &arg2,.); The format string contains characters and conversion fields for the variables to be read The format string should have one conversion field for each argument to be read. & is the unary address operator, it tells the computer to print the value read at the address reserved for the variable Janice Regan, CMPT 102, Sept scanf: Reading results The format string contains text and conversions Text (not blanks or tabs or newlines) must match text in the input stream Conversions are instructions giving the format to read each variable in the read statement. The contents of your format string will be read from the standard input (usually the keyboard) scanf continues reading until all conversions in the format string have been used or until an end of file is received, or until a conflict occurs ( the next character does not match the identifier or the expected text) Janice Regan, CMPT 102, Sept

15 Conversions for scanf: 1 A % to indicate the start of the conversion field An optional assignment suppression character An optional number indicating maximum field width to be printed An h or an l indicating short or long A conversion character d, i, u for integers and unsigned integers e, f, g for doubles and float, c for characters, s for a string of characters Janice Regan, CMPT 102, Sept scanf example Consider the input data stream: First test, the 1st input is 3.65, the 2nd is 234, the last is 1.234e-05 This can be read using scanf in the following ways: scanf( First a test, the 1st input is %4.2f, the 2nd is %3d, the last is %9e, &flvar1, &intvar2, &expvar3); scanf( %*s %*s %*s %*s %*s %*s %4f %*s %*s %*s %3d %*s %*s %*s %9e, &flvar1, &intvar2, &expvar3); scanf( %*31c %f %*11c %3d %12c %9e, &flvar1, &intvar2); Janice Regan, CMPT 102, Sept

16 read functions scanf( format string, arg1, arg2,.); Reads from the standard input Usually the keyboard fscanf( file, format string, arg1, arg2,.); Reads from a file sscanf( buff, format string, arg1, arg2,.); Reads from a buffer Janice Regan, CMPT 102, Sept General form for fscanf fscanf(filep, format string, &arg1, &arg2,...); The filep ( type FILE) references or points to the file being read from The format string contains characters and conversion fields for the variables to be read The format string should have one conversion field for each argument to be read. & is the unary address operator, it tells the computer to print the value read at the address reserved for the variable Janice Regan, CMPT 102, Sept

17 Using the fscanf statement: 1 When you declare variables you must also declare a variable of type FILE which will be used to reference the file within your program FILE * DataFileExp1 Before you can read data from a file you must open it using fopen DataFileExp1 = fopen("filename", r"); This connects the variable DataFileExp1, known as a file pointer (it points to or references the file) to the file with name filename Janice Regan, CMPT 102, Sept Using the fscanf statement: 2 Once the file is opened you can read data fscanf(datafileexp1, format string, arg1, ); This will write arguments arg1, (with the format specified in the format string) to the file pointed to by DataFileExp1 (filename). When DataFileExp1 == NULL you have reached the end of the file. You can test for end of file using Flag = feof( DataFileExp1); If Flag == 1 you are at the end of the file When you are finished writing to the file you must close the file filename fclose(datafileexp1); Janice Regan, CMPT 102, Sept

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 6/e 1992-2010 by Pearson Education, Inc. An important part of the solution to any problem is the presentation of the results. In this chapter, we discuss in depth the formatting features

More information

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 07: Data Input and Output Readings: Chapter 4 Input /Output Operations A program needs

More information

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code CS102: Standard I/O Our next topic is standard input and standard output in C. The adjective "standard" when applied to "input" or "output" could be interpreted to mean "default". Typically, standard output

More information

Advanced C Programming Topics

Advanced C Programming Topics Introductory Medical Device Prototyping Advanced C Programming Topics, http://saliterman.umn.edu/ Department of Biomedical Engineering, University of Minnesota Operations on Bits 1. Recall there are 8

More information

Standard File Pointers

Standard File Pointers 1 Programming in C Standard File Pointers Assigned to console unless redirected Standard input = stdin Used by scan function Can be redirected: cmd < input-file Standard output = stdout Used by printf

More information

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 18 I/O in C Standard C Library I/O commands are not included as part of the C language. Instead, they are part of the Standard C Library. A collection of functions and macros that must be implemented

More information

Input/Output: Advanced Concepts

Input/Output: Advanced Concepts Input/Output: Advanced Concepts CSE 130: Introduction to Programming in C Stony Brook University Related reading: Kelley/Pohl 1.9, 11.1 11.7 Output Formatting Review Recall that printf() employs a control

More information

Input / Output Functions

Input / Output Functions CSE 2421: Systems I Low-Level Programming and Computer Organization Input / Output Functions Presentation G Read/Study: Reek Chapter 15 Gojko Babić 10-03-2018 Input and Output Functions The stdio.h contain

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Lecture 4 Input & Output Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Outline printf scanf putchar getchar getch getche Input and Output in

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi CE 43 - Fall 97 Lecture 4 Input and Output Department of Computer Engineering Outline printf

More information

Unit 4. Input/Output Functions

Unit 4. Input/Output Functions Unit 4 Input/Output Functions Introduction to Input/Output Input refers to accepting data while output refers to presenting data. Normally the data is accepted from keyboard and is outputted onto the screen.

More information

CC112 Structured Programming

CC112 Structured Programming Arab Academy for Science and Technology and Maritime Transport College of Engineering and Technology Computer Engineering Department CC112 Structured Programming Lecture 3 1 LECTURE 3 Input / output operations

More information

Formatted Input/Output

Formatted Input/Output Chapter 3 Formatted Input/Output 1 The printf Function The printf function must be supplied with a format string ( 格式化字串 ), followed by any values that are to be inserted into the string during printing:

More information

DATA STRUCTURES USING C

DATA STRUCTURES USING C DATA STRUCTURES USING C File Handling in C Goals By the end of this unit you should understand how to open a file to write to it. how to open a file to read from it. how to open a file to append data to

More information

Binghamton University. CS-220 Spring Includes & Streams

Binghamton University. CS-220 Spring Includes & Streams Includes & Streams 1 C Pre-Processor C Program Pre-Processor Pre-processed C Program Header Files Header Files 2 #include Two flavors: #include Replace this line with the contents of file abc.h

More information

AWK - PRETTY PRINTING

AWK - PRETTY PRINTING AWK - PRETTY PRINTING http://www.tutorialspoint.com/awk/awk_pretty_printing.htm Copyright tutorialspoint.com So far we have used AWK's print and printf functions to display data on standard output. But

More information

SAMPLE MIDTERM SOLUTION

SAMPLE MIDTERM SOLUTION CMPT 102 SAMPLE MIDTERM SOLUTION PART 1 (40 points): SHORT ANSWER QUESTIONS 1. (8 points) Which of the following identifiers are invalid? Why? int my_book while loop FOR butter iff sentinel Calc-mean MeanOf45

More information

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017 C Concepts - I/O Lecture 19 COP 3014 Fall 2017 November 29, 2017 C vs. C++: Some important differences C has been around since around 1970 (or before) C++ was based on the C language While C is not actually

More information

Stream Model of I/O. Basic I/O in C

Stream Model of I/O. Basic I/O in C Stream Model of I/O 1 A stream provides a connection between the process that initializes it and an object, such as a file, which may be viewed as a sequence of data. In the simplest view, a stream object

More information

Should you know scanf and printf?

Should you know scanf and printf? C-LANGUAGE INPUT & OUTPUT C-Language Output with printf Input with scanf and gets_s and Defensive Programming Copyright 2016 Dan McElroy Should you know scanf and printf? scanf is only useful in the C-language,

More information

Introduction to Computing Lecture 03: Basic input / output operations

Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering nukhet.ozbek@ege.edu.tr Topics Streams

More information

printf("%c\n", character); printf("%s\n", "This is a string"); printf("%s\n", string); printf("%s\n",stringptr); return 0;

printf(%c\n, character); printf(%s\n, This is a string); printf(%s\n, string); printf(%s\n,stringptr); return 0; Chapter 9: Formatted Input/Output ================================= * All input and output is performed with streams - sequences of characters organized into lines. * Each line consists of zero or more

More information

CpSc 1111 Lab 4 Formatting and Flow Control

CpSc 1111 Lab 4 Formatting and Flow Control CpSc 1111 Lab 4 Formatting and Flow Control Overview By the end of the lab, you will be able to: use fscanf() to accept a character input from the user and print out the ASCII decimal, octal, and hexadecimal

More information

Course organization. Course introduction ( Week 1)

Course organization. Course introduction ( Week 1) Course organization Course introduction ( Week 1) Code editor: Emacs Part I: Introduction to C programming language (Week 2-9) Chapter 1: Overall Introduction (Week 1-3) Chapter 2: Types, operators and

More information

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files CS113: Lecture 7 Topics: The C Preprocessor I/O, Streams, Files 1 Remember the name: Pre-processor Most commonly used features: #include, #define. Think of the preprocessor as processing the file so as

More information

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection Overview By the end of the lab, you will be able to: declare variables perform basic arithmetic operations on integer variables

More information

ANSI C Programming Simple Programs

ANSI C Programming Simple Programs ANSI C Programming Simple Programs /* This program computes the distance between two points */ #include #include #include main() { /* Declare and initialize variables */ double

More information

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators Formatted Input and Output The printf function The scanf function Arithmetic and Assignment Operators Simple Assignment Side Effect

More information

INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT. Dept. of Electronic Engineering, NCHU. Original slides are from

INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT. Dept. of Electronic Engineering, NCHU. Original slides are from INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT Original slides are from http://sites.google.com/site/progntut/ Dept. of Electronic Engineering, NCHU Outline 2 printf and scanf Streams (input and output)

More information

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,

More information

Standard C Library Functions

Standard C Library Functions Demo lecture slides Although I will not usually give slides for demo lectures, the first two demo lectures involve practice with things which you should really know from G51PRG Since I covered much of

More information

CMPT 102 Introduction to Scientific Computer Programming

CMPT 102 Introduction to Scientific Computer Programming CMPT 102 Introduction to Scientific Computer Programming Control Structures for Loops Janice Regan, CMPT 102, Sept. 2006 0 Control Structures Three methods of processing a program In sequence Branching

More information

C: How to Program. Week /Mar/05

C: How to Program. Week /Mar/05 1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers

More information

SWEN-250 Personal SE. Introduction to C

SWEN-250 Personal SE. Introduction to C SWEN-250 Personal SE Introduction to C A Bit of History Developed in the early to mid 70s Dennis Ritchie as a systems programming language. Adopted by Ken Thompson to write Unix on a the PDP-11. At the

More information

Formatted Output Pearson Education, Inc. All rights reserved.

Formatted Output Pearson Education, Inc. All rights reserved. 1 29 Formatted Output 2 OBJECTIVES In this chapter you will learn: To understand input and output streams. To use printf formatting. To print with field widths and precisions. To use formatting flags in

More information

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection Overview By the end of the lab, you will be able to: declare variables perform basic arithmetic operations on integer variables

More information

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach C Fundamentals & Formatted Input/Output adopted from KNK C Programming : A Modern Approach C Fundamentals 2 Program: Printing a Pun The file name doesn t matter, but the.c extension is often required.

More information

ET156 Introduction to C Programming

ET156 Introduction to C Programming ET156 Introduction to C Programming g Unit 22 C Language Elements, Input/output functions, ARITHMETIC EXPRESSIONS AND LIBRARY FUNCTIONS Instructor : Stan Kong Email : skong@itt tech.edutech.edu General

More information

Input/Output Week 5:Lesson 16.1

Input/Output Week 5:Lesson 16.1 Input/Output Week 5:Lesson 16.1 Commands (On-Line) scanf/printf Principles of Programming-I / 131101 Prepared by: Dr. Bahjat Qazzaz --------------------------------------------------------------------------------------------

More information

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO Input and Output Streams and Stream IO 1 Standard Input and Output Need: #include Large list of input and output functions to: Read and write from a stream Open a file and make a stream Close

More information

Chapter 2 - Introduction to C Programming

Chapter 2 - Introduction to C Programming Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic

More information

File IO and command line input CSE 2451

File IO and command line input CSE 2451 File IO and command line input CSE 2451 File functions Open/Close files fopen() open a stream for a file fclose() closes a stream One character at a time: fgetc() similar to getchar() fputc() similar to

More information

Organization of a file

Organization of a file File Handling 1 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example: Consider keeping students records 100 students

More information

Fundamentals of Programming. Lecture 11: C Characters and Strings

Fundamentals of Programming. Lecture 11: C Characters and Strings 1 Fundamentals of Programming Lecture 11: C Characters and Strings Instructor: Fatemeh Zamani f_zamani@ce.sharif.edu Sharif University of Technology Computer Engineering Department The lectures of this

More information

Programming in C. Session 2. Seema Sirpal Delhi University Computer Centre

Programming in C. Session 2. Seema Sirpal Delhi University Computer Centre Programming in C Session 2 Seema Sirpal Delhi University Computer Centre Input & Output Input and Output form an important part of any program. To do anything useful your program needs to be able to accept

More information

2. Numbers In, Numbers Out

2. Numbers In, Numbers Out COMP1917: Computing 1 2. Numbers In, Numbers Out Reading: Moffat, Chapter 2. COMP1917 15s2 2. Numbers In, Numbers Out 1 The Art of Programming Think about the problem Write down a proposed solution Break

More information

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters University of Illinois at Urbana-Champaign Dept. of Electrical and Computer Engineering ECE 220: Computer Systems & Programming Review: Basic I/O in C Allowing Input from the Keyboard, Output to the Monitor

More information

Getting started with Java

Getting started with Java Getting started with Java Magic Lines public class MagicLines { public static void main(string[] args) { } } Comments Comments are lines in your code that get ignored during execution. Good for leaving

More information

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 12

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 12 BIL 104E Introduction to Scientific and Engineering Computing Lecture 12 Files v.s. Streams In C, a file can refer to a disk file, a terminal, a printer, or a tape drive. In other words, a file represents

More information

Basic I/O. COSC Software Tools. Streams. Standard I/O. Standard I/O. Formatted Output

Basic I/O. COSC Software Tools. Streams. Standard I/O. Standard I/O. Formatted Output Basic I/O COSC2031 - Software Tools C - Input/Output (K+R Ch. 7) We know how to do some basic input and output: getchar - reading characters putchar - writing characters printf - formatted output Input

More information

211: Computer Architecture Summer 2016

211: Computer Architecture Summer 2016 211: Computer Architecture Summer 2016 Liu Liu Topic: C Programming Data Representation I/O: - (example) cprintf.c Memory: - memory address - stack / heap / constant space - basic data layout Pointer:

More information

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow Variables Integer Representation Variables are used to store a value. The value a variable holds may change over its lifetime. At any point in time a variable stores one value (except quantum computers!)

More information

Operators and Control Flow. CS449 Fall 2017

Operators and Control Flow. CS449 Fall 2017 Operators and Control Flow CS449 Fall 2017 Running Example #include /* header file */ int main() { int grade, count, total, average; /* declaramons */ count = 0; /* inimalizamon */ total = 0;

More information

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO Input and Output Streams and Stream IO 1 Standard Input and Output Need: #include Large list of input and output functions to: Read and write from a stream Open a file and make a stream Close

More information

Chapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook

Chapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook Chapter 5 Section 5.1 Introduction to Strings CS 50 Hathairat Rattanasook "Strings" In computer science a string is a sequence of characters from the underlying character set. In C a string is a sequence

More information

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O) CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O) Overview (5) Topics Output: printf Input: scanf Basic format codes More on initializing variables 2000

More information

Computer System and programming in C

Computer System and programming in C 1 Basic Data Types Integral Types Integers are stored in various sizes. They can be signed or unsigned. Example Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign bit. If the sign

More information

CYSE 411/AIT681 Secure Software Engineering Topic #12. Secure Coding: Formatted Output

CYSE 411/AIT681 Secure Software Engineering Topic #12. Secure Coding: Formatted Output CYSE 411/AIT681 Secure Software Engineering Topic #12. Secure Coding: Formatted Output Instructor: Dr. Kun Sun 1 This lecture: [Seacord]: Chapter 6 Readings 2 Secure Coding String management Pointer Subterfuge

More information

2/9/18. CYSE 411/AIT681 Secure Software Engineering. Readings. Secure Coding. This lecture: String management Pointer Subterfuge

2/9/18. CYSE 411/AIT681 Secure Software Engineering. Readings. Secure Coding. This lecture: String management Pointer Subterfuge CYSE 411/AIT681 Secure Software Engineering Topic #12. Secure Coding: Formatted Output Instructor: Dr. Kun Sun 1 This lecture: [Seacord]: Chapter 6 Readings 2 String management Pointer Subterfuge Secure

More information

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language 1 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC

More information

UNIT IV-2. The I/O library functions can be classified into two broad categories:

UNIT IV-2. The I/O library functions can be classified into two broad categories: UNIT IV-2 6.0 INTRODUCTION Reading, processing and writing of data are the three essential functions of a computer program. Most programs take some data as input and display the processed data, often known

More information

Number Systems, Scalar Types, and Input and Output

Number Systems, Scalar Types, and Input and Output Number Systems, Scalar Types, and Input and Output Outline: Binary, Octal, Hexadecimal, and Decimal Numbers Character Set Comments Declaration Data Types and Constants Integral Data Types Floating-Point

More information

EE458 - Embedded Systems Lecture 4 Embedded Devel.

EE458 - Embedded Systems Lecture 4 Embedded Devel. EE458 - Embedded Lecture 4 Embedded Devel. Outline C File Streams References RTC: Chapter 2 File Streams man pages 1 Cross-platform Development Environment 2 Software available on the host system typically

More information

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments File I/O Content Input Output Devices File access Function of File I/O Redirection Command-line arguments UNIX and C language C is a general-purpose, high-level language that was originally developed by

More information

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance. 2.1 Introduction (No questions.) 2.2 A Simple Program: Printing a Line of Text 2.1 Which of the following must every C program have? (a) main (b) #include (c) /* (d) 2.2 Every statement in C

More information

Programming for Engineers Introduction to C

Programming for Engineers Introduction to C Programming for Engineers Introduction to C ICEN 200 Spring 2018 Prof. Dola Saha 1 Simple Program 2 Comments // Fig. 2.1: fig02_01.c // A first program in C begin with //, indicating that these two lines

More information

THE FUNDAMENTAL DATA TYPES

THE FUNDAMENTAL DATA TYPES THE FUNDAMENTAL DATA TYPES Declarations, Expressions, and Assignments Variables and constants are the objects that a prog. manipulates. All variables must be declared before they can be used. #include

More information

Week 2 C Fundamentals; Formatted Input/Output; int and float Types

Week 2 C Fundamentals; Formatted Input/Output; int and float Types Week 2 C Fundamentals; Formatted Input/Output; int and float Types How Computer Store Numbers int and float Types C Fundamentals General Forms of a C Program Variables, Identifiers and Assignment Formatted

More information

Basic Types and Formatted I/O

Basic Types and Formatted I/O Basic Types and Formatted I/O C Variables Names (1) Variable Names Names may contain letters, digits and underscores The first character must be a letter or an underscore. the underscore can be used but

More information

C Basics And Concepts Input And Output

C Basics And Concepts Input And Output C Basics And Concepts Input And Output Report Working group scientific computing Department of informatics Faculty of mathematics, informatics and natural sciences University of Hamburg Written by: Marcus

More information

2. Numbers In, Numbers Out

2. Numbers In, Numbers Out REGZ9280: Global Education Short Course - Engineering 2. Numbers In, Numbers Out Reading: Moffat, Chapter 2. REGZ9280 14s2 2. Numbers In, Numbers Out 1 The Art of Programming Think about the problem Write

More information

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6 1. What is File management? In real life, we want to store data permanently so that later on we can retrieve it and reuse it. A file is a collection of bytes stored on a secondary storage device like hard

More information

File I/O. Arash Rafiey. November 7, 2017

File I/O. Arash Rafiey. November 7, 2017 November 7, 2017 Files File is a place on disk where a group of related data is stored. Files File is a place on disk where a group of related data is stored. C provides various functions to handle files

More information

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University Overview of C, Part 2 CSE 130: Introduction to Programming in C Stony Brook University Integer Arithmetic in C Addition, subtraction, and multiplication work as you would expect Division (/) returns the

More information

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre Programming in C Session 8 Seema Sirpal Delhi University Computer Centre File I/O & Command Line Arguments An important part of any program is the ability to communicate with the world external to it.

More information

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2 CMPT 125 Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance John Edgar 2 Edit or write your program Using a text editor like gedit Save program with

More information

LSN 3 C Concepts for OS Programming

LSN 3 C Concepts for OS Programming LSN 3 C Concepts for OS Programming ECT362 Operating Systems Department of Engineering Technology LSN 3 C Programming (Review) Numerical operations Punctuators ( (), {}) Precedence and Association Mixed

More information

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

Goals of C  The Goals of C (cont.)  Goals of this Lecture The Design of C: A Rational Reconstruction Goals of this Lecture The Design of C: A Rational Reconstruction Help you learn about: The decisions that were available to the designers of C The decisions that were made by the designers of C Why? Learning

More information

Fundamentals of Programming Session 4

Fundamentals of Programming Session 4 Fundamentals of Programming Session 4 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2011 These slides are created using Deitel s slides, ( 1992-2010 by Pearson Education, Inc).

More information

Console Input and Output

Console Input and Output Console Input and Output Hour 5 PObjectives

More information

EC 413 Computer Organization

EC 413 Computer Organization EC 413 Computer Organization C/C++ Language Review Prof. Michel A. Kinsy Programming Languages There are many programming languages available: Pascal, C, C++, Java, Ada, Perl and Python All of these languages

More information

Formatting functions in C Language

Formatting functions in C Language Formatting functions in C Language Formatting means display data in different format, within given set of columns, show specified set of decimal and align the data to left or right along with zero fill

More information

Chapter 10: File Input / Output

Chapter 10: File Input / Output C: Chapter10 Page 1 of 6 C Tutorial.......... File input/output Chapter 10: File Input / Output OUTPUT TO A FILE Load and display the file named formout.c for your first example of writing data to a file.

More information

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath UNIT - I Introduction to C Programming Introduction to C C was originally developed in the year 1970s by Dennis Ritchie at Bell Laboratories, Inc. C is a general-purpose programming language. It has been

More information

Introduction to C An overview of the programming language C, syntax, data types and input/output

Introduction to C An overview of the programming language C, syntax, data types and input/output Introduction to C An overview of the programming language C, syntax, data types and input/output Teil I. a first C program TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 1 PROGRAMMING LANGUAGE C is

More information

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment. Lecture 12 CSE 110 20 July 1992 Today we ll cover the things that you still don t know that you need to know in order to do the assignment. 1 The NULL Pointer For each pointer type, there is one special

More information

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019 CSCI 2132: Software Development Introduction to C Norbert Zeh Faculty of Computer Science Dalhousie University Winter 2019 The C Programming Language Originally invented for writing OS and other system

More information

Lecture 3. More About C

Lecture 3. More About C Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 3-1 Lecture 3. More About C Programming languages have their lingo Programming language Types are categories of values int, float, char Constants

More information

File Handling in C. EECS 2031 Fall October 27, 2014

File Handling in C. EECS 2031 Fall October 27, 2014 File Handling in C EECS 2031 Fall 2014 October 27, 2014 1 Reading from and writing to files in C l stdio.h contains several functions that allow us to read from and write to files l Their names typically

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

EL2310 Scientific Programming

EL2310 Scientific Programming (yaseminb@kth.se) Overview Overview Roots of C Getting started with C Closer look at Hello World Programming Environment Discussion Basic Datatypes and printf Schedule Introduction to C - main part of

More information

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting Your factors.c and multtable.c files are due by Wednesday, 11:59 pm, to be submitted on the SoC handin page at http://handin.cs.clemson.edu.

More information

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Introduction to C Programming Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline Printing texts Adding 2 integers Comparing 2 integers C.E.,

More information

Files and Streams Opening and Closing a File Reading/Writing Text Reading/Writing Raw Data Random Access Files. C File Processing CS 2060

Files and Streams Opening and Closing a File Reading/Writing Text Reading/Writing Raw Data Random Access Files. C File Processing CS 2060 CS 2060 Files and Streams Files are used for long-term storage of data (on a hard drive rather than in memory). Files and Streams Files are used for long-term storage of data (on a hard drive rather than

More information

Output with printf Input. from a file from a command arguments from the command read

Output with printf Input. from a file from a command arguments from the command read More Scripting 1 Output with printf Input from a file from a command arguments from the command read 2 A script can test whether or not standard input is a terminal [ -t 0 ] What about standard output,

More information

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long LESSON 5 ARITHMETIC DATA PROCESSING The arithmetic data types are the fundamental data types of the C language. They are called "arithmetic" because operations such as addition and multiplication can be

More information

These are reserved words of the C language. For example int, float, if, else, for, while etc.

These are reserved words of the C language. For example int, float, if, else, for, while etc. Tokens in C Keywords These are reserved words of the C language. For example int, float, if, else, for, while etc. Identifiers An Identifier is a sequence of letters and digits, but must start with a letter.

More information

C programming basics T3-1 -

C programming basics T3-1 - C programming basics T3-1 - Outline 1. Introduction 2. Basic concepts 3. Functions 4. Data types 5. Control structures 6. Arrays and pointers 7. File management T3-2 - 3.1: Introduction T3-3 - Review of

More information

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be. CS 156 : COMPUTER SYSTEM CONCEPTS TEST 1 (C PROGRAMMING PART) FEBRUARY 6, 2001 Student s Name: MAXIMUM MARK: 100 Time allowed: 45 minutes Note: unless otherwise stated, the questions are with reference

More information

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering. C Tutorial: Part 1 Dr. Charalampos C. Tsimenidis Newcastle University School of Electrical and Electronic Engineering September 2013 Why C? Small (32 keywords) Stable Existing code base Fast Low-level

More information