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

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

Introduction to Computing Lecture 03: Basic input / output operations

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

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

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

Unit 4. Input/Output Functions

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

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

1/31/2018. Overview. The C Programming Language Part 2. Basic I/O. Basic I/O. Basic I/O. Conversion Characters. Input/Output Structures and Arrays

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Fundamental of Programming (C)

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

Reserved Words and Identifiers

Fundamentals of Programming

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

Advanced C Programming Topics

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

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

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

BSM540 Basics of C Language

Should you know scanf and printf?

211: Computer Architecture Summer 2016

BİL200 TUTORIAL-EXERCISES Objective:

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

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

SWEN-250 Personal SE. Introduction to C

CS3157: Advanced Programming. Outline

Fundamental of Programming (C)

CS 261 Fall Mike Lam, Professor. Structs and I/O

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

Display Input and Output (I/O)

UNIT-I Input/ Output functions and other library functions

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

Computer System and programming in C

File I/O. Last updated 10/30/18

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

Fundamentals of Programming. Lecture 11: C Characters and Strings

Programming. C++ Basics

CSE 303 Lecture 8. Intro to C programming

Fundamentals of Programming Session 8

Arrays, Strings, & Pointers

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Programming in C++ 4. The lexical basis of C++

Topic 6: A Quick Intro To C

ET156 Introduction to C Programming

Syntax and Variables

6.096 Introduction to C++ January (IAP) 2009

EC 413 Computer Organization

Continued from previous lecture

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

A Quick Look at C for C++ Programmers

Input / Output Functions

CC112 Structured Programming

CS2141 Software Development using C/C++ C++ Basics

Programming in C Quick Start! Biostatistics 615 Lecture 4

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

COMP 2355 Introduction to Systems Programming

ANSI C Programming Simple Programs

Work relative to other classes

Programming and Data Structures

Number Systems, Scalar Types, and Input and Output

C Language, Token, Keywords, Constant, variable

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

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

Lecture 3. More About C

WARM UP LESSONS BARE BASICS

The Design of C: A Rational Reconstruction: Part 2

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Standard File Pointers

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

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

Data Types and Variables in C language

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

A Fast Review of C Essentials Part I

CS16 Exam #1 7/17/ Minutes 100 Points total

(9-2) Strings I H&K Chapter 8. Instructor - Andrew S. O Fallon CptS 121 (March 7, 2018) Washington State University

Visual C# Instructor s Manual Table of Contents

Course organization. Course introduction ( Week 1)

Tokens, Expressions and Control Structures

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Full file at

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

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Computers Programming Course 5. Iulian Năstac

Lecture 02 C FUNDAMENTALS

Basic Types and Formatted I/O

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 7. Basic Types

Chapter 2: Basic Elements of C++

C mini reference. 5 Binary numbers 12

Standard C Library Functions

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Arithmetic Expressions in C

C Programming. Unit 9. Manipulating Strings File Processing.

High Performance Computing

Fundamentals of Programming

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

Transcription:

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 a strict subset of C++, most C code can be handled by a C++ compiler The older C-style way of including libraries looks like this: #include <stdio.h> #include <string.h> #include <math.h> #include <ctype.h> The 4 different styles of casting are a C++ feature. C casts look like this double val = 5.783; int integer = (int) val;

C vs. C++: Some important differences Some of the C++-Only features are: The Boolean type Classes and Objects namespaces and using statements exception handling (with try, catch, throw) using new and delete for dynamic memory management templates Function Overloading Pass by reference Default parameters

structs and enums In both C and C++, the struct definition is the same. In C++, declaring a struct or an enum automatically creates a new type. In C, we need to remind the compiler about the user-defined type every time we use it. We can define a new type in C using the typedef keyword. typedef struct student // structure "tag" { char name[20]; double gpa; } Student; // this is now the new type name struct student s1; // C declaration using "tag" Student s2; // C declaration using new type name

Basics of Formatted Input/Output in C I/O is essentially done one character (or byte) at a time stream a sequence of characters flowing from one place to another input stream: data flows from input device (keyboard, file, etc) into memory output stream: data flows from memory to output device (monitor, file, printer, etc) Standard I/O streams (with built-in meaning) stdin: standard input stream (default is keyboard) stdout: standard output stream (defaults to monitor) stderr: standard error stream Formatted I/O refers to the conversion of data to and from a stream of characters, for printing (or reading) in plain text format All text I/O we do is considered formatted I/O The other option is reading/writing direct binary information (common with file I/O, for example)

