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

Similar documents
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

Introduction to Programming

Fundamentals of Programming

Unit 4. Input/Output Functions

Fundamental of Programming (C)

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

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

Introduction to Computing Lecture 03: Basic input / output operations

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

Input/Output Week 5:Lesson 16.1

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

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

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

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

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

Data Types. Data Types. Integer Types. Signed Integers

Introduction to C. Systems Programming Concepts

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

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

BSM540 Basics of C Language

CSC 1107: Structured Programming

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

CSC 1107: Structured Programming

Should you know scanf and printf?

Chapter 7. Basic Types

C: How to Program. Week /Mar/05

Standard I/O in C and C++

Course Outline Introduction to C-Programming

Advanced C Programming Topics

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

Introduction to the C Programming Language

Lecture 3. More About C

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

Chapter 2 - Introduction to C Programming

n Group of statements that are executed repeatedly while some condition remains true

Fundamental of Programming (C)

Input / Output Functions

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Fundamentals of Programming. Lecture 11: C Characters and Strings

8. Characters and Arrays

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

Dept. of CSE, IIT KGP

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

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

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

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

Continued from previous lecture

C-LANGUAGE CURRICULAM

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

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

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

Computers Programming Course 5. Iulian Năstac

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

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

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

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

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

8. Characters, Strings and Files

WARM UP LESSONS BARE BASICS

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Introduction to C Programming

Standard File Pointers

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

The Design of C: A Rational Reconstruction (cont.)

Lecture 4. Console input/output operations. 1. I/O functions for characters 2. I/O functions for strings 3. I/O operations with data formatting

ET156 Introduction to C Programming

The Design of C: A Rational Reconstruction: Part 2

The C language. Introductory course #1

Intermediate Programming, Spring 2017*

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo

AWK - PRETTY PRINTING

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

Control Flow, Functions and Basic Linkage

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

211: Computer Architecture Summer 2016

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

The Design of C: A Rational Reconstruction (cont.)" Jennifer Rexford!

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

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

CC112 Structured Programming

C programming basics T3-1 -

File IO and command line input CSE 2451

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

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

COMP 208 Computers in Engineering

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5)

Fundamentals of Programming Session 4

C - Basic Introduction

Data types, variables, constants

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

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

BİL200 TUTORIAL-EXERCISES Objective:

Lesson 7. Reading and Writing a.k.a. Input and Output

Lecture 02 C FUNDAMENTALS

Console Input and Output

Transcription:

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

Overview Input/Output Structures and Arrays 2

Basic I/O character-based putchar (c) output getchar () input formatted - standard I/O printf (stuff goes in here) - output scanf (stuff goes in here) - input *** white space is important format specifiers (% before specifier) 3

Basic I/O #include <stdio.h> int main (void) { int i = 65; /* what if 258 instead of 65? */ char a; printf ("i = %d\n", i); printf ( i output with putchar = "); putchar (i); printf ("\n"); a = (char) i; printf ("a = %c\n", a); printf ( i = %c\n", i); getchar (); return(0); 4

Basic I/O #include <stdio.h> int main() { int x; scanf ("%d\n", &x); printf ("x = %d\n", x); pointer needed for scanf variable must use a pointer to the variable to change the value of the variable 5

Conversion Characters % plus character placeholder for variable replacement determines interpretation of bits 6

Basic I/O #include <stdio.h> int main (void) { int i = 65; printf ("i = %d\n", i); printf ("i output with putchar = "); putchar (i); printf ("\n"); printf ("i = %i\n", i); i = getchar (); printf ("i = %c\n", i); printf ("i = %x\n", i); printf ("i = %d\n", i); return(0); 7

Basic I/O #include <stdio.h> int main () { int x; scanf ("%d", &x); /* why need &? */ printf ("%d\n", x); float var; scanf ("%f", &var); printf ("%f\n", var); scanf ("%d", &var); printf ("%d\n", var); scanf ("%lf", &var); printf ("%lf\n", var); int first, second; scanf ("%d %d", &first, &second); int i, j; scanf (" %d %*d %*d%*d %d ", &i, &j); 8

Conversion Characters for Output (printf) Source: xkcd.com/571 9

Printing Decimal and Floating Point integers: %nd n = width of the whole number portion for decimal integers float: %m.nf m = total character width, including decimal point n = precision width after decimal 10

