I/O in C + Misc Lecture 13 7.2.2002. 6.2..2001 Sudeshna Sarkar, IIT Kharagpur.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Chapter 4 Function By C. Shing ITEC Dept Radford University.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
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.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Chapter 18 I/O in C.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
Module-E- Libraries1/49 Module E- Standard Libraries Input and Validation Formatted Output Library Functions.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
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.
CTYPE.H Introduction  The ctype header is used for testing and converting characters.  A control character refers to a character that.
Gator Engineering Project 1 Grades released Re-grading –Within one week –TA: Fardad, or office hours: MW 2:00 – 4:00 PM TA Huiyuan’s office hour.
Sudeshna Sarkar, IIT Kharagpur 1 I/O in C + Misc Lecture –
Files A collection of related data treated as a unit. Two types Text
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
Chapter 18 I/O in C Original slides from Gregory Byrd, North Carolina State University Modified slides by C. Wilcox, Y. Malaiya Colorado State University.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
Introduction to ‘c’ language
Lecture 3 Expressions, Type Conversion, Math and String
Arithmetic Expressions
C Characters and Strings
Character Processing How characters can be treated as small integers?
Tokens in C Keywords Identifiers Constants
Input/output.
INTRODUCTION Every language has some features that provides interaction between the program and the user of the program. C language uses the reference.
Functions, Part 2 of 2 Topics Functions That Return a Value
Revision Lecture
Chapter 18 I/O in C.
Introduction to C CSE 2031 Fall /3/ :33 AM.
Data Type.
Input and Output Lecture 4.
The C “switch” Statement
Conversions of the type of the value of an expression
The C “switch” Statement
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Data Type.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
A First Book of ANSI C Fourth Edition
The switch Statement Topics Multiple Selection switch Statement
Chapter 18 I/O in C.
Lectures on Numerical Methods
Chapter 18 I/O in C.
Chapter 4 Managing Input and Output Operations
Character Processing How characters can be treated as small integers?
Terminal-Based Programs
Introduction to C EECS May 2019.
Data Type.
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
C Characters and Strings
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Chapter 18 I/O in C.
Presentation transcript:

I/O in C + Misc Lecture 13 7.2.2002. 6.2..2001 Sudeshna Sarkar, IIT Kharagpur

Input : movement of data into memory from the outside world. read operation Changes the value of the variable Output: movement of data from memory to outside world. write operation does not change value of memory 6.2..2001 Sudeshna Sarkar, IIT Kharagpur

Conceptually, C performs all I/O on text streams. In C, input and output is handled by a set of standard library functions. Conceptually, C performs all I/O on text streams. The standard input stream from the keyboard is referred to as stdin. The standard output stream is referred to as stdout, 6.2..2001 Sudeshna Sarkar, IIT Kharagpur

Single character input : getchar getchar reads a single character from stdin char ch; ch = getchar(); The getchar function accepts any character types in including newline, blank, and tab. 6.2..2001 Sudeshna Sarkar, IIT Kharagpur

Single character output : putchar putchar prints a single character to stdout char ch; . . . putchar (ch) ; 6.2..2001 Sudeshna Sarkar, IIT Kharagpur

Example /* Read a character from the keyboard and output it in reverse case */ #include <stdio.h> #include <ctype.h> main() { char alpha; printf (“\n Enter a alphabet (lowercase or uppercase): “); alpha = getchar(); if (islower(alpha)) putchar (toupper(alpha)); else putchar (tolower(alpha)); } 6.2..2001 Sudeshna Sarkar, IIT Kharagpur

Display formatted output (printf) printf (“Enter %d positive numbers:”, num1*2); printf (“control string”, list of expressions) ; Control string gives the format of output. Expressions are what to output %d is a placeholder for an int value. printf (“The %d-th character is %c\n”, i, ch); % placeholders in format string match expressions in output list in number, order, type. 6.2..2001 Sudeshna Sarkar, IIT Kharagpur

Read input : scanf scanf (“control string”, &input list) ; scanf(“%d”, &number); scanf(“%d%lf”,&studentId, &grade); Input list variables must be preceded by an & % placeholders in the format must match variables in the input list. one-for-one in number, order, and type. 6.2..2001 Sudeshna Sarkar, IIT Kharagpur

Whitespace “Whitespace”: space ‘ ‘, tab ‘\t’, newline ‘\n’ Skipped by scanf for int (%d) and double (%lf) user can type spaces before a number and they are ignored. Not skipped for char input (%c) each character typed, including spaces, is used 6.2..2001 Sudeshna Sarkar, IIT Kharagpur

Review: Mixed Mode Arithmetic What is 2 * 3.14 ? The compiler implicitly (automatically) converts int to double when they occur together. Generally in mixed expressions, the variable whose type has lower precision is promoted to the type of the variable with higher precision. int unsigned int unsigned long int long int float double long double higher precision 6.2..2001 Sudeshna Sarkar, IIT Kharagpur

double d; int x=5, y=2; double dx=5.0, dy=2.0; d = x/y; printf(“1. d =%f\n”, d); d = dx/y; printf(“2. d =%f\n”, d); d = x/dy; printf(“3. d =%f\n”, d); 6.2..2001 Sudeshna Sarkar, IIT Kharagpur

Assignment : mixed mode float i, j; i = 1.99; j = -1.99; printf (“i = %d, j = %d\n”, i, j); /* floating point numbers are truncated while converting to int */ Assignment rules: If the two operands in an assignment expression are of different data types, then the value of the righthand operand will be converted to the type of the operand on the left. A float value is truncated if assigned to an int. A double identifier is rounded if assigned to a float. An int may be altered if assigned to a short int or to a char. 6.2..2001 Sudeshna Sarkar, IIT Kharagpur

Explicit conversions: case (type) expression; double avg = total/count; double avg = (double)total / (double)count; ((int)(i+f)) % 4 /* i is int, f is float */ 6.2..2001 Sudeshna Sarkar, IIT Kharagpur

Errors Syntax : the required form of the program The C compiler catched these “syntax errors” Semantics and logic: what the program means what you want it to do The C compiler cannot catch these kinds of errors. 6.2..2001 Sudeshna Sarkar, IIT Kharagpur

Library functions: stdio.h int getchar (void) int printf(...) int putchar(c) int rand(void) int scanf(...) 6.2..2001 Sudeshna Sarkar, IIT Kharagpur

Library functions: math.h double acos (double) double asin (double) double atan (double) double ceil (double) double floor (double) double cos(double) double exp(double) double fabs (double) double log (double) double log10 (double) double pow (double, double) double sin (double) double sinh (double) double cosh (double) double tan (double) double tanh (double) double sqrt(double) 6.2..2001 Sudeshna Sarkar, IIT Kharagpur

Library functions: ctype.h int isalnum (char) int isalpha (char) int isascii (char) int isdigit (char) int islower (char) int ispunct (char) int isspace (char) int isupper (char) int toascii (char) int tolower (char) int toupper (char) 6.2..2001 Sudeshna Sarkar, IIT Kharagpur