Input Output Functions in C
Input Output Functions in C
Topics to be discussed
Header File
Standard Library
Syntax
Semantics
Operations
1
06-09-2024
Header File
Header File
2
06-09-2024
Standard Library
3
06-09-2024
Syntax
Semantics
4
06-09-2024
5
06-09-2024
6
06-09-2024
Types of Operations
Operation
Input Output
Input/Output operations
I/O operation in C language is performed through set of library
function supplied with complier.
There are set of header file which provides various library
function
A common used header file in C programming is stdio.h .
It is called standard input output header file.
The set of library functions that perform input-output operation
is known as standard input/output library (stdio.h)
Inside this header file there are two common functions.
printf() and scanf()
7
06-09-2024
Reading a Character
getchar();
Ex:
char variable_name;
variable_name=getchar();
Writing a Character
putchar(var_name);
Ex:
char c=getchar();
putchar(c);
8
06-09-2024
scanf()
This function is used for input purpose.
This function is used to read some data from the keyboard
and store it in the variable.
Syntax: scanf(“control string”, address of the variable);
Ex:-
int x; /* Declaration of an integer variable */
scanf(“%d”, &x); /* Input from keyboard for the variable x
*/
printf(“%d”, x); /* To print the value of the variable x */
scanf()
9
06-09-2024
Conversion Specifications
•The format specifier or the Conversion specification used by the printf() and scanf()
function specifies the type and size of data.
•Each format specifier must begin with a % sign .
Specifier meaning
%c a single character
%d or %i decimal integer
%f or %e or %g floating point number
%lf long range floating point
(double)
%Lf long double
%h short int
%s string
%u unsigned decimal integer
%o octal integer
%x hexadecimal
%[…] Read a string of words
Examples
int marks;
scanf(“%d”,&marks);
int basic,da;
scanf(“%d%d”,&basic,&da);
float x;
scanf(“%f”,&x);
double y;
scanf(“%lf”,&y);
10
06-09-2024
N.B
The modifier h can be used before conversion specifications d,i,o,u,x to specify
short integer.
The modifier l can be used before them to specify a long integer.
The modifier l can be used before conversion specification f,e,g to specify
double .
The modifier L can be used before f,e,g to specify long double.
Example:
%ld
%hd
%Lf
%hx
printf()
11
06-09-2024
Examples
printf(“Programming in C”);
printf(“\n”);
printf(“%d”,x);
printf(“x=%d\n”,x);
printf(“The value of a is %d”,a);
Integer Examples
printf(“%d”,9678); 9 6 7 8
printf(“%6d”,9678); 9 6 7 8
printf(“%2d”,9678); 9 6 7 8
printf(“%-6d”,9678); 9 6 7 8
printf(“%06d”,9678); 0 0 9 6 7 8
12
06-09-2024
Real Examples
Syntax: %w.pf
w indicates the number of digits used for display
p indicates the number of digits to be displayed after decimal
Let y=98.7654;
printf(“%7.4f”,y); 9 8 . 7 6 5 4
printf(“%7.2f”,y); 9 8 . 7 7
printf(“-7.2f”,y); 9 8 . 7 7
String Examples
Syntax: %w.ps
w specifies width of field
p specifies only first p characters of string are displayed
Ex:
char a[20]=“Hello World”;
printf(“%s”,a);
H e l l o W o r l d
13
06-09-2024
Statements
Comments
14
06-09-2024
Example 1
Example 2
/*This program calculates area of a rectangle*/
#include<stdio.h>
main()
{
float length,breadth,area;
printf(“Enter length: ”);
scanf(“%f”,&length);
printf(“Enter breadth: ”);
scanf(“%f”,&breadth);
area=length*breadth;
printf(“Area of rectangle is: %f”,area);
}
15