62
Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering [email protected]

Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Introduction to Computing

Lecture 03:

Basic input / output operations

Assist.Prof.Dr. Nükhet ÖZBEK

Ege University

Department of Electrical & Electronics Engineering

[email protected]

Page 2: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Topics

• Streams

• printf function

• scanf function

Page 3: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Data storage

• Data can be stored in two different ways:

– by assignment to a variable

– copying data from an input device into a variable using a function like scanf

Page 4: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Input operation

• Sometimes, it is need to copy data into a variable for a program to manipulate different data each time it executes

– This data transfer from the outside world into memory is called an input operation

Page 5: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Output operation

• As the program executes, it performs computations and stores the results in memory

• These program results can be displayed to the user by an output operation

Page 6: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Input/Output

Program

Page 7: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Input/output functions

• All input/output operations in C are performed by special program units called input/output functions

• You can gain access to most of the operations through the preprocessor directive

– #include <stdio.h>

Page 8: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Input/output functions

• In C, a function call is used to call or activate a function

• Calling a function is analogous to asking a friend to perform an urgent task

Page 9: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Streams

• Text input or output is dealt with as a sequence of characters

• A stream serves as a channel to convey characters between I/O and programs

Page 10: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Streams: Input -- Example

135 25.5

_

1 3 5 2 5 . 5 \n

int item;

float cost;

scanf(“%d %f”, &item, &cost);

input buffer

Page 11: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Streams: Input -- Example (cont)

135 25.5

_

1 3 5 2 5 . 5 \n

int item;

float cost;

scanf(“%d %f”, &item, &cost);

item cost

Page 12: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Streams: Input – Example (cont)

135 25.5

_

2 5 . 5 \n

int item;

float cost;

scanf(“%d %f”, &item, &cost);

item

135

cost

Page 13: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Streams: Input – Example (cont)

135 25.5

_

\n

int item;

float cost;

scanf(“%d %f”, &item, &cost);

item

135

cost

25.5

Page 14: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Streams: Output -- Example

H e l l o ! \n

printf(“Hello!\n”);

output buffer

Page 15: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

H e l l o ! \n

printf(“Hello!\n”);

Streams: Output – Example (cont)

Page 16: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

e l l o ! \n

H

printf(“Hello!\n”);

Streams: Output – Example (cont)

Page 17: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

l l o ! \n

He

printf(“Hello!\n”);

Streams: Output – Example (cont)

Page 18: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

l o ! \n

Hel

printf(“Hello!\n”);

Streams: Output – Example (cont)

Page 19: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

o ! \n

Hell

printf(“Hello!\n”);

Streams: Output – Example (cont)

Page 20: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

! \n

Hello

printf(“Hello!\n”);

Streams: Output – Example (cont)

Page 21: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

\n

Hello!

printf(“Hello!\n”);

Streams: Output – Example (cont)

Page 22: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Hello!

_

printf(“Hello!\n”);

Streams: Output – Example (cont)

Page 23: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Streams

• From the program's point of view, the characters are queued in a pipe

• The sequence of characters is organized into lines

• Each line:

– can have zero or more characters

– ends with the "newline" character '\n'

Page 24: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

"Standard" Streams • Standard streams:

– stdin - standard input

• usually from keyboard

– stdout - standard output

• usually to screen

– stderr - standard error

• usually to screen

• must have at the top of your program #include <stdio.h>

• can be redirected

Page 25: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

stdin: Input

• Data is read in from stdin (into a variable) using the scanf() function

Page 26: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Example: ReadData

Input name, age, gender, idNumber

Page 27: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

#include <stdio.h>

Page 28: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

#include <stdio.h>

/*************************************\

Read in important info about a lecturer

\**************************************/

Page 29: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

#include <stdio.h>

/*************************************\

Read in important info about a lecturer

\**************************************/

int main()

{

return 0;

}

Page 30: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

#include <stdio.h>

/*************************************\

Read in important info about a lecturer

\**************************************/

int main()

{

char name[100];

float age;

char gender;

int idNumber;

return 0;

}

