×

Unformatted input() and output() function in C

Unformatted Input and Output functions take the character, character array, and strings as input. We can read-only character data types with these functions. These functions are already defined in the libraries. It reads only a single input from the user and displays the value on the console output screen.

Disadvantages of Unformatted Input and Output Functions

  1. This function doesn't allow giving input and output in the desired manner.
  2. In this function, there are no format specifiers in the syntax.
  3. In this function, the data is not stored in a user-friendly manner.
  4. Most of these functions, are used to store the character data or string data.

Types of Unformatted Input and Output Function in C

  1. getch()
  2. getche()
  3. getchar()
  4. putchar()
  5. gets()
  6. puts()
  7. putch()

1. getch():

getch() function is a built-in function declared in conio.h header file. It reads a single character from the keyboard. The  input character won't be displayed on the console Output screen. getch() function immediately returns the input character without pressing the enter key.

Syntax:

getch() or variable name= getch();

In this function we won't pass any parameters, and it returns a single character, but it won't be displayed on the console output screen.

Example program on getch() function:

// C program to implement getch() function in C
#include <conio.h>
#include <stdio.h>
int main()
{
printf("Enter any character: ");
getch(); // A character is entered, but it won't be displayed on the screen
return 0;
}

Output:

Enter any character:

2. getche():

getche() function is a built-in function declared in conio.h header file. It reads a single character from the keyboard. The input character will be displayed on the console Output screen. getch() function immediately returns the input character without pressing the enter key.

Syntax:

getche() or variable name= getche();

In this function, we won't pass any parameters, and it returns a single character, but it will be displayed on the console output screen.

Example program on getche() function:

// C program to implement getche() function in C
#include <conio.h>
#include <stdio.h>
int main()
{
printf("Enter any character: ");
getche(); // A character is entered but it will be displayed on the screen immediately
return 0;
}

Output:

Enter any character: H

3. getchar():

getchar() function is a built-in function declared in stdio.h header file. It reads a single character from the keyboard if the user enters multiple characters or a single character.

Syntax:

Variable name= getchar();

In this function, we won't pass any parameters, and it reads a single character, but it can be displayed on the console output screen using output functions.

Example program on getchar() function:

// C program show the functionality of getchar() function
#include <stdio.h>
int main()
{
charch; // declaring a character variable
printf("Enter the character: ");
ch = getchar(); // reading the character from the keyboard
printf(" \nThe entered character is: %c", ch); // printing the entered character
return 0;
}

Output:

Unformatted Input and Output Function in C

In the output, we gave the input as "Hello", but in the output, it displayed only H. By this, we can say only the first character is considered in the getchar() function.

4. putchar():

putchar() is a built-in function present stdio.h header file. It displays a single character from the keyboard if the user enters multiple characters or a single character.

We can pass the character into the function or by passing a variable which has stored a character.putchar() prints a single character.

Syntax:

putchar(variable_name); 

It takes a single character or a variable as parameter. And return a character on the console output screen.

Example program on putchar() function:

// program to implement putchar function
#include <stdio.h>
int main()
{
	charch; // declaring the character
	printf("Enter any character: ");
// Reads a character
	scanf("%c",&ch);
	// Displays that character using putchar function
	printf(" \nThe entered character is: ");
	putchar(ch); // method 1
	printf("\n second method\n The passed character is: ");
	putchar('A'); // Method 2
	return 0;
}

Output:

Unformatted Input and Output Function in C

5. gets():

gets() is a built-in function present stdio.h header file. By using gets() function, we can read the collection of characters or a string. We should have to declare a character array to store the string. This function allows spaces in the string.

Syntax:

charstr[ size ]; //declaration of character array
gets(str);

This function takes a character or a string as a parameter. And it won’t have any return data type.

Example program:

// program to implement the gets() function in C
#include <stdio.h>
// Driver code
int main()
{
	charstr[100]; // declaration of character array str of size 100
	printf("Please enter a string: ");
	gets(str); // reading the string using gets() function
	printf(" \nThe Entered string is: %s",str); // printing the string
	return 0;
}