printf Examples print two values separated by tab printf ("%d\t%d\n", fahr, celsius); print the first number in a field three digits wide, and the second in a field six digits wide printf ("%3d %6d\n", fahr, celsius); each % construction in the first argument of printf is paired with the corresponding second argument, third argument, etc.; must match up properly by number and type, or wrong values printed printf ("\na=%f\nb=%f\nc=%f\npi=%f", a, b, c, d); c = a + b; printf ("%d + %d = %d\n", a, b, c); 11

Formatted I/O printf and scanf both formatted I/O both use standard I/O location printf converts values to character form according to format string outputs to stdout scanf converts characters according to the format string, followed by pointer arguments indicating where the resulting values are stored inputs from stdin 12

scanf requires two parameters format string argument with specifiers set of variable pointers to store corresponding values format string skips over all leading white space (spaces, tabs, newlines) % and type indicator in between: maximum field-width, type indicator modifier, or * (input suppression) input stops at end of format string, type mismatch in reading next call to scanf resume searching for input of correct type where previous scanf left off return value # of values converted 13

scanf Conversion Strings FORMAT MEANING VARIABLE TYPE %d read an integer value int %ld read a long integer value long %f read a real value float %lf read a double precision real value double %c read a character char %s read a character string from the input array of char 14

scanf Examples int day, month, year; scanf ("%d/%d/%d", &month, &day, &year); Input: 01/29/64 month == 1 day == 29 year == 64 int anint; scanf ("%*s %i", &anint); Input: Age: 29 anint == 29 int anint; scanf ("%i%%", &anint); Input: 23% anint == 23 int anint; long l; scanf ("%d %ld", &anint, &l); Input: -23 200 anint == -23 l == 200 double d; scanf ("%lf", &d); Input: 3.14 d == 3.14 Note: pressing the Enter key means you have entered a character string s; scanf ("%9s", s); Input: VeryLongString s == VeryLongS int anint, anint2; scanf ("%2i", &anint); scanf ( %2i, &anint2); Input: 2345 anint == 23 anint2 == 45 15

More scanf Examples Letter Type of Matching Argument Auto-skip; Leading White-Space Example % % (a literal, matched but not no int anint; converted or assigned) scanf("%i%%", &anint); d int yes int anint; long l; scanf("%d %ld", &anint, &l); i int yes int anint; scanf("%i", &anint); o unsigned int yes unsigned int auint; scanf("%o", &auint); u unsigned int yes unsigned int auint; scanf("%u", &auint); x unsigned int yes unsigned int auint; scanf("%d", &auint); a, e, f, float or double yes float f; double d; g scanf("%f %lf", &f, &d); c char no char ch; scanf(" %c", &ch); s array of char yes char s[30]; scanf("%29s", s); n int no int x, cnt; scanf("x: %d%n", &x, &cnt); [ array of char no char s1[64], s2[64]; scanf(" %[^\n]", s1); scanf("%[^\t] %[^\t]", s1, s2); Sample Matching Input 23% -23 200 0x23 023 23 1A 1.2 3.4 Q hello X: 123 (cnt==6) Hello World field1 field2 16

scanf Examples to allow spaces to be input with the string scanf (" %[^\n]s",a); 17

Increment Operators prefix/postfix 18

Ternary Operators condition? result1 : result2 19

Conditional Statement compound if #include <stdio.h> int main() { int x; scanf ("%d\n", &x); if (x > 0) printf ("%d is positive\n", x); else if (x < 0) printf ("%d is negative\n", x); else printf ("%d is zero\n", x); 20

Iteration while loop #include <stdio.h> int main () { int n = 10; while (n > 0) { printf ( %n, ", n); --n; printf ("liftoff!\n"); 21

Iteration for loop #include <stdio.h> int main () { int n; for (n = 10; n > 0; n--) { printf ( %n, ", n); printf ("liftoff!\n"); 22

Iteration for loop 23

break Statement break #include <stdio.h> int main () { int n; for (n = 10; n > 0; n--) { printf ("%n, ", n); if (n == 3) { printf ("Countdown aborted!\n"); break; 24

continue Statement continue #include <stdio.h> int main () { int n; for (n = 10; n > 0; n--) { if (n == 5) continue; printf ("%n, ", n); printf ( liftoff!\n"); 25

switch Statement switch (x) { case 1: case 2: case 3: printf ( x is 1, 2, or 3\n ); break; case 4: printf ( x is 4\n ); break; default: printf ( x is not 1, 2, 3, or 4\n ); if (x == 1 x == 2 x == 3) { ( x is 1, 2, or 3\n ); else if (x == 4) { printf ( x is 4\n ); else { printf ( x is not 1, 2, 3, or 4\n ); 26