Unit 3 Input Output Statements
Unit 3 Input Output Statements
Unit 3 Input Output Statements
Unit: 3
Input/output statements
1.1. Header files
1.2. Formatted input/output
1.3. Character input/output
1.4. Program using input/output
==============================================================
C Header Files
1) System defined header file: The header file which is predefined is known as a system defined header
file.
2) User-defined header file: The header file which is defined by the user is known as a user-defined header
file.
Include Syntax
Both the user and the system header files are included using the preprocessing directive #include. It has
the following two forms −
#include <file>
This form is used for system header files. It searches for a file named 'file' in a standard list of system directories.
#include "file"
This form is used for header files of your own program. It searches for a file named 'file' in the directory containing
the current file.
It is used for performing input and output operations with the help of using printf() and scanf() function.
#include<string.h>
It is used for performing string related functionalities like strlen(), strcmp(), etc.
#include<iostream>
It is used to perform input and output operations with the help of using cin and cout objects.
#include<math.h>
This header file contains some predefined math functions that perform mathematical operations, such as
sqrt(), log2(), pow(), etc.
Formatted input/output
C provides standard functions scanf() and printf(), for performing formatted input and output .These
functions accept, as parameters, a format specification string and a list of variables.
printf is used to print the contents that are inside the parenthesis of the printf statement.
Scanf is used to read and store the input given by a user to a variable. For scanf variable is necessary to
store the information given by the user
Voidmain ()
{
int a,b,c;
scanf(“%d %d”,&a,&b);
C=a+b;
Here we can see how the printf and scanf function are used in a program
Character Input/output
SINGLE CHARACTER INPUT-- THE getchar FUNCTION
Single characters can be entered into the computer using the C library function getchar. The function does
not require any arguments,though a pair of empty parentheses must follow the word getchar.
Syntax:
character variable= getchar();
PAGE \* MERGEFORMAT 1 | Page
C – PROGRAMMING
Ch=getchar();
Single character output can be displayed using the C library function putchar. The character being
transmitted will normally be represented as a character- type variable. It must be expressed as an
argument to the function, following the word putchar.
Syntax:
putchar(character variable);
………..
putchar(c);
Following program accepts a single character from the keyboard and displays it.
#include<stdio.h>
#include<conio.h>
void main()
char ch;
clrscr();
ch = getchar();
putchar(ch);
getch();
#include <stdio.h>
#include <string.h>
#include <math.h>
PAGE \* MERGEFORMAT 1 | Page
C – PROGRAMMING
int main()
{
char str1[10] = "hello";
char str2[10] = "javatpoint";
long int a = pow(3, 3);
printf("The value of a is %d", a);
int length = strlen(str2);
printf("\nThe length of the string str2 is : %d", length);
return 0;
}
In the above code, we have observed the usage of header files like string.h in which strlen() function is defined, and
pow() function is defined in math.h header file.
Output
printf() function
The printf() function is used for output. It prints the given statement to the console.
printf("format string",argument_list);
scanf() function
The scanf() function is used for input. It reads the input data from the console.
scanf("format string",argument_list);
Let's see a simple example of c language that gets input from the user and prints the cube of the given
number.
#include<stdio.h>
int main()
{
int number;
printf("enter a number:");
scanf("%d",&number);
printf("cube of number is:%d ",number*number*number);
return 0;
}
Output
enter a number:5
cube of number is:125
#include<stdio.h>
int main()
{
int x=0,y=0,result=0;
printf("enter first number:");
scanf("%d",&x);
printf("enter second number:");
scanf("%d",&y);
result=x+y;
printf("sum of 2 numbers:%d ",result);
return 0;
}
Output
enter first number:9
enter second number:9
sum of 2 numbers:18
void main()
int i;
scanf("%d", &i);
Syntax: gets(variable);
gets(name); // it will ask the user for a name and save the name into name.
puts()
This function is used to display text in the monitor which is stored in the variable. But variable is always string data
type.
Example:
puts(name);
# include<conio.h>
void main()
char name[100];
gets(name);
puts(name);
getch();
PAGE \* MERGEFORMAT 1 | Page
C – PROGRAMMING
Exercise 1
Very short questions:
1) What is header file?
2) How many types of header files?
3) Which preprocessing directive is used in header file?
4) What is the use of #include<stdio.h> header file?
5) Why we use printf() function in C?
6) Write a use of scanf()?
7) Which function is use for single character input?
8) Which function is use for single character output?
9) Write correct syntax of printf() function.
10) Write correct syntax of scanf() function.
Short questions:
1) What is header file? How many types of header files are available in C?
2) How can you take input integer and single character from user?
3) Write any two different header files available in C.
4) Write a simple program to add two numbers take input from user’s keyword.
5) Write two difference between getchar() and putchar() function.
6) Distinguish between scanf() and printf() function.
7) How can you take input single character through keyboard? Write an example.
8) What is the main difference between gets()and puts() functions?
9) Write simple use of gets() and puts() functions.
10) What is gets() function? How it is different from puts() function?
Long questions:
1) What are input/output functions of C language? Explain with examples.
2) Find out square root of a number using C program.
3) Differentiate between printf() and scanf() statements.
4) Enter two numbers and print sum, product, difference and division of numbers using C program.
5) Write a program to input name, age and salary and print them on screen.
12) Write a program to convert temperature in Centigrade (C) into Fahrenheit (F). [ F = 1.8C + 32 ]
13) Write a C program to input name, age and salary and print them on screen.
14) Write a program to calculate circumference of a circle having radius r. the radius r should be taken from user.
[C=2 πr ]
15) Write a C program that asks the name of student and marks obtained by him / her in five subjects. Display the
total marks obtained by student.
16) Write a program to read height and base of a triangle and find area of it.
17) Write a program to read Indian currency (INR) and convert into Nepalese currency (NPR). [ hint: use 100 INR =
160 NPR ]
18) Write a program to read cost price (CP) and selling price (SP) of a mobile for a shop and calculate profit or loss
percentage.
19) Write a C program to read two integers and find quotient value after dividing first integer by second integer.
Comment on the result.
20) Enter two numbers and print sum, product, difference and division of both numbers using C program.