Input-Output in C
There are three main functions of any program : it takes data as input,processes this data and gives output. The input operation involves movement of data from an input device(generally keyboard) to computer memory, while in output operation the data from computer memory to the output device (generally screen).
The input output is performed through a set of library function that supplied with every C compiler. These functions are formally not a part of the language but they are considered standard for all input-output operation of C.
There are several header files that are provide necessary information in support of the various library function. These header file are entered in the program using #include directive at the beginning of the program .
#include <stdio.h> for standard input output. similarly there are other header files like math.h, string.h, stdlib.h that should be included when certain library function are used.
In this article we discuss about the input functions scanf() and getchar() and the output functions printf(), and putchar().
Conversion Specification
conversion specification for printf()
conversion character | Meaning |
%c | Print a single character |
%d,%i | Print a integer |
%u | Print an unsigned integer |
%o | Print an unsigned Octal integer |
%x | Print a unsigned Hexadecimal Integer using a,b,c,d,e,f |
%X | Print a unsigned Hexadecimal Integer using A,B,C,D,E,F |
%f | Print a floating point number |
%e | Print a floating point number in exponential format |
%E | same as %e but it print E for exponent |
%g | Print a single character |
Conversion specification for scanf()
conversion character | Meaning |
%c | Print a single character |
%d,%i | Print a integer |
%u | Print an unsigned integer |
%o | Print an unsigned Octal integer |
%x | Print a unsigned Hexadecimal Integer using a,b,c,d,e,f |
%X | Print a unsigned Hexadecimal Integer using A,B,C,D,E,F |
%f | Print a floating point number |
%e | Print a floating point number in exponential format |
%E | same as %e but it print E for exponent |
%g | Print a single character |
Reading Input data
Input data can be entered into the memory from a standard input device (keyboard). The scanf () library function can used for entering input data. this function can take all types of values (numeric, character, string) as input. The scanf() function can be written as-
syntax : scanf( "Control string" , address_1, address_2, .......);
This function should have at least two parameters. first parameter is a control string, which contains conversion specification characters. It should be within the double quotes.
The other parameter are addresses of variables. The address of a variable is found by preceding the variable name by an ampersand(&) sign.
some examples of scanf() function:-
#include<stdio.h>
int main()
{
int marks;
scanf("%d",&marks);
return 0;
}
input : 56
In this example, the control string contain only one conversion specification character %d which implies that one integer value should be entered as input. this entered value will be stored in the variable marks.
#include<stdio.h>
int main()
{
char ch;
scanf("%c",&ch);
return 0;
}
input : E
In this example, the control string contain conversion specification character %c which implies that single character should be entered as input. This entered value will be stored in the variable ch.
#include<stdio.h>
int main()
{
float weight;
scanf("%f",&weight);
return 0;
}
input : 65.23
Here the control string contain conversion specification character %f which implies that floating point number should be entered as input. This entered value will be stored in the variable weight.
#include<stdio.h>
int main()
{
char str[20];
scanf("%s",str);
return 0;
}
input : Hello world!
Here the control string contain conversion specification character %s which implies that string should be entered as input. Note that the variable str is not preceded by ampersand(&) sign. The entered string will be stored in the variable str.
More than one value can also be entered by scanf() function shown in below :
#include<stdio.h>
int main()
{
char ch;
int num;
float height;
scanf("%c%d%f",&ch,&num,&height);
return 0;
}
input :
f
22
2.3
Writing Output Data
Output data can be written form computer memory to the standard output device(monitor) using printf() library function. with the function all type of values (numeric, character or string) can be written as output. the printf() function can be written as -
printf("control string" , variable_1,variable_2, .......);
In this function, control string contain conversion specification characters and text. It should be should be enclosed with in the double quotes.
#include<stdio.h>
int main()
{
printf("Welcome to Education ki duniya!");
return 0;
}
Output :
Welcome to Education ki duniya!
Here control string has only text and no conversion specification characters, so the output is only text.
#include<stdio.h>
int main()
{
int num;
printf("Enter the number:");
scanf("%d",&num);
return 0;
}
Output:
Enter the number:25
here also printf() does not contain any conversion specification character and is used to display a message that tells the user to enter his age.
#include<stdio.h>
int main()
{
int num=25;
char letter='A';
float value=2.65;
char str[30]="EKD";
printf("number = %d, letter = %c,
value = %f, str = %s",num,letter,
value,str);
return 0;
}
output :
number = 25, letter = A, value = 2.650000,
str = EKD
In this example control string contain a conversion specification character %d, %c, %f, %s, which mean that an integer, a character, a floating point , a string value will be displayed. The variables num has the integer value , letter has the character value , value has the floating value and str has string value to be displayed as output.
#include<stdio.h>
int main()
{
int dec=12;
printf("Octal equivalent of decimal %d = %o",dec,dec);
printf("\nHexa equivalent of decimal %d = %x",dec,dec);
return 0;
}
Output :
Octal equivalent of decimal 12 = 14
Hexa equivalent of decimal 12 = c
Here the conversion specification character %o and %x , thus the octal and Hexa equivalent of the decimal number stored in the variable dec is displayed.
Here we'll see how we can use escape sequence in the printf statement. The most commonly used escape sequences used are '\n' and '\t'.
Escape Sequence in C
#include<stdio.h>
int main()
{
int num=25;
char letter='A';
float value=2.65;
char str[30]="EKD";
printf("number = %d\t letter = %c\t
value = %f\t str = %s\n",num,letter,
value,str);
printf("Hello world!");
return 0;
}
Output :
number = 25 letter = A value = 2.650000 str = EKD
Hello world!
********* I hope you Understand about Input - Output in C 😃😃 ********
Comments
Post a Comment