Mastering Input and Output Operations in C
The ability to efficiently handle input and output operations is crucial for any C programmer. In this comprehensive guide, we'll explore the different methods for performing these operations, focusing on both formatted and unformatted approaches.
- Formatted Input / Output Functions
- Unformatted Input / Output Functions
Formatted Input / Output Functions
There are two formatted input/ouput functions.
- scanf() : used for obtaining formatted input.
- printf() : used for obtaining formatted output.
scanf() :
The scanf() function is used to read the values for variables from standard input device i.e., keyword.
Syntax : scanf(" control string ", address_list);
Control String :
- Control string is enclosed within double quotes
- It specifies the type of values that are to be read from keyboard.
- Control string consists of fields specification written in the form % field specified.
Address_list :
It contains addresses of input/output variables preceded. The addresses are specified by preceding a variable by & operator.
Example : scanf("%d %f %c", &i, &a, &b);
When user enters 10, 5.5 , z from keyboard, 10 is assigned to i, 5.5 is assigned to a, and z is assigned to b.
We can also specify field widths in field formats as % field width type specifier.
Example : scanf("%3d %2d", &a, &b);
If we give input 500 and 10, then 500 is assigned to a and 10 is assigned to b.
It is not always advisable to use field width specifiers in scanf statements.
It may sometimes assign wrong values to variables
Example : scanf("%2d %3d", &a, &b);
Here, if we give 5004 and 10 as input, then 50 is assigned to a and 04 to b. which is wrong assignment. hence, generally field widths are not used with scanf statement.
Field formats for different data types are given below
Format | Type of value |
---|---|
%d | Integer |
%f | Floating numbers |
%c | Character |
%ld | Long integer |
%u | Unsigned integer |
%lf | Double |
%s | String type |
%x | Hexadecimal |
%o | Octal |
%i | Decimal, hexadeciaml or octal |
%e | Floating point numbers in exponential form |
%g | Floating point numbers with trailing zeros |
The follwoing letters may be used as prefix for certain conversion characters.
- for short integers
- for long integers or double
- for long double
printf() :
printf() function is used to print result on monitor.
Example : printf("control string", arg1, arg2,...argn);
Control String :
Control string can have,
- Format specifier given in the form % specifier.
- Escape sequence characters such as \t(tab), \n(new line), \b(blank) etc.
- It can have a string that must be printed on the standard o/p device, i.e monitor or console.
arg1, arg2,...argn are variables whose value must be printed on the monitor in the format specified in control string.
Examples :
- printf("%d %c", num, ch);
- printf("hello world");
- printf("%d \n %c", num, ch);
In example 3, suppose num has value 5 and ch has value a then output will be printed as,
5,
A
The output is printed in two different lines because new line character has been used between %d and %c.
Unformatted Input/output Functions :
Different functions in this category are as follows
- getchar()
- putchar()
- gets()
- puts()
- getch()
- putch()
getchar() :
This function returns single character entered form keyboard. No arguments are required for this function. By calling this function we can read a string
Syntax : var = getchar(); var is an identifier of char type
putchar(var) :
This function displays a single character on an output device.
gets() :
This function reads an input string
Syntax : gets(var); var is a character array
Example :
char name[50];
gets(name);
This statement reads a string from keyboard.
puts(var) :
This function displays the string stored in var on the output device, making it useful for presenting information.
5. getch()
6. putch()
These functions are additional tools for character-based input and output operations.
Example Programs:
1. Character Input and Output:
A program exemplifying the usage of getchar() and putchar() functions to receive and display characters from the keyboard
#include<stdio.h>
void main()
{
int i = 0;
char ch[10];
do
{
printf("Enter characters from keyboard : ");
i++;
ch[i]=getchar();
}
while(c[i]!=EOF);
for(i=1;ch[i]!='\0'; i++)
putchar(ch[i]);
}
input : Enter charactes from keyboard : abcd
Output : abcd
2. String Input and Output:
A program demonstrating the use of gets() and puts() functions to read and display strings.
#include<stdio.h>
void main()
{
char a[25];
printf("Enter the string : ");
gets(a);
puts(a);
}
input : Enter the string : Hello
Output : Hello
Conclusion:
Mastering input and output operations is fundamental for effective C programming. Whether utilizing formatted functions like scanf() and printf() or unformatted functions like getchar() and gets(), understanding their nuances ensures accurate and efficient data handling. Aspiring programmers can use these insights to enhance their skills and create robust C programs.
0 Comments