Professional Documents
Culture Documents
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:
Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC
Unit III- Input Output Statements
#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.
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
}
OUTPUT:
Hello World!
Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC
Unit III- Input Output Statements
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.
Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC
Unit III- Input Output Statements
Theory Questions:
1. Explain the following functions with examples:
a. printf()
b. scanf()
c. getchar()
d. putchar()
e. gets()
f. puts()
g. getch()
Farzana.nadaf@azamcampus.org
B.C.A. (Science) Department, AISC