Page 31: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

#include <stdio.h>

/*************************************\

Read in important info about a lecturer

\**************************************/

int main()

{

char name[100];

float age;

char gender;

int idNumber;

scanf("%s %f %c %d", name, &age, &gender, &idNumber);

return 0;

}

Page 32: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

#include <stdio.h>

/*************************************\

Read in important info about a lecturer

\**************************************/

int main()

{

char name[100];

float age;

char gender;

int idNumber;

scanf("%s %f %c %d", name, &age, &gender, &idNumber);

return 0;

}

Page 33: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

#include <stdio.h> /*************************************\

Read in important info about a lecturer

\**************************************/

int main()

{

char name[100];

float age;

char gender;

int idNumber;

scanf("%s %f %c %d", name, &age, &gender, &idNumber);

return 0;

}

Input: Ali 19.5 M 2085

Ali

19.5

M

2085

Page 34: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

stdout:Output

• Data (e.g., from a variable) is written out to stdout using the printf() function.

Page 35: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Example: Write Data

Set name to “Ali”

Set age to 19.5

Set gender to „M‟

Set idNumber to 2085

Output name, age, gender, idNumber

Page 36: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

#include <stdio.h>

/*****************************************\

Write out important info about a lecturer

\*****************************************/

int main()

{

char *name = ”Ali" ;

float age = 19.5;

char gender = ‟M';

int idNumber = 2085 ;

printf("%s\n%f\n%c\n%d\n", name, age, gender, idNumber);

return 0;

}

Ali

19.5

M

2085

_

Page 37: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Formatted Input and Output • General form: printf(format-control-string, other-arguments);

scanf(format-control-string, other-arguments );

• Examples: printf("%s %f %c %d", name, age, gender, idNumber);

scanf("%s %f %c %d", name, &age, &gender, &idNumber);

Page 38: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

• Describes the format of the data for output

• Contains “conversion specifiers or place holders” and “literal characters”

Example:

printf(“%s is %d years old.\n”, name, age);

printf -- Format-Control-String

Page 39: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

• Describes the format of the data for output

• Contains “conversion specifiers or place holders” and “literal characters”

Example:

printf(“%s is %d years old.\n”, name, age);

conversion specifiers or

placeholders

printf -- Format-Control-String (cont)

Page 40: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

printf(“%s is %d years old.\n”, name, age);

literal characters

• Describes the format of the data for output

• Contains “conversion specifiers” and “literal characters”

Example:

printf -- Format-Control-String (cont)

Page 41: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

• For printf: variables containing data for output

Example:

printf(“%s is %d years old.\n”, name, age);

printf -- Other-Arguments

• In case of multiple placeholders, number of place holders must match number of variables

• C matches variables with placeholders in left-to-right order

Page 42: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

• Describes the format of the data given as input

• Contains “conversion specifiers or place holders”

scanf -- Format-Control-String

Example:

scanf("%s %f %c %d", name, &age, &gender,&id);

conversion

specifiers

Page 43: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

• For scanf: “pointers” to variables in which the input will be stored

scanf -- Other-Arguments

scanf("%s %f %c %d", name, &age, &gender,&id);

Example:

Page 44: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

• „&‟ is for scanf only!

scanf("%s %f %c %d", name, &age, &gender,&id);

scanf -- Other-Arguments (cont)

• For scanf: “pointers” to variables in which the input will be stored

Example:

• Variables of type int, float or char need ‘&’

• Do NOT use ‘&’ with strings!

Page 45: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Common Conversion Specifiers for Numerical Information

• decimal integer: %d printf(“What is %d plus %d?\n”, x, y);

scanf(“%d”, &sum);

• float: %f

printf(“%f squared is...? ”, x);

scanf(“%f”, &ans);

• double:

printf(“%f squared is...? ”, x);

scanf(“%lf”, &ans);

Page 46: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Conversion Specifiers for Alphanumeric Information

• char: %c printf(“What letter follows %c?\n”,ch);

scanf(“%c”, &nextchar);

