C/C++ I/O Functions
Detailed explanation with simple
programs
getch()
• Reads one character from the keyboard without showing it. Useful
for password input or pausing the screen.
•
• Example Program:
• #include <conio.h>
• #include <iostream>
• using namespace std;
• int main() {
• char ch;
• cout << "Press any key: ";
• ch = getch();
• cout << "\nYou pressed: " << ch;
• return 0;
• }
getche()
• Reads one character from the keyboard and displays it immediately.
Useful for taking single key input with feedback.
•
• Example Program:
• #include <conio.h>
• #include <iostream>
• using namespace std;
• int main() {
• char ch;
• cout << "Press any key: ";
• ch = getche();
• cout << "\nYou pressed: " << ch;
• return 0;
gets()
• Reads a full line of text including spaces.
•
• Example Program:
• #include <stdio.h>
• int main() {
• char name[50];
• printf("Enter your full name: ");
• gets(name);
• Cout<<“ you entered ”<< name<<endl;
• }
puts()
• Displays a string and automatically adds a newline.
Only used for strings.
•
• Example Program:
• #include <stdio.h>
• int main() {
• char name[] = "Hina";
• puts(name);
• return 0;
• }