0% found this document useful (0 votes)
24 views19 pages

C Programming-Chapter 04

Chapter 4 discusses managing input and output operations in C programming, detailing how to read data using functions like getchar() and scanf(), and display data using putchar() and printf(). It explains formatted input and output, including the use of control strings and conversion characters. The chapter also includes examples and assignments to reinforce understanding of these concepts.

Uploaded by

flxv07
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)
24 views19 pages

C Programming-Chapter 04

Chapter 4 discusses managing input and output operations in C programming, detailing how to read data using functions like getchar() and scanf(), and display data using putchar() and printf(). It explains formatted input and output, including the use of control strings and conversion characters. The chapter also includes examples and assignments to reinforce understanding of these concepts.

Uploaded by

flxv07
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
You are on page 1/ 19

WELCOME

PRESENTED BY
SHEYONA G
CHAPTER-04
Managing Input and
Output Operations
Input & Output

INPUT
 Input means to feed some data into a program.
 An input can be given in the form of a file or from the command line.
 C programming provides a set of built-in functions to read the given input and feed it
to the program as per requirement.

OUTPUT
 Output it means to display some data on screen, printer, or in any file.
 C programming provides a set of built-in functions to output the data on the
computer screen.
Standard Files

The file pointers are the means to access the file for reading and writing purpose.
It explains how to read values from the screen and how to print the result on the screen.
getchar()

 getchar() function is used to read a single character from the keyboard

 Once the program encounters the getchar() function it will wait for the user to

enter data by keyboard.

 Once it is entered it will be stored in a variable


Example for getchar()
#include <stdio.h>
int main(void)
{
char var;
printf( "Do you like C programming ? Press Y or N ");
var = getchar();
if(var == 'Y')
{
printf(" Yes C is very interesting language");
}
else
{
printf (" Why ? It is easy to learn here, need practice only");
}
return 0;
}
putchar()
 It is written to display one character by using putchar function in C programming
language.

Example for putchar()

#include <stdio.h>
int main(void)
The output would be:
{ a
putchar('a');
return 0;
}
FORMATTED INPUT & OUTPUT
 Formatted input and output refers to an input or output data that has been arranged
in a particular format.
 It enables the user to specify the type of the data and the way in which it should be
read in or written out.
Formatted input: scanf()

 scanf() function reads user input from the keyboard and stores it in variables
 It’s a part of standard file stdio.h

Syntax:
scanf ("control string", varl, var2,............ varn);
Control string:

 It is a sequence of one or more character groups.


 Each character group is a combination of the % symbol and one of the conversion
characters
 The control string specifies the type of the values which are to be supplied to the
variables.
Rules:

1. Control string must be enclosed within the double quotes


2. For every input variable, there must be one character group.
3. Each character group should begin with a % and followed by a conversion character
4. Multiple numbers of character groups can be allowed within the control string.
In such a case it can be separated by blank spaces or commas.
5. Each input variable in the address list must be preceded by an ampersand
eg: &var, &x for string input variable, & is not used.
6. All the address list variables are separated by commas. Don't use blank spaces
7. Address list is not enclosed within double quotes.
8. The values for the address list should map in number, type and order of variables.
9. There must be a comma to separate the control string and the address list
EXAMPLE 1: EXAMPLE 2:

#include<stdio.h> #include <stdio.h>


void main() void main()
{ {
int a; int number1, number2, sum;
printf("Enter the number\n"); printf("Enter two integers: ");
scanf("%d",&a); scanf("%d %d", &number1, &number2);
if(a%2==0) sum = number1 + number2;
printf("Number is Even\n"); printf("%d + %d = %d", number1, number2, sum);
else }
printf("Number is Odd\n");
}
Formatted output: printf() function
 printf() function to display the output on the monitor

Syntax:
printf("control string and conversion specifiers", variable list);

The format string can contain:


a. Characters that are simply printed as they are
b. Conversion specifications or format specifications that begin with a % sign
c. Escape sequences (Backslash characters) that begin with a \ sign
void main() #include<stdio.h>
{ void main()
printf("Hello World");
{
}
int a;
printf("Enter the number\n");
scanf("%d",&a);
if(a%2==0)
printf("Number is Even\n");
else
printf("Number is Odd\n");
}
Assignments

1. Explain getchar() function with example


2. Explain putchar() function with example
3. Write one example for reading an integer variable using scanf() function
4. Write one example for displaying an integer variable using print() function
5. Write a C program to find sum of two integer numbers using scanf() and printf() function.
6. How will you read an integer and float variable at a time using a scanf() function?
Assignments

1. The unformatted input function ___________ is used to read a character from keyboard.
2. The unformatted output function ___________ is used to write a character on monitor.
3. The formatted output function ___________ is used to write a value on monitor.
4. The formatted input function ___________ is used to read a value from keyboard.
5. The control string ___________ is used in scanf() to read an integer type variable.
6. The control string ___________ is used in scanf() to read an character type variable.
7. The control string ___________ is used in scanf() to read an float type variable.
8. The control string ___________ is used in scanf() to read an hexadecimal type variable.

You might also like