![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain unformatted input and output functions in C language
Unformatted input and output functions read a single input sent by the user and permits to display the value as the output at the console.
Unformatted input functions
The unformatted input functions in C programming language are explained below −
getchar()
It reads a character from the keyboard.
The syntax of getchar() function is as follows −
Variablename=getchar();
For example,
Char a; a = getchar();
Example Program
Following is the C program for getchar() function −
#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:
"); while ((ch=getc(fp))!=EOF){ 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 doesnot exist"); // then tells the user that the file does not exist. } } return 0; }
Output
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() ^Z text on the file: This is an example program on getchar()
gets()
It reads a string from the keyboard
The syntax for gets() function is as follows −
gets(variablename);
Example Program
#include<stdio.h> #include<string.h> main(){ char str[10]; printf("Enter your name:
"); gets(str); printf("Hello %s welcome to Tutorialspoint", str); }
Output
Enter your name: Madhu Hello Madhu welcome to Tutorialspoint
Unformatted output functions
The unformatted output functions in C programming language are as follows −
putchar()
It displays a character on the monitor.
The syntax for putchar() function is as follows −
Putchar(variablename);
For example,
Putchar(‘a’);
puts()
It displays a string on the monitor.
The syntax for puts() function is as follows −
puts(variablename);
For example,
puts("tutorial");
Example Program for Unformatted Input and Output
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 cntrl 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; }
Output
When the above program is executed, it produces the following result −
enter the text.press cntrl Z: This is an example program on putchar() ^Z text on the file: This is an example program on putchar()