• string: %s printf(“Name: %s\n”, name);

scanf(“%s”, name);

Page 47: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

•i or d: display a signed decimal integer

• f: display a floating point value (default precision is 6)

• e or E: display a floating point value in exponential notation

• g or G: display a floating point value in either f form or e form; whichever is more compact for the given value and precision

• L: placed before any float conversion specifier to indicate that a long double is displayed

printf: Conversion Specifiers

Page 48: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

scanf: Conversion Specifiers

•d: read an optionally signed decimal

integer

•i: read an optionally signed decimal, octal,

or hexadecimal integer

i and d: the argument is a “pointer” to an

integer int idNumber;

scanf("%d", &idNumber);

Page 49: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

scanf: Conversion Specifiers (cont)

• h or l: placed before any integer conversion

specifiers to indicate that a short or long integer is to be input long int idNumber;

scanf("%ld", &idNumber);

• l or L: placed before any float conversion

specifiers to indicate that a double or long double is to be input

Page 50: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Skipping Characters in Input Stream

• Skipping blank spaces

scanf("%d %d %d", &day, &month, &year);

• An alternative – Enter data as dd-mm-yyyy: 16-3-1999

– Store each number in date variables

scanf("%d-%d-%d", &day, &month, &year);

Page 51: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Displaying Prompts

• When input data are needed in an interactive program, it is better to use the printf function to display a prompting message or prompt, that tells the user what data to enter and if necessary, the format of the input

Page 52: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Displaying Prompts - Examples

• printf(“Enter the distance in miles> “);

scanf(“%lf”, &miles);

• printf(“Enter date as dd-mm-yyyy”);

scanf("%d-%d-%d", &day, &month, &year);

Page 53: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Formatting output

• Using justification for decimal numbers

– int a=15, b=116;

printf(“%d\n”,a);

printf(“%d”,b);

Output:

15

116

Page 54: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Formatting output

• Using justification for decimal numbers

– int a=15, b=116;

printf(“%3d\n”,a);

printf(“%3d”,b);

Output:

15

116

Page 55: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Formatting output

• Floating point numbers

– double fp = 251.7366;

printf( "%f %.3f %.2f %e %E\n", fp, fp, fp, fp,fp );

Output:

251.736600 251.737 251.74

2.517366e+002 2.517366E+002

Page 56: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Formatting output with \n

• \n is an escape sequence in C

• The cursor is a moving place market that indicates the next position on the screen where information will be displayed

• When executing a printf function, the cursor is advanced to the start of the next line on the screen if \n is encountered in the format string

Page 57: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

\n Examples

• printf(“Here is the first line\n“);

printf(“\nand this is the second.\n“);

• Here is the first line

and this is the second.

Page 58: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

\n Examples

• printf(“This sentence appears \non two lines.“);

• This sentence appears

on two lines.

Page 59: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Formatting output with \t

• \t is another escape sequence in C for horizontal tab

• Example: – printf(“age id\n”);

printf(“--- ------\n”);

printf(“%d %d\n”,age1,id1);

printf(“%d %d\n”,age2,id2);

– Output:

age id

--- ------

12 1222

3 1423

Page 60: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Formatting output with \t

• Example with (\t and justification): – printf(“age\tid\n”);

printf(“---\t------\n”);

printf(“%3d\t%4d\n”,age1,id1);

printf(“%3d\t%4d\n”,age2,id2);

– Output:

age id

--- ------

12 1222

3 1423

Page 61: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Some Other Escape sequences

• \a: Alert (bell)

• \b: Backspace

• \‟: Single quotation mark

• \”: Double quotation mark

• \\: Backslash

Page 62: Introduction to C - Yola · 2017. 3. 16. · Displaying Prompts •When input data are needed in an interactive program, it is better to use the printf function to display a prompting

Summary

• Input from keyboard is via the stdin stream using the scanf() function

• Output to the screen is via the stdout stream using the printf() function

• Streams carry characters

– divided into lines with „\n‟ character

• To use the C library functions, you must include the stdio.h header file

• Input and output can be formatted