Download as ppsx, pdf, or txt
Download as ppsx, pdf, or txt
You are on page 1of 43

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 of Number to


specification a number be read

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
Character
to be read

%s specifier can’t read the blank space

Chapter – 4 : Managing Input and Output Operation


Formatted Input: Inputting Character Strings
void main()
{

char n1[5],n2[5],n3[5];
clrscr();
printf("Enter value of n1=>");
scanf(" %5c",n1);
printf("\n n1=%5c",n1);
printf("\n----------------------------------------------\n");
printf("Enter value of n2=>");
scanf(" %s",n2);
printf(" n2=%s",n2);
printf("\n----------------------------------------------\n");
printf("Enter value of no and n3=>");
scanf(" %c",n3);
printf(" n3=%s",n3);

getch();
}
Chapter – 4 : Managing Input and Output Operation
Formatted Input: Inputting Character Strings

Chapter – 4 : Managing Input and Output Operation


Formatted Input: Inputting Character Strings
void main()
{

char n1[100];
clrscr();

printf("Enter value of n1=>");


scanf(" %[a-h]",n2);
printf(" n1=%-100s",n1);

getch();
}

Chapter – 4 : Managing Input and Output Operation


Formatted Input: Inputting Character Strings
void main()
{

char n2[100];
clrscr();

printf("Enter value of n2=>");


scanf(" %[^x]",n2);
printf(" n2=%-100s",n2);

getch();
}
Chapter – 4 : Managing Input and Output Operation
Formatted Input: Inputting Character Strings
void main()
{

char n1[100];
clrscr();

printf("Enter value of n1=>");


scanf(" %[a-z ]",n2);
printf(" n1=%-100s",n1);

getch();
}

Note: %s can allow the space with the help of %[ ] specification.


Chapter – 4 : Managing Input and Output Operation
Reading Mixed Data Types
 It is possible to use one scanf statement to input a data
line containing mixed data
 Example:

scanf(“%d %c %f %s”, &count, &code, &ratio, name)

Input value would be 1 a 1.756 coffee

 Note: In above example following input would create an error

1 a coffee 1.756 (Expected float value instead of coffee which is


string ,3rd specifier is float (%f) )

Chapter – 4 : Managing Input and Output Operation


Code Meaning
%c Single character
%d Decimal Integer
%e Floating point value
%e Floating point value
%g Floating point value
%h Short Integer
%i Decimal, Hexadecimal or Octal Integer
%o Octal Integer
%s String
%u Unsigned Decimal
%[…] String of word(s)

Following letters may be used as prefix


h For short integers
l For Long integers or double
L For long double

Chapter – 4 : Managing Input and Output Operation


 Rules for scanf:

1. Each variable to be read must have a field specification

2. For Each field specification, there must be a variable address of proper


type

3. A non-whitespace character used in format string must have a


matching character in the user input.

4. Never end the format string with whitespace. It is an fatal error!

5. The scanf reads until:


A. The white space character found in a numeric specification or

B. The maximum number of character have been read or

C. An error is detected or

D. The end of file is reached


Chapter – 4 : Managing Input and Output Operation
Formatted output

 printf function used for printing caption

printf(“control string”,arg1,arg2,…,argn);

 Example:
1. printf(“Hello World\n”);

2. printf(“x=%d\n”, x);

Chapter – 4 : Managing Input and Output Operation


Output of Integer Numbers
%w d
Minimum field Values to be
width of output printed

 Number is given right justified in given field width


Format

printf(“%d”,9876) 9 8 7 6

printf(“%6d”,9876) 9 8 7 6

printf(“%-d”,9876) 9 8 7 6

printf(“%06d”,9876) 0 0 9 8 7 6
The minus(-) and zero (0) are known as flags. Which is used for forceful left-
justified alignment

Chapter – 4 : Managing Input and Output Operation


