1. There are two main ways to handle input-output in C - formatted functions like printf() and scanf() which require format specifiers, and unformatted functions like getchar() and putchar() which work only with characters.
2. Formatted functions allow formatting of different data types like integers, floats, and strings. Unformatted functions only work with characters.
3. Common formatted functions include printf() for output and scanf() for input. printf() outputs data according to format specifiers, while scanf() reads input and stores it in variables based on specifiers.
Introduction to input/output operations in C. Programs read, process, and display data. Data can be assigned directly or via functions classified into formatted and unformatted categories.
Explanation of formatted (e.g., printf, scanf) vs unformatted functions (e.g., getch). Formatted operations require format strings, while unformatted operate specifically on characters.
Detailed syntax and working of printf() for output. Importance of matching format strings with variable types. Demonstrated examples, including errors from mismatched formats.
Breakdown of format specifications, flags, width, and precision impacting output. Examples show variations in output formatting for integers and floats.
Detailed use of scanf() for input, requiring conversion symbols and the address operator (&) for variable memory locations. Format specifications are similar to printf().
Summary table of data types and their respective format strings for printf and scanf, clarifying data input requirements for integers, floats, characters, and strings.
Introduction to escape sequences used in printf() and scanf() such as newline, backspace, and tab, illustrating special character handling.
Description of various unformatted functions including getchar, putchar, getch, getche, gets, and puts to handle character and string input and output.
Introduction
• Reading inputdata, processing it and displaying the
results are the three tasks of any program.
• There are two ways to accept the data.
– In one method, a data value is assigned to the variable with an
assignment statement.
• int year = 2005; char letter = ‘a’; int x = 12345;
– Another way of accepting the data is with functions.
• There are a number of I/O functions in C, based
on the data type. The input/output functions are
classified in two types.
– Formatted functions
– Unformatted functions
3.
Formatted function
• Withthe formatted functions, the input or
output is formatted as per our requirement.
• All the I/O function are defined as stdio.h
header file.
• Header file should be included in the program
at the beginning.
Formatted Functions Unformatted Functions
• It read and write all types • Works only with character
of data values. data type
• Require format string to • Do not require format
produce formatted result conversion for formatting
• Returns value after data type
execution
6.
printf() function
• Thisfunction displays output with specified format
• It requires format conversion symbol or format string
and variables names to the print the data
• The list of variables are specified in the printf()
statement
• The values of the variables are printed as the
sequence mentioned in printf()
• The format string symbol and variable name should
be the same in number and type
7.
printf() function
• Syntax
printf(“control string”, varialbe1, variable2,..., variableN);
• The control string specifies the field format such as
%d, %s, %g, %f and variables as taken by the
programmer
• All theformat specification starts with % and a
format specification letter after this symbol.
• It indicates the type of data and its format.
• If the format string does not match with the
corresponding variable, the result will not be
correct.
• Along with format specification use
– Flags
– Width
– Precision
13.
• Flag
– It is used for output justification, numeric signs, decimal
points, trailing zeros.
– The flag (-) justifies the result. If it is not given the default
result is right justification.
• Width
– It sets the minimum field width for an output value.
– Width can be specified through a decimal point or using an
asterisk ‘*’.
Sr. No Format Meaning Explanation
1 %wd Format for integer w is width in integer and d
output is conversion specification
2 %w.cf Format for float w is width in integer, c
numbers specifies the number of
digits after decimal point
and f specifies the
conversion specification
3 %w.cs Format for string w is width for total
output characters, c are used
displaying leading blanks
and s specifies conversion
specification
19.
scanf() function
• scanf()function reads all the types of data
values.
• It is used for runtime assignment of variables.
• The scanf() statement also requires
conversion symbol to identify the data to be
read during the execution of the program.
• The scanf() stops functioning when some
input entered does not match format string.
20.
scanf() function
Syntax :
scanf(“%d%f %c”, &a, &b, &c);
Scanf statement requires ‘&’ operator called address
operator
The address operator prints the memory location of
the variable
scanf() statement the role of ‘&’ operator is to
indicate the memory location of the variable, so that
the value read would be placed at that location.
21.
scanf() function
Thescanf() function statement also return values.
The return value is exactly equal to the number of
values correctly read.
If the read value is convertible to the given format,
conversion is made.
22.
void main()
{
int a;
clrscr();
printf(“Enter value of ‘A’ : “);
scanf(“%c”, &a);
printf(“A : %c”,a);
}
OUTPUT
Enter value of ‘A’ : 8
A:8
23.
void main()
{
char a;
clrscr();
printf(“Enter value of ‘A’ : “);
scanf(“%d”, &a);
printf(“A : %d”,a);
}
OUTPUT
Enter value of ‘A’ : 255
A : 255
Enter value of ‘A’ : 256
A : 256
24.
Sr. No Format Meaning Explanation
1 %wd Format for integer w is width in integer and d
input is conversion specification
2 %w.cf Format for float w is width in integer, c
point input specifies the number of
digits after decimal point
and f specifies the
conversion specification
3 %w.cs Format for string w is width for total
input characters, c are used
displaying leading blanks
and s specifies conversion
specification
25.
Data Type Format string
Integer Short Integer %d or %i
Short unsigned %u
Long signed %ld
Long unsigned %lu
Unsigned hexadecimal %u
Unsigned octal %o
Real Floating %f or %g
Double Floating %lf
Character Signed Character %c
Unsigned Character %c
String %s
Octal number %o
Displays Hexa decimal %hx
number in lowercase
Displays Hexa decimal %p
number in lowercase
Aborts program with %n
error
26.
Escape Sequence
Escape Sequence Use ASCII value
• printf() and scanf() statement n New Line 10
follows the combination of
characters called escape b Backspace 8
sequence f Form feed 12
• Escape sequence are special ’ Single quote 39
characters starting with ‘’ Backslash 92
0 Null 0
t Horizontal Tab 9
r Carriage Return 13
a Alert 7
” Double Quote 34
v Variable tab 11
? Question mark 63
27.
void main()
{
int a = 1, b = a + 1, c = b + 1, d = c + 1;
clrscr();
printf(“t A = %dnB = %d ’C = %d’”,a,b,c);
printf(“nb***D = %d**”,d);
printf(“n*************”);
printf(“rA = %d B = %d”, a, b);
}
OUTPUT
A=1
B=2 ‘C = 3’
***D=4**
A = 1 B = 2******
28.
Unformatted Functions
• Chas three types of I/O functions
– Character I/O
– String I/O
– File I/O
– Character I/O
29.
getchar
• This functionreads a character type data from
standard input.
• It reads one character at a time till the user presses
the enter key.
• Syntax
VariableName = getchar();
• Example
char c;
c = getchar();
30.
putchar
• This functionprints one character on the
screen at a time, read by the standard input.
• Syntax
– puncher(variableName)
• Example
char c = ‘C’;
putchar(c);
31.
getch() and getche()
•These functions read any alphanumeric character
from the standard input device.
• The character entered is not displayed by the getch()
function.
• The character entered is displayed by the getche()
function.
• Exampe
ch = getch();
ch = getche();
32.
gets()
• This functionis used for accepting any string through stdin
keyword until enter key is pressed.
• The header file stdio.h is needed for implementing the
above function.
• Syntax
char str[length of string in number];
gets(str);
void main()
{
char ch[30];
clrscr();
printf(“Enter the string : “);
gets();
printf(“n Entered string : %s”, ch);
}
33.
puts()
• This functionprints the string or character array.
• It is opposite to gets()
char str[length of string in number];
gets(str);
puts(str);