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

Input and Output in C Programming

The document discusses input and output operations in C programming. It describes how to read and write characters using getchar() and putchar() functions. It explains formatted input using scanf() to read integers, characters, and real numbers specifying field widths and formats. It also covers formatted output using printf() to display processed data with examples. The key functions, data types, and library functions for input/output operations in C are presented.

Uploaded by

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

Input and Output in C Programming

The document discusses input and output operations in C programming. It describes how to read and write characters using getchar() and putchar() functions. It explains formatted input using scanf() to read integers, characters, and real numbers specifying field widths and formats. It also covers formatted output using printf() to display processed data with examples. The key functions, data types, and library functions for input/output operations in C are presented.

Uploaded by

Drive User
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

CE143 : COMPUTER CONCEPTS & PROGRAMMING

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 – 54 : Constants,
Managing Input and Output
Variables Operation
& Data Types in ‘C’
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 – 54 : Constants,
Managing Input and Output
Variables Operation
& Data Types in ‘C’
Introduction

Processing Reading
Input:
1. X=5,a=0;
2. scanf();
Writing

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

Chapter – 54 : Constants,
Managing Input and Output
Variables Operation
& Data Types in ‘C’
Reading a Character

 getchar(): Reads single character


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

Chapter – 54 : Constants, Variables


Managing Input & Data Types
and Output in ‘C’
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 – 54 : Constants, Variables


Managing Input & Data Types
and Output in ‘C’
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 – 54 : Constants,
Managing Input and Output
Variables Operation
& Data Types in ‘C’
Chapter – 54 : Constants, Variables
Managing Input & Data Types
and Output in ‘C’
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 – 54 : Constants, Variables


Managing Input & Data Types
and Output in ‘C’
Operation
Chapter – 54 : Constants,
Managing Input and Output
Variables Operation
& Data Types in ‘C’
ctype.h Library Function

 Declares several functions that are useful for testing and


mapping characters.

Chapter – 54 : Constants,
Managing Input and Output
Variables Operation
& Data Types in ‘C’
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 – 54 : Constants,
Managing Input and Output
Variables Operation
& Data Types in ‘C’
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 – 54 : Constants,
Managing Input and Output
Variables Operation
& Data Types in ‘C’
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 – 54 : Constants,
Managing Input and Output
Variables Operation
& Data Types in ‘C’
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 – 54 : Constants,
Managing Input and Output
Variables Operation
& Data Types in ‘C’
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 – 54 : Constants,
Managing Input and Output
Variables Operation
& Data Types in ‘C’
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 – 54 : Constants,
Managing Input and Output
Variables Operation
& Data Types in ‘C’
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 – 54 : Constants,
Managing Input and Output
Variables Operation
& Data Types in ‘C’
Formatted Input: Inputting Integer Numbers

Chapter – 54 : Constants,
Managing Input and Output
Variables Operation
& Data Types in ‘C’
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 – 54 : Constants,
Managing Input and Output
Variables Operation
& Data Types in ‘C’
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 – 54 : Constants,
Managing Input and Output
Variables Operation
& Data Types in ‘C’
Chapter – 54 : Constants,
Managing Input and Output
Variables Operation
& Data Types in ‘C’
Formatted Input: Inputting Character Strings

%ws or %w c
Character
to be read

%s specifier can’t read the blank space

Chapter – 54 : Constants,
Managing Input and Output
Variables Operation
& Data Types in ‘C’

You might also like