Output of Integer numbers
void main()
{

int a=12345;
clrscr();

printf("a=%d \n",a);
printf("a=%10d \n",a);
printf("a=%010d \n",a);
printf("a=%-d \n",a);
printf("a=%10d \n",-a);
getch();
}
Chapter – 4 : Managing Input and Output Operation
Output of Real Numbers
%w.p f
Minimum field Numbers to be read
width of output after decimal point

 The default precision is 6.


 So the field width w should satisfy the condition w>=p+7
 The value will be round off and right justified in the field of w
columns
 The minus(-) and zero (0) are known as flags. Which is used for
forceful left-justified alignment

Chapter – 4 : Managing Input and Output Operation


Output of Real Numbers
Format of y=98.7654:

printf(“%7.4f”,y) 9 8 . 7 6 5 4

printf(“%7.2f”,y) 9 8 . 7 7

printf(“%-7.2f”,y) 9 8 . 7 7

printf(“%f”,y) 9 8 . 7 6 5 4

printf(“%10.2e”,y) 9 . 8 8 e + 0 1

printf(“%11.4e”,-y) - 9 . 8 7 6 5 e + 0 1

printf(“%-10.2e”,y) 9 . 8 8 e + 0 1

printf(“%e”,y) 9 . 8 7 6 5 4 0 e + 0 1

Chapter – 4 : Managing Input and Output Operation


Output of Real Numbers

 Another way….

printf(“%*.*f”,width,precision,number);

 Example:

printf(“*.*f ”,7,2,number); and

printf(“%7.2f”,number); both are equal.

Chapter – 4 : Managing Input and Output Operation


Output of Real Numbers
void main()
{

float a=98.7654;
clrscr();

printf("a=%f \n",a);
printf("a=%7.4f \n",a);
printf("a=%7.2f \n",a);
printf("a=%-7.2f \n",a);
printf("a=%07.2f \n",a);
printf("a=%*.*f \n",7,2,a);
printf("\n");
printf("a=%10.2e \n",a);
printf("a=%12.4e \n",-a);
printf("a=%-10.2e \n",a);
printf("a=%e \n",a);

getch();
}
Chapter – 4 : Managing Input and Output Operation
Output of Single Character
%w c
Minimum field Character to be
width of output read

 By default Right-Justified

 Default value of w is 1

 The minus(-) and zero (0) are known as flags.


Which is used for forceful left-justified alignment

Chapter – 4 : Managing Input and Output Operation


Output of Strings
%w.ps

Minimum field Number of starting


width of output characters to be
displayed

 By default Right-Justified

Chapter – 4 : Managing Input and Output Operation


Output of Strings
Format of “NEW DELHI 110001”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

%s N E W D E L H I 1 1 0 0 0 1

%20s N E W D E L H I 1 1 0 0 0 1

%20.10s N E W D E L H I

%.5s N E W D

%-20.10s N E W D E L H I

%5s N E W D E L H I 1 1 0 0 0 1

Chapter – 4 : Managing Input and Output Operation


Output of Strings
void main()
{

char a='A';
char name[20]="ANIL KUMAR GUPTA";
clrscr();
printf("Output of the characters\n\n");
printf("%c\n%3c\n%5c\n",a,a,a);
printf("%3c\n%c\n\n",a,a);
printf("Output of the strings\n\n");
printf("%s\n",name);
printf("%20s\n",name);
printf("%20.10s\n",name);
printf("%.5s\n",name);
printf("%-20.10s\n",name);
printf("%5s\n",name);

getch();
}

Chapter – 4 : Managing Input and Output Operation


Previous Year Questions
 Explain the functions isalpha( ) and toupper( ).
 Explain format specification to print an integer number. %

wd printf(“%4d”,123);
 How to input a sentence (words with space) in C language?

 Explain any two functions of ctype.h file.

 Explain the following functions:

1. getc() 2. putc() 3. gets() 4. getchar() 5. putchar()


 The getchar() function accepts any character typed in. This
also includes RETURN and TAB. True or false?
 isdigit() is a function of ctype.h file which checks weather

the argument is character or not. True or false?

Chapter – 4
7 : Managing Input and Output Operation

You might also like