+ All Categories
Home > Education > CHAPTER 4

CHAPTER 4

Date post: 13-Jul-2015
Category:
Upload: mohdmizan
View: 70 times
Download: 0 times
Share this document with a friend
Popular Tags:
21
DATA INPUT AND OUTPUT 1
Transcript

DATA INPUT AND

OUTPUT

1

2

• Define input output statements

• Explain function of input output

statements

• Create and modify input output

file in programme

3

4

C Environment

C environment

5

input device output device

error device

stdinstdout

stderr

input/output operation take places as a stream

of character

#include<stdio.h> includes the contents of

standard input-output file, stdio.h at compile

time (contains definitions of stdin, stdout,

stderr)

6

C provides standard functions, scanf() and

printf() for performing formatted input and

output

Input data may contain a mixed mode data:

7

DATA TYPES EXAMPLE

float floating point 101.7, 0.5, 2.36

double floating point 2.365, 75.666, 0.12222

int decimal number 225, 3, 75, 9662

char character a, f, apple, 15DET09F1002

printf() and puts()

printf() function

used for formatted output to standard output device that is screen

general form:

e.g:

printf(“%d%d%d\n”,456,456,456)

Output: 456456456

8

printf(“format_specifier”,variable_list1,variable_list2,…)

9

Codes Meaning

\b Back space

\f Form feed

\n New line

\r Carriage return

\t Horizontal tab

\” Double quote mark

\’ Single quote mark

\0 Null

\\ Back slash

\v Vertical tab

\a Alert

\o Octal constant

\x Hexadecimal constants

puts()

to display a string contained in a string

variable

general form:

e.g:

char str[20]=“I am going to England”;

puts(str);

if str has the string “I am going to Englad”, puts(str) would

display the entire string “I am going to England” and

moves the cursor to the beginning of the next line

10

int puts (char*)

scanf() and gets()

scanf() function

used for formatted input from standard input device that is keyboard

general form:

e.g:

printf(“Please enter a number”);

scanf(“%d”,&number1)

Output: Please enter a number 15

11

scanf(“format specifier”,variable_list1,variable_list2,…)

gets()

to accept string up to a new line character into

a string variable

general form:

12

char*gets (char *);

To illustrate gets() and puts()

13

#include<stdio.h>

#include<conio.h>

void main()

{

char words[40];

clrscr();

printf(“Enter a line of text: “);

gets (words);

printf(“\nThe line of text entered is “);

puts (words);

}

Enter a line of text: I love programming

The line of text entered is I love programming

14

printf() STATEMENT OUTPUT

printf(“Line 1”);

printf(“Line 2”);

printf(“Line 3”);

Line1Line2Line3

The newline character (\n) must be include whenever

the cursor have to move to the next line

printf() STATEMENT OUTPUT

printf(“Line 1\n”);

printf(“Line 2\n”);

printf(“Line 3\n”);

Line1

Line2

Line3

printf() STATEMENT OUTPUT

printf(“Line 1”);

printf(“\nLine 2\n”);

printf(“Line 3”);

Line1

Line2

Line3

Specifies the field in which the data has to

be entered

15

code meaning

%c Single character

%d Decimal integer

%e Floating point value

%f Floating point value

%o Octal value

%x Hexadecimal value

%s A string

%u Unsigned integer

int d;

to accept a value into a variable n of int type

16

scanf(“%d”,&n); Which is %d is the format specifier for int

Variable n

char c;

to accept a value into a variable c of char type

scanf(“%c”, &c); Note that the format specified used is %c

Variable c

float f;

to accept a floating point value into the variable f

17

scanf(“%f”, &f); Note that the format specified used is %f

Variable f

To read values into three variable i, f, and c of

type int, float and char using a single statement

scanf(“%d%f%c”, &i,

&f, &c);

Note that the order of the format

specifiers matches with that of the

variable

int d;

To display the value of the variable n of int type

18

printf(“%d”,n); Which is %d is the format specifier for int

Variable n

char c;

To display the character stored in the variable c

printf(“%c”,c); Note that the format specified used is %c

Variable c

float f;

to display the floating point value stored in the variable f

19

printf(“%f”,f); Note that the format specified used is %f

Variable f

To display the value stored in three variable i, f, and c of type int, float and char using a single statement

printf(“%d%f%c”,i,f,

c);

Note that the order of the format

specifiers matches with that of the

variable

To illustrate simple printf() and scanf()

20

#include<stdio.h>

void main()

{

int number_1, number_2, total;

printf(“Enter first number: “);

scanf(“%d”, &number_1);

printf(“Enter second number: “);

scanf(“%d”, &number_2);

total= number_1 + number_2;

printf(“\nTotal for first and second number : %d”, total);

}

Enter first number : 15

Enter second number :65

Total for first and second number :80

21


Recommended