Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Conio.h in C?
2.1.
Syntax
3.
What is the use of conio.h in C?
4.
Functions of Conio.h in C
4.1.
1. clrscr()
4.1.1.
Example:
4.2.
C
4.3.
2. getch()
4.3.1.
Example:
4.4.
C
4.5.
3. putch()
4.5.1.
Example:
4.6.
C
4.7.
4. getche()
4.7.1.
Example:
4.8.
C
4.9.
5. cscanf()
4.9.1.
Example:
4.10.
C
4.11.
6. cprintf()
4.11.1.
Example:
4.12.
C
4.13.
7. cgets()
4.13.1.
Example:
4.14.
C
4.15.
8. cputs()
4.15.1.
Example:
4.16.
C
4.17.
9. textbackground()
4.17.1.
Example:
4.18.
C
4.19.
10. textcolor()
4.19.1.
Example:
4.20.
C
4.21.
11. gotoxy
4.21.1.
Example:
4.22.
C
4.23.
12. kbhit() 
4.23.1.
Example:
4.24.
C
4.25.
13. wherex and wherey
4.25.1.
Example:
4.26.
C
5.
Advantages of using Conio.h
6.
Disadvantages of using Conio.h
7.
Frequently Asked Questions
7.1.
Why do we use #include conio h in C?
7.2.
Is it necessary to use conio.h in C?
7.3.
What is getch () in C?
7.4.
How to install conio h for C?
7.5.
What is the use of Stdio H and Conio H in C?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

What is conio.h in C?

Author Nidhi Kumari
1 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Welcome to our blog! Here, we're going to talk about something cool: conio.h in C programming. Have you ever wanted to make your computer programs more interactive and fun to use? Well, that's where conio.h comes in. It's like a secret tool that few people know about, but it can make a big difference in how your programs work.

What is Cohio.H in C?

Conio.h is also a header file in C and is used to include input-output library functions. In this article, we will cover the conio.h header file in C.

Also see: C Static Function

Click on the following link to read further: Features of C Language

What is Conio.h in C?

The ‘conio.h’ header file is a C library that provides functions for handling console input and output operations. It is a non-standard C header file i.e., it is not part of the official C language specification. It is not supported by modern compilers such as GCC.

Syntax

The syntax of the conio.h file in C is as follows:

#include <conio.h>
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is the use of conio.h in C?

The conio.h header in C is not part of the standard C library but was used primarily in older DOS-based systems. It provides functions for console input and output, screen handling, and text-based interface development. Programmers used conio.h to create interactive text-based applications with features like colored text, cursor control, and keyboard input.

However, it's essential to note that conio.h is not portable and is not supported by modern compilers or operating systems. Standard input/output functions like printf and scanf have largely replaced its use in favor of more platform-independent and maintainable code. Programmers today typically avoid using conio.h in favor of more standardized approaches to achieve similar functionality.

Functions of Conio.h in C

some of the popular functions of conio.h in C

Let’s discuss some of the popular functions of conio.h in C as follows

1. clrscr()

You can use this function to clear the output screen.

Example:

  • C

C

#include<stdio.h>
#include<conio.h>


int main()
{
   printf("Welcome Ninja.");
   printf("\nThe clrscr() function will clear these two lines.");
   clrscr();
   printf("Clear Screen.");
   return 0;
}

Output:

Clear Screen. Press any key to continue.

 

Explanation:

The output only retains the string from the last printf, as the earlier outputs are cleared by the clrscr() function.

2. getch()

This function can get a single-character user input from the console.

Example:

  • C

C

#include<stdio.h>
#include<conio.h>

int main()
{
   printf("Enter a single character: ");
   getch();
   return 0;
}

Output:

Enter a single character: Press any key to continue.

 

Explanation:

In the output above, ‘press any key to continue’ is displayed after the user pressed some key on his keyboard.

3. putch()

The putch() function prints a single character on the output screen.

Example:

  • C

C

#include<stdio.h>
#include<conio.h>

int main()
{
   char ch = 'c';
   putch(ch);
   return 0;
}

Output:

c Press any key to continue.

 

Explanation:

In the output above, a single character ‘c’ is printed to the terminal.

4. getche()

You can use this function to get alphanumeric values. It displays the character that is provided as input and reflects the character on the screen.

Example:

  • C

C

#include<stdio.h>
#include<conio.h>
int main()
{
   printf("Enter your character input: ");
   getche();
   return 0;
}

Output:

Enter your character input: A Press any key to continue.

 

Explanation:

In the output above, ‘press any key to continue’ is displayed after the user pressed ‘A’ on his keyboard.

5. cscanf()

This function can take input from the console. The cscanf function works similarly to the scanf() function.

Example:

  • C

C

#include<stdio.h>
#include<conio.h>
int main()
{
   int num;
   cscanf("%d", num);
   return 0;
}

6. cprintf()

This function can print output to the console. The cprintf() function works similarly to the printf() function.

Example:

  • C

C

#include<stdio.h>
#include<conio.h>
int main()
{
   cprintf("Welcome Ninja.");
   return 0;
}

Output:

Welcome Ninja. Press any key to continue.

 

Explanation:

In the output above, ‘Welcome Ninja’ is displayed on the console, before the program terminates.

7. cgets()

You can input a string of characters from the console until carriage-return (CR) and linefeed(LF) are encountered.
Carriage-return(\r): It helps to move the cursor to the start of the current line.

