0% found this document useful (0 votes)
15 views23 pages

Chapter - 4

The document discusses input and output operations in computer programming. It covers reading and writing characters and numbers, formatted input using scanf(), and formatted output using printf(). Examples are provided to demonstrate various I/O functions and formatting specifiers.

Uploaded by

Trupal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views23 pages

Chapter - 4

The document discusses input and output operations in computer programming. It covers reading and writing characters and numbers, formatted input using scanf(), and formatted output using printf(). Examples are provided to demonstrate various I/O functions and formatting specifiers.

Uploaded by

Trupal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

CE143 : COMPUTER CONCEPTS & PROGRAMMING

July – November 2019

Chapter – 4

Managing Input and Output Operation

Devang Patel Institute of Advance Technology and Research


Objectives
■ To be able to describe how a character is read
■ To be able to express how a character is written
■ To be able to explain formatted input
■ To be able discuss formatted output

Chapter – 4 : Managing Input and Output Operation


Introduction
■ In this chapter, we will discuss
■ Reading a character, Writing a character, Introduction to

ASCII code, library functions


■ Formatted input using scanf ( )

■ Formatted output of integer and real data using printf ( )

Chapter – 4 : Managing Input and Output Operation


Introduction

Processing Reading Input:


1. X=5,a=0;
2. scanf();
Writing

Output:
1. printf()
Processed Data/
Information/ Result

Chapter – 4 : Managing Input and Output Operation


Reading a Character

■ getchar(): Reads single character


variable_name=getchar();
Example: char name;
name=getchar();

Chapter – 4 : Managing Input and Output Operation


Writing a Character

■ putchar(): Writing a character one at a time


putchar(variable_name);
Example: char var=‘a’;
putchar(var);
putchar(‘\n’); \\new line

Chapter – 4 : Managing Input and Output Operation


Using char data type
void main()
{
char a,b;
clrscr();

printf("Using getchar()\n");
printf("-------------------\n\n");
printf("Enter the value of a=>");
a=getchar();
printf("\n");
printf("Value of a is=>");
putchar(a);

printf("\n\n\n");

printf("Using scanf()\n");
printf("-------------------\n\n");
printf("Enter the value of b=>");
scanf("%c",&b);
printf("\n");
printf("Value of b is=>%c",b);
getch();
}

Chapter – 4 : Managing Input and Output Operation


Chapter – 4 : Managing Input and Output Operation
Using int data type
void main()
{
int a,b;
clrscr();

printf("Using getchar()\n");
printf("-------------------\n\n");
printf("Enter the value of a=>");
a=getchar();
printf("\n");
printf("Value of a is=>");
putchar(a);

printf("\n\n\n");

printf("Using scanf()\n");
printf("-------------------\n\n");
printf("Enter the value of b=>");
scanf("%d",&b);
printf("\n");
printf("Value of b is=>%d",b);
getch();
}

Chapter – 4 : Managing Input and Output Operation


Chapter – 4 : Managing Input and Output Operation
ctype.h Library Function
■ Declares several functions that are useful for testing and
mapping characters.

Chapter – 4 : Managing Input and Output Operation


Formatted Input

■ It refers to an input data that has been arranged in


a particular format
Example: Input data= 15.75 123 John

float int char

scanf(“field format”,arg 1, arg 2,…arg n);

Chapter – 4 : Managing Input and Output Operation


Formatted Input: Inputting Integer Numbers
%w d

Conversion Width Number


of a to be
specificatio number read
n
scanf (“%2d %5d”,&num1,&num2);

Case 1: For input 50 and 31426


num1=50 and num2=31426

Case 2: For input 31426 and 50


num1=31 and num2=426 (50 is unread will be assigned
to first variable in next scanf call. )
Chapter – 4 : Managing Input and Output Operation
Formatted Input: Inputting Integer Numbers

scanf (“%d %*d %d”,&a, &b);

■ Assigning a data 123, 456 and 78.2 respectively.


■ Assigned data would be :
a=123
456 skipped (because of * )
B=78

Chapter – 4 : Managing Input and Output Operation


Formatted Input: Inputting Integer Numbers
void main()
{
int a,b,c;
clrscr();
printf("Enter three numbers a, b and c\n");
scanf("%d %*d %d",&a,&b,&c);
printf("a=%d b=%d c=%d",a,b,c);
getch();
}

Chapter – 4 : Managing Input and Output Operation


Formatted Input: Inputting Integer Numbers
void main()
{
int a,b,c;
clrscr();
printf("Enter three numbers a, b and c\n");
scanf("%d %*d %d",&a,&b);
printf("a=%d b=%d ",a,b);
getch();
}

Chapter – 4 : Managing Input and Output Operation


Formatted Input: Inputting Integer Numbers
void main()
{
int a,b,c;
clrscr();
printf("Enter three numbers a, b and c\n");
scanf("%d %*d %d",&a,&b);
printf("a=%d b=%d c=%d",a,b);
getch();
}

Chapter – 4 : Managing Input and Output Operation


void main()
{
int a,b,c,x,y,z,p,q,r;
clrscr();
printf("Enter three numbers a, b and c\n");
scanf("%d %*d %d",&a,&b,&c);
printf("a=%d b=%d %d",a,b,c);
printf("\n----------------------------------------------\n");
printf("Enter two 4-digits numbers x and y\n");
scanf("%2d %4d",&x,&y);
printf("x=%d y=%d",x,y);
printf("\n-----------------------------------------------\n");
printf("Enter two integrers numbers a and x\n");
scanf("%d %d",&a,&x);
printf("a=%d x=%d ",a,x);
printf("\n-----------------------------------------------\n");
printf("Enter a 9-digits numbers \n");
scanf("%3d %4d %3d",&p,&q,&r);
printf("p=%d q=%d r=%d ",p,q,r);
printf("\n-----------------------------------------------\n");
printf("Enter two three digits numbers\n");
scanf("%d %d",&x,&y);
printf("x=%d y=%d",x,y);
getch();
}

Chapter – 4 : Managing Input and Output Operation


Formatted Input: Inputting Integer Numbers

Chapter – 4 : Managing Input and Output Operation


Formatted Input: Inputting Real Numbers

■ No field width like integers, so only %f is


used
■ For double %lf is used
scanf (“%f %f %f”, &x, &y, &z);

Case 1: For input 475.89, 43.21E-1 and 678


x=475.89, y=4.321 and z=678

Note: A number may be skipped using %*f.

Chapter – 4 : Managing Input and Output Operation


Formatted Input: Inputting Real Numbers
void main()
{
float x,y;
double p,q;
clrscr();
printf("Enter value of x and y\n");
scanf("%f %e",&x,&y);
printf("\n x=%f y=%f",x,y);
printf("\n----------------------------------------------\n");
printf("Enter value of p and q\n");
scanf("%lf %lf",&p,&q);
printf("p=%.12lf q=%12e",p,q);

getch();
}

Chapter – 4 : Managing Input and Output Operation


Chapter – 4 : Managing Input and Output Operation
Formatted Input: Inputting Character Strings
%ws or %w c
Characte
r to be
read

%s specifier can’t read the blank space

Chapter – 4 : Managing Input and Output Operation

You might also like