
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Unformatted Input and Output Functions in C Language
Unformatted input and output functions read a single input sent by the user and permit the display of the value as the output at the console.
Unformatted input functions
The unformatted input functions in the C programming language are used only for character data type or character string/array and cannot be used for any other datatype. These functions are used to read a single input from the user and this allows the user to display the value at the console.
The getchar() Function
This getchar() function reads only the first single character from the given keyboard that multiplies characters is typed by the user and this function reads one character at a time until the enter key is pressed. This function is declared in stdio.h file.
The syntax of getchar() function is as follows ?
Variablename=getchar();
This code declares a character variable a and assigns it with the value returned by the getchar() function, which reads a single character from the standard input.
Char a; a = getchar();
Example Program
The below example writes the user input to a file until EOF, then reads and prints the file's content word by word. This will also check the file before reading.
#include <stdio.h> int main() { char ch; FILE *fp; fp = fopen("file.txt", "w"); // open the file in write mode printf("enter the text then press cntrl Z:
"); while ((ch = getchar()) != EOF) { putc(ch, fp); } fclose(fp); fp = fopen("file.txt", "r"); printf("text on the file:
"); if (fp) { char word[100]; while (fscanf(fp, "%s", word) != EOF) { // read words from file printf("%s
", word); // print each word on separate lines } fclose(fp); // close file } else { printf("file does not exist
"); // tell the user that the file does not exist } return 0; }
When the above program is executed, it produces the following result ?
enter the text then press cntrl Z: This is an example program on getchar() text on the file: This is an example program on getchar()
The gets() Function
It reads a string from the keyboard by the user and these characters get stored in a character array. This function allows us to write separated texts or strings. This function is declared in stdio.h file. The syntax for gets() function is as follows ?
gets(variablename);
Example Program
#include <stdio.h> #include <string.h> int main() { char str[10]; printf("Enter your name:
"); fgets(str, sizeof(str), stdin); // Remove newline character if present str[strcspn(str, "
")] = '\0'; printf("Hello %s, welcome to Tutorialspoint", str); return 0; }
Following is the output of the above program ?
Enter your name: Madhu Hello Madhu welcome to Tutorialspoint
Unformatted output functions
The unformatted output functions in C programming language are as follows ?
The putchar() Function
It displays a single character on the monitor, taking one character as input and writing it to the standard output. This function is commonly used in programs that require character-by-character output.
The syntax for putchar() function is as follows ?
Putchar(variablename);
The putchar('a); function prints the character 'a' to a standard output.
Putchar(?a');
The puts() Function
The puts() function displays a group of characters or strings stored in a character array on the monitor.
The syntax for puts() function is as follows ?
puts(variablename);
This function call prints the string "tutorial" specified by a character to the standard output.
puts("tutorial");
Example Program
Following is the C program for putc() and getc() functions ?
#include <stdio.h> int main() { char ch; FILE *fp; fp = fopen("std1.txt", "w"); printf("enter the text. press Ctrl+Z:
"); while((ch = getchar()) != EOF) { putc(ch, fp); } fclose(fp); fp = fopen("std1.txt", "r"); printf("text on the file:
"); while((ch = getc(fp)) != EOF) { putchar(ch); } fclose(fp); return 0; }
When the above program is executed, it produces the following result ?
enter the text.press cntrl Z: This is an example program on putchar() text on the file: This is an example program on putchar()