i/o in c + misc

17
7.2.2002 Sudeshna Sarkar, IIT Khara gpur 1 I/O in C + Misc Lecture 13 5.2.2002 – 7.2.2002

Upload: cicily

Post on 06-Jan-2016

23 views

Category:

Documents


0 download

DESCRIPTION

I/O in C + Misc. Lecture 13 5.2.2002 – 7.2.2002. 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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: I/O in C + Misc

7.2.2002 Sudeshna Sarkar, IIT Kharagpur1

I/O in C + Misc

Lecture 135.2.2002 – 7.2.2002

Page 2: I/O in C + Misc

7.2.2002 Sudeshna Sarkar, IIT Kharagpur2

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

Page 3: I/O in C + Misc

7.2.2002 Sudeshna Sarkar, IIT Kharagpur3

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,

Page 4: I/O in C + Misc

7.2.2002 Sudeshna Sarkar, IIT Kharagpur4

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.

Page 5: I/O in C + Misc

7.2.2002 Sudeshna Sarkar, IIT Kharagpur5

Single character output : putchar

putchar prints a single character to stdout

char ch;

. . .

putchar (ch) ;

Page 6: I/O in C + Misc

7.2.2002 Sudeshna Sarkar, IIT Kharagpur6

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));

}

Page 7: I/O in C + Misc

7.2.2002 Sudeshna Sarkar, IIT Kharagpur7

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.

Page 8: I/O in C + Misc

7.2.2002 Sudeshna Sarkar, IIT Kharagpur8

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.

Page 9: I/O in C + Misc

7.2.2002 Sudeshna Sarkar, IIT Kharagpur9

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

Page 10: I/O in C + Misc

7.2.2002 Sudeshna Sarkar, IIT Kharagpur10

Review: Mixed Mode ArithmeticWhat 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 doublehigher precision

Page 11: I/O in C + Misc

7.2.2002 Sudeshna Sarkar, IIT Kharagpur11

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);

Page 12: I/O in C + Misc

7.2.2002 Sudeshna Sarkar, IIT Kharagpur12

Assignment : mixed modefloat 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.

Page 13: I/O in C + Misc

7.2.2002 Sudeshna Sarkar, IIT Kharagpur13

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 */

Page 14: I/O in C + Misc

7.2.2002 Sudeshna Sarkar, IIT Kharagpur14

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.

Page 15: I/O in C + Misc

7.2.2002 Sudeshna Sarkar, IIT Kharagpur15

Library functions: stdio.h

int getchar (void)

int printf(...)

int putchar(c)

int rand(void)

int scanf(...)

Page 16: I/O in C + Misc

7.2.2002 Sudeshna Sarkar, IIT Kharagpur16

Library functions: math.hdouble 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)

Page 17: I/O in C + Misc

7.2.2002 Sudeshna Sarkar, IIT Kharagpur17

Library functions: ctype.hint 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)