Output:

Unformatted Input and Output Function in C

6. puts():

puts() is a built-in function present stdio.h header file. By using puts() function, we can print the collection of characters or a string. We should have to declare a character array to store the string. This function also prints spaces between the characters.

Syntax:

puts( character array);

This function takes a character array or a string as a parameter. And returns a string or a character array on the console output screen.

Example:

// program to implement the gets() function in C
#include <stdio.h>
// Driver code
int main()
{
	charstr[100]; // declaration of character array str of size 100
	printf("Please enter a string: ");
	gets(str); // reading the string using gets() function
	printf(" \nThe Entered string is: %s",str); // printing the string using printf()
	printf(" \nThe Entered string is: ");
	puts(str); // printing the string using puts function
	return 0;
}

Output:

Unformatted Input and Output Function in C

7. putch():

putch() function is a built-in function in C present in conio.h header file. It is used to display a single character on the console output screen.

Syntax:

putch( variable name );

This function takes a single character or a variable as a parameter. And it returns a single character on the console output screen.

Example program:

//  program to implement the putch() function in C
#include <stdio.h>
// Driver code
int main()
{
charch;
printf("Enter any character:\n ");
ch = getch(); // reading the character using getch() function
printf("\nTheEntered character is: ");
    // Displays that character on the console output screen
putch(ch); // using putch() function
return 0;
}

Output:

Enter any character:
The Entered character is: H

Conclusion

In this article, we learned about the unformatted input and output functions, the definition and disadvantages of this function and discussed examples of each and every unformatted i/o function.


Related Topics

Continue in C

C language: C language is a procedure oriented programming language. We can say that it is a platform dependent language. C language is introduced by Dennis Ritchie in the year 1970. We...

2 minutes read.

Pyramid Pattern in C

Pyramid Pattern in C A pyramid is a polyhedron created by connecting a base that will be a polygon, and all the lateral faces are triangles. All the bases are connected...

5 minutes read.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 minutes read.

C String Manipulation programs without using Library Functions

C string manipulation programs without using Library Functions The string is the character list, and typically we operate with library functions to read and print the entire string, but here you...

6 minutes read.

Difference between C and Java

C programming and Java programming are two of the earliest programming languages. C programming follows a procedural approach whereas Java programming follows an object-oriented approach. Java programming is a part...

3 minutes read.

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.

Projects on C language in 2024

Introduction Most aspiring programmers learn the C language as their first high-level programming language. The most flexible language utilized in every industry is, without a doubt, C. Even after 50 years...

9 minutes read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.

Add two numbers using the function in C

Here we will learn how to add two numbers by creating function in C. Let’s learn this with help of example. Code: - #include <stdio.h> int add_two_no(int a, int b); int main(){   int first_num,...

1 minute read.

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.

What is String Comparison in C

String comparison is the process of comparing two strings (sequences of characters) to determine if they are equal, or if one is greater or less than the other. The comparison...

4 minutes read.

Flow chart of While loop in C

This is a flowchart that represents the process of executing the while loop in the C programming language.Generally, as we know there are three main components of while loop:1. The...

3 minutes read.

Purpose of a Function Prototype in C

A function prototype in C is a declaration of a function that specifies the function's name, return type and parameters. It has the following syntax: return_type function_name(parameter_list); For example, the prototype for...

4 minutes read.

Difference between rand() and srand() function in C

What is the rand()? The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

C and C++ Binary Files

What is a binary file? A file in which the content is written in binary format is called a binary file. A binary file is not a text file. There are...

7 minutes read.

Break, continue and goto statement in C

Break statement Break statement is used to break the process of a loop (while, do while and for) and switch case. Syntax: break; Example 1 while(test Expression) { // codes if(condition for break){ break; } // codes } Example 2 For(int it, condition,...

1 minute read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

C Array

An array is a collection of elements that are used to hold the fixed number of values of the same type. We cannot change the size and type of array...

2 minutes read.