×

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

What is a buffer in C?

A buffer is an area of memory set aside for the temporary storage of data. A data buffer (or just buffer) is a region of a physical memory storage used to...

5 minutes read.

Runtime vs compile time in C

In the C programming language, the often used terms in every step consist of compile time and runtime. The compile time is referred to the source code that will be...

4 minutes read.

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a...

2 minutes read.

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.

C Program to Find the Largest Number using Ternary Operator

In this tutorial, we will write some program to determine which of the provided integers is larger using the ternary operator, also known as the conditional operator in C. Ternary Operator: The ternary...

3 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

Write a program that produces different results in C and C++

In this tutorial, we'll explore several programs that, depending on whether they are developed using C or C++ compilers, will produce varying outputs. There are numerous similar programs, but we will just...

2 minutes read.

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

1 minute read.

Character Class Tests in C

Introduction The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple...

4 minutes read.

Calloc in C

The calloc() is a library function in C which is used for memory allocation. The calloc() function dynamically allocates multiple blocks of memory to complex data structures. This includes data structures...

3 minutes read.

GCD of Two Numbers in C

The GCD means the greatest common divisor of two or more integers, it is also called as hcf. The gcd returns the greatest integer of the given two integers that...

3 minutes read.

Long int in C

We use data type to avoid the type of data to be stored for a variable at the time of declaration.So, we can say that the variable type is known...

3 minutes read.

How to lock top row in Excel

How to lock top row in excel While working with an Excel spreadsheet, the user utilizes the rows and columns to enter the details under various headings. Sometimes while scrolling the...

4 minutes read.

How to use atoi() function in C

Introduction: The atoi() is a function used in C programming language. This function converts string characters to the integer value. The atoi() function converts the input string to the return type of...

4 minutes read.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 minutes read.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.

Deadlock Detection Program in C

What is Deadlock? A deadlock is a circumstance where a group in the process is halted because they are each holding onto resources while waiting for other methods to obtain them. Think...

5 minutes read.

Bigint (BIG INTEGERS) in C with Example

The maximum number of digits that a long, long int can have in C/C++ is 20. The issue is how to store the 22-digit number, which is difficult to do...

9 minutes read.

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 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.