C I/O stdio.h contains basic I/O functions scanf: reads from standard input (stdin) printf: writes to standard output (stdout) There are other functions similar to printf and scanf that write to and read from other streams How to include, for C or C++ compiler #include <stdio.h> // for a C compiler #include <cstdio> // for a C++ compiler

Output with printf The basic format of a printf function call is: printf (format string, list of expressions); where: format string is the layout of what s being printed list of expressions is a comma-separated list of variables or expressions yielding results to be inserted into the output To output string literals, just use one parameter on printf, the string itself printf("hello, world!\n"); printf("greetings, Earthling\n\n");

Conversion Specifiers A conversion specifier is a symbol that is used as a placeholder in a formatting string. For integer output (for example), %d is the specifier that holds the place for integers. Here are some commonly used conversion specifiers (not a comprehensive list): Specifier Purpose %d int (signed decimal integer) %u unsigned decimal integer %f floating point values (fixed notation) - float, double %e floating point values (exponential notation) %s string %c character

Printing Integers To output an integer, use %d in the format string, and an integer expression in the list of expressions. int numstudents = 35123; printf("fsu has %d students", numstudents); // FSU has 35123 students We can specify the field wicth (i.e. how many spaces the item prints in). Defaults to right-justification. Place a number between the % and the d. In this example, field width is 10: printf("fsu has %10d students", numstudents); // FSU has 35123 students

Printing Integers To left justify, use a negative number in the field width printf("fsu has %-10d students", numstudents); // FSU has 35123 students If the field width is too small or left unspecified, it defaults to the minimum number of characters required to print the item: printf("fsu has %2d students", numstudents); // FSU has 35123 students Specifying the field width is most useful when printing multiple lines of output that are meant to line up in a table format

Printing Floating-point numbers Use the %f modifer to print floating point values in fixed notation: double cost = 123.45; printf("your total is $%f today\n", cost); // Your total is $123.450000 today Use %e for exponential notation: printf("your total is $%e today\n", cost); // Your total is $1.234500e+02 today Note that the e+02 means times 10 to the 2nd power

Printing Floating-point numbers You can also control the decimal precision, which is the number of places after the decimal. Output will round to the appropriate number of decimal places, if necessary: printf("your total is $%.2f today\n", cost); // Your total is $123.45 today Field width can also be controlled, as with integers: printf("your total is $%9.2f today\n", cost); // Your total is $ 123.45 today n the conversion specifier, the number before the decimal is field width, and the number after is the precision. (In this example, 9 and 2).

Printing characters and strings Use the formatting specifier %c for characters. Default field size is 1 character: char letter = Q ; printf("%c%c%c\n", *, letter, * ); // Output is: *Q* Use %s for printing strings. Field widths work just like with integers: printf("%s%10s%-10send\n", "Hello", "Alice", "Bob"); // Hello AliceBob END

scanf basics To read data in from standard input (keyboard), we call the scanf function. The basic form of a call to scanf is: scanf(format string, list of variable addresses); The format string is like that of printf But instead of expressions, we need space to store incoming data, hence the list of variable addresses If x is a variable, then the expression &x means address of x scanf example: int month, day; printf("please enter your birth month, followed by the day: "); scanf("%d %d", &month, &day);

scanf Basics Conversion Specifiers Mostly the same as for output. Some small differences Use %f for type float, but use %lf for types double and long double The data type read, the conversion specifier, and the variable used need to match in type White space is skipped by default in consecutive numeric reads. But it is not skipped for character/string inputs.

printf/scanf with C-strings An entire C-style string can be easily printed, by using the %s formatting symbol, along with the name of the char array storing the string (as the argument filling in that position): char greeting[] = "Hello"; printf("%s", greeting); // prints the word "Hello" Be careful to only use this on char arrays that are being used as C-style strings. (This means, only if the null character is present as a terminator).

printf/scanf with C-strings Similarly, you can read a string into a char array with scanf. The following call allows the entry of a word (up to 19 characters and a terminating null character) from the keyboard, which is stored in the array word1: char word1[20]; scanf("%s", word1); Characters are read from the keyboard until the first white space (space, tab, newline) character is encountered. The input is stored in the character array and the null character is automatically appended. Note also that the & was not needed in the scanf call (word1 was used, instead of &word1). This is because the name of the array by itself (with no index) actually IS a variable that stores an address (a pointer).