Input/Output functions in C

Input/Output functions in C

 

 

Input/Output Functions in C

C Language doesn’t have built in input output functions to carry out input output operations. Instead, it is implemented via standard library functions called header files.

What is a header file

Header file contains the declarations and prototypes of the basic input/output functions used in the program files called source file. Whole set of library functions is divided into category wise and saved in different header files having extension “.h” as per its purpose. These header files have the references to the input/output functions within them, which are connected to their actual definitions when the actual program is run.

Some most commonly used header files-

stdio.h – contains functions related to standard input output

math.h –     contains functions related to mathematics

conio.h – contains functions related to Console input output

string.h – contains functions related to string handling

alloc.h – contains functions related to dynamic memory allocation

dos.h – contains functions related to DOS interface

stdlib.h – contains functions related to data conversion

Basic Input /Output Functions

Input/Output Functions in C by Sanjiv Kumar

The basic input/output sub system consists of keyboard and monitor. keyboard serves as default input device and monitor/console as basic output device. So the functions used for basic input and output operations can be divided into two categories- formatted and unformatted.

Formatted Input/Output functions

Formatted Input/Output functions allows better presentable view of data from standard input and standard output devices. It allows the output be formatted as per user requirements.  Example printf() and scanf().

printf Function

printf stands for print in formatted manner. It is used to write data in the required format from the computer to a standard output device. printf function is used to output the arguments of the following types-

  • Single character
  • Constants
  • Variables
  • Expressions resulting in numerical values
  • Strings

Syntax

1              printf(“string”);

It is the simplest form and will write the string in the standard output device.

2              printf(“format string”,argumentlist);

This is the typical printf() command where output is generated under the control of format string .

  • Format string controls how printf function converts, formats and prints its arguments.
  • Argumentlist can be a single or multiple variable, constant or expression.

A simple program to print a table of 2. where printf() formats the output to look pleasant.

#include
void main()
{
int x, i;
for (i=1;i<=10;i++)
{
       x=2*i;
       printf("2 \t*\t 1\t =\t%d\n",x);
}
}

Scanf Function

Scanf meaning is to scan or read in formatted manner. Scanf function is used to read the data in the required format from the standard input device into the computer. This function can be used to input the arguments of the following types-

  • Single Character
  • Variables
  • Expressions resulting in numerical values
  • strings

Syntax

scanf(“format string”,address_list);

here format string controls how scanf scans , converts and stores it’s arguments as per format specifiers. address_list is a list of addresses where the input data items are to be stored.

#include
void main()
{
char name[10];
printf("May I know your name , Please\n");
scanf("%s",&name);
printf("Hello! %s, How are you ",name);
}

Note: Two format strings may or may not have spaces, if it have then it is ignored but if there is space after the last format string then scanf scans for one extra value but the same is not saved anywhere hence ignored.

Unformatted IO function

C Language also have unformatted io functions which are displayed on the standard output devices on as it basis. You can’t use formatting on these functions. There are character and string io functions which falls under unformatted input/output category.

Character Input/Output functions

Character io functions are mainly helpful in reading/writing single character at a time. Which in turn can be used to read/write a complete file until End Of File signal is encountered.

There are three basic io functions

getchar()

getchar() function returns a character that has been most recently typed by the user and echoes it on the screen when user presses an Enter key. It is mandatory to press enter key to get the character shown on the standard output device.

getche()

getche function returns the recently typed character and echoes it on the screen, user don’t have to press enter key to get it displayed. The main advantage of getche is that user don’t have to press enter key after typing each character

getch()

getch() function also returns the character that has been recently typed by the user but it doesn’t display the typed character on the screen. Also user don’t have to press enter key after typing a character. This function is used in password building programs because when user enters a password it is not displayed on the visual display unit.

putchar()  

putchar function is used to write the one character to the standard output device, although operating system can redirect the output to other devices as well.

#include
void main()
{
int ch;
while((ch=getchar())!=EOF)
                {
                putchar(ch);
                if(ch=='\n')putchar('\n');
                }
}

Note: This program needs to be terminated by EOF (End of File) signal. EOF signal is generated by pressing Control+z in MS-DOS Systems.

String IO Functions

A group of characters form a string. C language supports String input/output functions namely gets and puts

gets()

gets function is used to read string of characters from standard input file ie stdin (which is keyboard by default ). gets reads and stores characters in an array pointed to by str until \n or EOF is reached.  gets is also helpful in reading whitespaces ie blanks and tabs etc.

puts()

puts function is used to copy a null-terminated string to the standard output file (stdout) except the terminating NULL Character itself.

#include
void main()
{
char name[20];
puts(Please tell  Your Name...);
gets(name);
printf("Hello! %s",name);
}

 

Similar Posts :    Operators and Expressions in C                  Elements of C Language

You may also like :

How to Activate GST in Tally.ERP9 ?        How To Enable JavaScript in Browsers      What is New in HTML5

To Download Official TurboC Compiler from here

 

What is header file in c ?

Header file plays very important role in compiling and executing C programs, it contains the declarations and prototypes of the basic input/output functions used in the program files

What is the use of printf and scanf ?

printf means print in formatted manner and is used to display output of c program to standard output device ie monitor in formatted manner. scanf means scan in formatted manner , this function is used to read input from standard input device ie keyboard in formatted manner and saves the data in some memory location.

What is difference in getchar, getche and getch functions?

getchar() function returns a character that has been most recently typed by the user and echoes it on the screen when user presses an Enter key.
getche function returns the recently typed character and echoes it on the screen, user don’t have to press enter key to get it displayed.
getch() function also returns the character that has been recently typed by the user but it doesn’t display the typed character on the screen.
1 1 vote
Article Rating
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
yashodha
yashodha
1 year ago

Nice

trackback
1 year ago

[…] Arrays in C                                  Control Structures in C                         Input/Output functions in C […]

Please Subscribe to Stay Connected

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

DigitalSanjiv will use the information you provide on this form to be in touch with you and to provide updates and marketing.
Share via
Copy link
Powered by Social Snap
%d bloggers like this: