0% found this document useful (0 votes)
7 views12 pages

CP Module 2 Chapter 1 Notes

Console I/O in C involves reading input from the keyboard and displaying output on the screen using functions like getchar() and putchar(). For string handling, gets() and puts() are used, although gets() is unsafe and removed in C11. Formatted input and output are managed by printf() and scanf() functions, allowing for controlled data reading and writing.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

CP Module 2 Chapter 1 Notes

Console I/O in C involves reading input from the keyboard and displaying output on the screen using functions like getchar() and putchar(). For string handling, gets() and puts() are used, although gets() is unsafe and removed in C11. Formatted input and output are managed by printf() and scanf() functions, allowing for controlled data reading and writing.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Console Input/Output

What Are Console I/O in C?


Console I/O (Input/Output) refers to the process of reading input from the
keyboard and displaying output on the screen in a C program.
Console Input: means reading data from the keyboard during program
execution.
Console Output: means displaying information to the user on the screen.

READING AND WRITING CHARACTERS:


The simplest functions for character-level I/O are:
 getchar() — Reads a character from the keyboard.
 putchar() — Writes a character to the screen.

The prototypes for getchar( ) and putchar( ) are shown


here(syntax):

getchar():
• Waits for the user to press a key.
• Returns the ASCII value of the character.
• Also echoes the character to the screen (in most compilers).
• Returns EOF (End of File, usually -1) on error.

putchar():
• print/display a single character to the screen.
• Accepts an int (character is printed from its low-order byte).
• Returns the character printed or EOF on error.
Example :

Output:

Problem with getchar()


• Buffered Input: getchar() is usually line-buffered — input is
stored until the user presses ENTER.
• This behavior may be inconvenient in interactive programs,
especially when multiple characters are stored in the input
buffer.

Alternatives to getchar()
• Since getchar() might not behave interactively in all
environments, many compilers provide non-standard but
practical alternatives:
• Common functions include:
int getch(void); // Reads a character, does NOT print it
int getche(void); // Reads a character, print it.
Header:
• Is found in <conio.h>
Behavior:
• getch(): Waits for a keypress, returns immediately without
echo.
• getche(): Same as getch() but echoes the character.
Example:
#include <stdio.h>
int main()
{
printf("Hello World! ");
getch();
return 0;
} O/p: Hello World!

READING AND WRITING STRINGS

Reading and writing strings (arrays of characters) is an important part of


console I/O in C.
For basic operations, C provides the following functions:
• gets() – Reads a full line of input from the keyboard.
• puts() – Outputs a string to the screen with a newline

gets() function :
prototype or syntax:
char *gets(char *str);

Description/Working:
• Reads a line of text from the keyboard .
• Reading stops when ENTER (carriage return) is pressed.
• The carriage return is not stored in the string; instead, a null terminator
(\0) is added at
the end.
• Returns the same pointer (str) if successful, or NULL on failure.
Note: gets() does not check for buffer [Link] is
unsafe and removed in C11 standard
due to security issues.

puts() function:

Syntax or prototype:

int puts(const char


*str);

Description/Working:
• used to display the string on the screen followed by new line(\n).
• Does not handle formatting or variables.
Returns a non-negative value on success, or EOF on error .
Example:
Puts(“hello”);
Formatted Input/Output:

 The functions printf( ) and scanf( ) perform formatted output and


input— that is, they can read and write data in various formats that
are under your control.
 The printf( ) function writes data to the console.
 The scanf( ) function, its complement, reads data from the
keyboard.
 Both functions can operate on any of the built-in data types, plus
null-terminated character strings.

Formatted Input:
 scanf() function :The function used to get input from the user
during execution of program and stored in a variable of specified
form is called scanf() function.

Syntax:
Syntax: scanf(“format string”,& variable name);
format string -format specifier is written
& - address operator
Formatted Output:

You might also like