You are on page 1of 6

Unit III- Input Output Statements

1. printf, scanf functions


2. getchar, putchar, getch functions
3. gets, puts functions
4. Escape sequence characters
5. Format specifiers (Refer notes of Unit II)

Input means to provide the program with some data to be used in the program
and Output means to display data on screen or write the data to a printer or a file.
C programming language provides many built-in functions to read any given input
and to display data on screen when there is a need to output the result.

scanf() and printf() functions
The standard input-output header file, named stdio.h contains the definition of the
functions printf() and scanf(), which are used to display output on screen and to
take input from user respectively.
#include<stdio.h>
void main()
{
// defining a variable
int i;
/*
displaying message on the screen
asking the user to input a value
*/
printf("Please enter a value...");
/*
reading the value entered by the user
*/
scanf("%d", &i);
/*
displaying the number as output
*/
printf( "\nYou entered: %d", i);
Prof. Farzana Azeem Sheikh Page 1

Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC
Unit III- Input Output Statements

}
Note:
C language is case sensitive. For example, printf() and scanf() are different from
Printf() and Scanf(). All characters in printf() and scanf() functions must be in
lower case.

Practice Programs:

1. WAP to read a number and display it.


2. WAP to read a float no. and a character and display it.
3. WAP to accept two nos. and display average of it.
4. WAP to swap values of two nos.

Prof. Farzana Azeem Sheikh Page 2

Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC
Unit III- Input Output Statements

The getchar() and putchar() Functions


The int getchar(void) function reads the next available character from the screen
and returns it as an integer (Since char values are stored as an int). This function
reads only single character at a time. You can use this method in the loop in case
you want to read more than one character from the screen.
The int putchar(int c) function puts the passed character on the screen and returns
the same character. This function puts only single character at a time. You can use
this method in the loop in case you want to display more than one character on the
screen. Check the following example −

#include <stdio.h>
int main( )
{
int c;
printf( "Enter a value :");
c = getchar( );
printf( "\nYou entered: ");
putchar( c );
return 0;
}
When the above code is compiled and executed, it waits for you to input some text.
When you enter a text and press enter, then the program proceeds and reads only a
single character and displays it.
Practice Programs:
1. WAP to read a character and display it.
2. WAP to read a character and display its ASCII value.
3. WAP to read a character and display its previous and next character.

Prof. Farzana Azeem Sheikh Page 3

Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC
Unit III- Input Output Statements

Getch() function
Declaration: int getch(void);
This function waits for any character input from keyboard. But, it won’t echo the
input character on to the output screen

EXAMPLE

This is a simple Hello World! C program. After displaying Hello World! in output


screen, this program waits for any character input from keyboard. After any single
character is typed/pressed, this program returns 0. But, please note that, getch()
function will just wait for any keyboard input (and press ENTER). It won’t display
the given input character in output screen.
#include <stdio.h>
int main()
{
    /* Our first simple C basic program */
    printf("Hello World! ");
    getch();
    return 0;

}
OUTPUT:
Hello World!

Prof. Farzana Azeem Sheikh Page 4

Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC
Unit III- Input Output Statements

gets() and puts() functions


 gets() : Reads characters from the standard input and stores them as a string.
 puts() : prints characters from the standard output.Just like printf statement.
Syntax for gets() and puts()
gets(char_array_variable);
puts(char_array_variable/string);

Example:
#include <stdio.h>
int main()
{
char sentence[50];
printf("Enter your sentence\n");
gets(sentence);//read input from entered by the user
printf("your sentance here\n\n");
puts(sentence); //display the sentence
return 0;
}

Practice Programs:
1. WAP to read name of a student and marks of 3 subjects. Display total
marks and percentage of student with his/her name.

Prof. Farzana Azeem Sheikh Page 5

Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC
Unit III- Input Output Statements

2. WAP to find area of circle and rectangle.

Theory Questions:
1. Explain the following functions with examples:
a. printf()
b. scanf()
c. getchar()
d. putchar()
e. gets()
f. puts()
g. getch()

Prof. Farzana Azeem Sheikh Page 6

Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC

You might also like