Linefeed: It used to imply moving down to the next line.

They can be combined to create a new line(\n) effect.

Example:

  • C

C

#include<stdio.h>
#include<conio.h>

int main()
{
   char name[20];
   char *ptr;
   name[0] = 10;
   printf("User Input:");
   ptr = cgets(name);
   printf("\n Here is your output: %s", ptr);
   return 0;
}
Input:
User Input: Coding Ninjas.

 

Output:

Here is your output: Coding Ninjas. Press any key to continue.

 

Explanation:

The user input is stored in memory using cgets() function, and it is displayed using the printf() function.

8. cputs()

A series of characters is printed on the output screen using cputs() function.

Example:

  • C

C

#include<stdio.h>
#include<conio.h>
int main()
{
   cputs("Hello Ninja!");
   return 0;
}

Output:

Hello Ninja! Press any key to continue.

 

Explanation:

In the output above, cputs() function displays the given string to the console.

9. textbackground()

This method is used to change the text's background colour.

The syntax of the textbackground() method is as follows:

textbackground(COLOR);

 

Call this function with a parameter specifying the color in capital characters before displaying any content.

Example:

  • C

C

#include <stdio.h>
#include <conio.h>
int main()
{
textbackground(RED);
return 0;
}

10. textcolor()

This method is used to change the text's colour.

The syntax of the textcolor() method is as follows:

textcolor(COLOR);

 

Example:

  • C

C

#include <stdio.h>
#include <conio.h>

int main()
{

textcolor(BLUE);
return 0;
}

11. gotoxy

Using the gotoxy function, the cursor position can be changed. This function takes two parameters.

The syntax of the gotoxy() method is as follows:

void gotoxy(int x, int y);

 

Here, x and y are the positions.

Example:

  • C

C

#include <stdio.h>
#include <conio.h>
int main()
{
  int x, y;
  x = 20;
  y = 10;
  gotoxy(x, y);
  printf("Example of gotoxy.");
  getch();
  return 0;
}

These two values(x,y) were provided to the function gotoxy() as a parameter, and after program execution, the cursor location will be modified.

12. kbhit() 

Here kb stands for keyboard and kbhit results in keyboard hit. This feature enables us to determine whether or not a person has hit any keys. Knowing whether or not a player has hit a key is helpful in games. It is a non-standard library method.

Example:

  • C

C

#include <iostream.h>
#include <conio.h>
int main()
{
while (!kbhit())
printf("Press any key\n");
return 0;
}

The “Press any key” statement will keep showing on the console until the user hits a key on the keyboard.

13. wherex and wherey

You can find the cursor's current position using wherex and wherey method. Here x and y represent the x and y coordinates.

Example:

  • C

C

#include<stdio.h>
#include<conio.h>
int main()
{
   int a, b;
   a = wherex();
   b = wherey();
   return 0;
}

Advantages of using Conio.h

The advantages of using Conio.h are as follows:

  • Sharing of Data: The primary function of a conio.h header file is to transfer data between different files stored inside it. If you want to use various functions and view them all, you must store all the functions in the same file.
     
  • Reuse: You can reuse the desired function repeatedly by only including the separate header file.
     
  • Modularity: It maintains modularity by putting all related types of functions in a single file.
     
  • Less Complex Code: Including the header file decreases the lines of code, resulting in a less complex program.
     
  • Reduces Risk: Using appropriate header files can reduce the risk of unknown errors.
     

Disadvantages of using Conio.h

The disadvantages of using Conio.h are as follows:

  • Tight Coupling: Even if two files in your project are totally independent, it will still result in a tight coupling dependency between them.
     
  • Increased execution time: Regardless of whether it is required, every project header is included in every source file resulting in an increased number of operations and execution time.
     
  • Third-party libraries: Let's say that you created a module for your application. Let's imagine that this module implements an API. You then want to create a different app that should make use of this module.

    In this scenario, either you have to keep track of the header files you need to include, or you can simply copy your previous header file. Copying the file will also include many unnecessary third-party libraries. 

Frequently Asked Questions

Why do we use #include conio h in C?

This C statement is used for including the conio header file in your program which provides functions for handling console input and output operations.

Is it necessary to use conio.h in C?

No, it's not necessary to use conio.h in C. It's a non-standard header primarily used in older DOS-based systems for console I/O operations. Modern C programming typically relies on standard input/output functions like printf and scanf.

What is getch () in C?

getch() is a function in C that reads a single character from the keyboard without displaying it. It is commonly used to capture user input without immediately showing it on the screen.

How to install conio h for C?

To install conio.h for C, simply include it in your C program and link it during compilation. No separate installation is required; it's a standard C header file.

What is the use of Stdio H and Conio H in C?

stdio.h is for standard input/output functions, while conio.h provides functions for console input/output and screen control in C programming.

Conclusion

In this article, we extensively discussed the conio.h header file in C. We also discussed various functions defined inside this header file and the advantages of using a header file.

We hope this blog has helped you with your interview. We recommend you visit our articles on different topics of C, such as

If you liked our article, do upvote our article and help other ninjas grow.  You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Head over to our practice platform Coding Ninjas Studio to practise top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!!

Happy Reading!!

Previous article
C #Pragma
Next article
Addition of two numbers in C
Live masterclass