
Understanding Input and Output Statements in C Programming
Learn about input and output statements, formatted and unformatted functions, the role of standard libraries in C language, and examples of functions like printf, scanf, getchar, and more that facilitate input and output operations in C programming.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
Input and Output Statements Input and Output Statements (Formatted and Unformatted) (Formatted and Unformatted)
Input and output In C language C programming language has standard libraries that allow input and output in a program. The studio.h or standard input-output library in C that has methods for input and output. Input output built-in functions in C falls into two categories: 1. Formatted input output (I/O) functions and 2. Unformatted input output (I/O) functions. printf() and scanf() are examples for formatted input and output functions and getch(), getchar(), gets(), puts(), putchar() etc. are examples of unformatted input output functions. Unformatted I/O functions are used only for character data type or character array/string and cannot be used for any other datatype.
Printf and Scanf function The standard input-output header file, named stdio.h contains the definition of the functions printf() and scanf(), which are used to display output on screen and to take input from user respectively. Format can be a simple constant string, but you can specify %s, %d, %c, %f, etc., to print or read strings, integer, character, or float respectively. There are many other formatting options available that can be used based on requirements.
getchar ( ) ; This function provides for getting exactly one character from the keyboard until and unless the enter key is pressed. This function is declared in stdio.h Example: char ch; ch = getchar ( ) ;
putchar (char) ; This function provides for printing exactly one character to the screen. This function is declared in stdio.h. Example: char ch; ch = getchar ( ) ; /* input a character from kbd*/ putchar (ch) ; /* display it on the screen */
gets() & puts() gets() & puts() Functions The gets() function reads a group of characters or strings from the keyboard by the user and these characters get stored in a character array. This function allows us to write space-separated texts or strings It reads data until either a terminating newline or EOF (end of file) occurs. It is declared in stdio.h header file The puts() function writes the string str and a trailing newline to stdout. str This is the pointer to an array of chars where the C string is stored.
#include<stdio.h> void main() { char str[100]; printf("Enter a string"); gets(str); puts(str); getch(); }
Difference between scanf and gets() The main difference between these two functions is that scanf() stops reading characters when it encounters a space, but gets() reads space as a character too. If you enter the name as Sanjay Kumar using scanf() it will only read and store Sanjay and will leave the part after space. But gets() function will read it completely.
getch() It reads a single character from the keyboard by the user but doesn t display that character on the console screen and immediately returned without pressing enter key. This function is declared in conio.h(header file) and is also used for hold the screen. int main() { printf("Enter any character: "); // Reads a character but // not displays getch(); return 0; } Output::Enter any character:
getche() getche() function reads a single character from the keyboard by the user and displays it on the console screen and immediately returns without pressing the enter key. This function is declared in conio.h.
putch() putch() function is used to display a single character which is given by the user and that character prints at the current cursor location. This function is declared in conio.h (header file)
Input/Output in C getc ( *file ) ; This function is similar to getchar( ) except the input can be from the keyboard or a file. Example: char ch; ch = getc (stdin) ; /* input from keyboard */ ch = getc (fileptr) ; /* input from a file */
Input/Output in C putc ( char, *file ) ; This function is similar to putchar ( ) except the output can be to the screen or a file. Example: char ch; ch = getc (stdin) ; /* input from keyboard */ putc (ch, stdout) ; /* output to the screen */ putc (ch, outfileptr) ; /*output to a file */