Professional Documents
Culture Documents
2 HOURS
I/O OPERATION 2
Console related I/O library functions are listed under <conio.h> header file.
Formatted I/O
FORMATTED INPUT 5
printf() and scanf() allow conversion between ASCII representations and internal data types.
Formatted string contains text to be read/written, and formatting characters that describe how data is to be
read/written.
Syntax:
scanf (“datatype_format”, &variable_name);
The function scan() reads and converts characters from the standards input depending on the format specification string
and stores the input in memory location of the variable
For Example:
scanf(“%d”,&Name);
6
FORMAT SPECIFIERS
The format specification string, along with the data to be output, are the parameters to the printf()
function.
Syntax:
printf(“text + datatype_format”, variable_name);
For Example:
printf(“my roll number is %d”,roll);
Datatype formats 9
E.g.
1. scanf(“%d %d %f %c”, &sn, &roll, &percent, &gpa);
2. Printf(“My roll is %d and my grade is %c”, roll_no, grade);
11
UNFORMATTED INPUT 12
These are used to read a single input from user at console
Unlike formatted input, it doesn’t require the specification of format or conversion characters.
It can directly transfer the value to memory
E.g. are
1. Gets() Take a string as input
2. Getchar take a character as input
3. Getche() take a character as input (it also echoes or displays the entered char]
UNFORMATTED OUTPUT 13
These are used to display output to user at console
Unlike formatted output, it doesn’t require the specification of format or conversion characters.
It can directly transfer the value from memory
E.g. are
1. puts() Display a string (collection of characters and numbers)
2. putch() display a single character
3. putchar() display a single character
Getch() 14
#include <stdio.h>
#include <conio.h>
int main()
{
printf(" Passed value by the User is: %c", getch()); // print a character entered by the user
return 0;
}
16
17
Putch() 18
putch(character);
OR
putch(character_variable);
Example 19
#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
clrscr();
printf(“press any character: ");
scanf("%c", &ch);
printf("\n Thank you for entering");
printf("\n Pressed character is:");
putch(ch); //print the value of "ch" variable in screen
getch(); /* Holding output */
return 1;
}
Getche() 20
It reads a single character from the keyboard and displays immediately on output
screen without waiting for enter key.
21
#include <stdio.h>
#include <conio.h>
// Example for getche() in C
int main()
{
printf("%c", getche());
return 0;
}
22
#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
printf("Press any character: ");
ch = getche();
printf("\n Pressed character is: %c", ch);
return 0;
}
getchar() and putchar() 23
The getchar() function reads a character from a standard input device.
Syntax is: character_variable = getchar();
The getchar() function makes wait until a key is pressed and then assigns this
character to character_variable.
The putchar() function displays a character to the standard output device.
Syntax is: putchar(character_variable);
24
example:
int main()
{
char ch1;
printf(“enter a character=”);
ch1= getchar();
printf(“\nthe entered character is:”);
putchar(ch1);
getch();
return 0;
}
25
Output:
Enter a character= A
The entered character is: A
gets() and puts()
26
gets():
The gets() function is used to read a string of text, containing whitespaces,
until a newline character is encountered.
It can be used as alternative function for scanf() function for reading strings.
Unlike scanf() function, it does not skip whitespaces (i.e. It can be used to
read multi-words string).
Syntax: gets(string_variable);
puts():
The puts() function is used to display the string on the screen.
Syntax: puts(string_variable);
Examplee 27
28
Output:
Enter your name: RISHI RAI
Your name is: RISHI RAI
Important Questions 29