Input Output in C Language
In this article, lets learn How input and outputs are used in C programming language.
Topics covered in this Article.
1. What is input? and what is output in C programming?
2. Need for Input and Output?
3.Types of Input and Output Function in C.
A. Formatted Functions.
scanf(),printf()
B. Unformatted Functions.
getchar(),putchar(),gets(),puts()
Before to start learning this topic, first of all we are to learn .
1.What is input? and what is output in C programming?
Input: In General input is to provide something to the computer or any device is receving signal or command from outside resources is reffered as input.
command or signals are data sent to the computer for processing.
Output: Computer program creates many output. these include messages,printed documents,on screen data ,sounds and videos.
"Output is the information that it prints on paper,write to file or displays on screen as a result of particular program".
Output in C Programming mean displaying some data or information on the printer, the screen, or any other file. For generating output the C programming language has various built-in functions. These functions sends(process) data on any screen & printer, and also for saving the output in the form of binary files or texts.
"Feeding some data or information in the program is reffered as input".
Programmer or user can give the input from the command line or in the from of binary or text file.
The C programming language comes with a set of various built-in functions for reading the input and then feeding it to the available program as per our requirements.
2. Need of input output in C
When a programmer says input, it would mean that they are feeding some data or info in the program. They can give this input from the command line or in the form of any file. The C programming language comes with a set of various built-in functions for reading the input and then feeding it to the available program as per our requirements.
On the other hand, when a programmer says output, they mean displaying some data and information on the printer, the screen, or any other file. The C programming language also comes with various built-in functions for generating the output of the available data on any screen & printer, and also for saving the output in the form of binary files or texts.
3. Types of Input and Output Functions in C
In C, input refers to feeding information or data in the program. On the other hand, output refers to writing the data and information on any printer file and displaying it on the screen. A standard library .i.e stdio.h header file is available in the C language for reading any input and generating the output to be displayed on the console.
Input and Output Functions in C language are classified in to types.
A. Formatted Functions
B. Unformatted Functions
A. Formatted Functions.
The C Language comes with standard input output functions printf() and scanf() so that we can perform formatted input and output in a C program. C Language performs formatted input and output operations using standard input output formatted functions which is available in standard library file stdio.h
The formatted functions basically present or accept the available data (input) in a specific format. The standard library stdio.h in C contains various functions for the input-output operations. The scanf() and printf() functions help a programmer to format the functions in their desired format. Programmer use the formatted input and output functions for taking single or multiple inputs from the user at console.These formated functions also allow user to display single or multiple values.
Lets Learn Some Important formated input output function in C Language.
i.printf(): C Program use this function for displaying a single or multiple values in the form of output for the user at console.
The printf() function basically gets defined in the header file stdio.h, and Programmer use it for showing the standard output at console. The printf function in C program is use to display or print the value of variable or any sample text.
This function is a part of the C language standard library "stdio.h" and allow programmer to format output in various ways.
Syntax of printf: printf("format String",args);
printf("format String",args);
Format String:It may have different format specifiers.It is format specifier to print the value of variables such as character,integer,long,bool etc.
format specifiers:
%d format specifier is to display integer value.
%c format specifier is to display single character value.
%f format specifier is to print floating point number value.
%s format specifier is to display string value.
%x format specifier is to display Hexadecimal value.
args: These are the list of variables names that corresponds to the list of format specifiers.
ii. scanf():
The scanf() Library function in C programming is a powerful tool for reading input from the user or from a file and storing it in variables.
The scanf() function is a standard Library function in C Programming used to reads all datatypes from the(user) standard input stream stdin into the locations(variables) given by the entries in argument-list. Each argument must list be a pointer to a variable with a type that corresponds to a type specifier in format-string.
By specifying format specifiers in the format string, you can read input values of different types, such as character,strings,integers, floating-point numbers.
The scanf in C accepts two values: formatted string and memory location of variables in which we wish to store input data obtained from the user.
Synatx: int scanf ( const char * format_string, &variable );
The two parameters passed to scanf function are.
format_string:
Like printf() function scanf() has format specifier or format string. For example, %c is used for character value, %d is used for integer numbers, etc.
&variable:
To store input data, scanf needs to known the memory address or location of a variable. The ampersand (&) operator or address of the operator is used to know memory address or a variable location.
For example, &salary is the address of salary.The value taken from the input is stored in the location pointed by the variable. This is why we pass the variable address in which we want to store the value.
B. Unformatted Functions in C :
C Programming language has following types of unformatted function
a. getchar()
b. putchar()
c.gets()
d. puts()
e. getc().
a. getchar(): is the most basic input function in C Programming Language that reads a single character from the standard input stream stdin(i.e Keyboard device), regardless of what it is, and returns it to the program. This standard library function is used when single character input is required from the user.
It is defined inside the standard input output stream <stdio.h> header file.
The standard input stream provides multiple functions to take the input from the user at the run time of a program.
The function reads the next available character from the screen and returns it as an integer. Programmer can use getchar() function inside a loop to read more than one character from the screen .
Syntax of getchar() in C :
int getchar(void);
getchar() function does not take any parameters.
Return Value
The input from the standard input is read as an unsigned char and then it is typecast and returned as an integer value(int).
EOF is returned in two cases:
When the file end is reached
When there is an error during the execution
b. putchar():The putchar is a function in the C programming language that writes a single character to the standard output stream, stdout.
The putchar function is specified in the C standard library header file <stdio.h>.
This function puts the passed character on the screen and returns the same character. This function displays only single character at a time. You can use this function in the loop in case you want to display more than one character on the screen.
Its syntax is as follows:
int putchar (int character);
The int character parameter for this function is mandatory, which is the character to be printed or written to stdout or console.if the writing is successful, the argument character is returned. Otherwise, end-of-file is returned.
Check the following example −
c.gets() function in C:
gets() is a pre-defined function in C Library which is used to read a string or a text line. This function reads input until it encounters newline or End Of File(EOF) function and store the input in a defined string variable. The gets() function does not stop reading input until it encounter newline character.
in simple words we can say that the gets() function stops its reading a soon as newline character is encountered. gets() function reads whitespace as string and does not stop when any whitespace is encountered.
Syntax:gets( variable name );
The c program below illustrates the use of the gets() function
#include<stdio.h>
int main()
{
char name[10];
printf("Enter Your Favourite Programming: ");
gets(name);
printf("\n your favourite programming is %s",name);
return(0);
}
Output:
Enter Your Favourite Programming:
C Language
your favourite programming is C Programming
Program Explanation:
In the program given above we declared name variable of character type which can store 10 character i.e char name[10].
after that printf() function shows the message -->Enter Your Favourite Programming:
at this point compiler waits for the name to be input from the user or stdin.
next we entered the name C language . at this point the gets(name) reads the value entered and store it in name variable. Here the gets() function accepts white spaces also and contine reading character till EOF encountered.
Next at the end printf() shows the output->
your favourite programming is C Programming
d. puts() function in C
puts() function is the standard library function defined in stdio.h header file. This function in c programming is used to write a line or string to standard output stream(stdout) or console.
puts() function does not include null character. It prints the passed string along with a newline i.e.’\n’ and returns an integer value. The return value of this function depends on success of the program.
Syntax:
int puts(const char* str);
Here in the given syntax , str is the constant string that is to be printed.
Parameters:
• str − This is the constant string is to be written on stdout.
Return Value:
On successful execution the function returns non-negative value. On error or unsuccessful execution, the function returns EOF
e. getch() in C:
getch() function in C programming language is very usefil to get or read a character from the user. getch stands for 'get character'. This function also used to hold the output screen unles user press the key on the keyboard.
While this is not a part of the C standard, this is still a POSIX C function.This function takes in a single character from the standard input (stdin) keyboard, and returns an integer.
. Using getch() function, we can hide the input character provided by the users.
Syntax: int getch(void);
Parameters: The getch() function has void parameter means this function does not accept any parameter from the end user or standard input.