04_Basic Input-Output
04_Basic Input-Output
Control string
Variable list
● As shown in the above syntax the printf statement consists of two parts are,
Function Name and the Function Arguments, enclosed in parentheses and are
separated with comma.
● The print formatting function named with printf in C library and is placed in
standard input out header file i.e. stdio.h
● The function arguments are further divided in to two parts are,
i. Control String:-‐
● The parameter control string in the printf function is nothing but a C string
that contains the text that has to be written on standard output device.
● Control string is further grouped with number of optional parts are,
“[escape sequences] [text string] [reference specifiers]”
Escape Sequences:-‐
Escape sequences are actually non printing control characters that being with
backslash (\). There are various characters in escape sequence set that supported by C
language are shown in below table.
Escape Escape
Purpose of use Purpose of use
Sequence Sequence
\a Audible Signal \? Question mark
\b Backspace \\ Back slash
\t Tab \’ Single quote
\n Newline \” Double Quote
\v Vertical tab \o Octal constant
\f New Page or Clear screen \x Hexadecimal constant
\r Carriage return
Text String:-‐
Control string may also contain text to be printed like instructions to the user,
captions, identifiers, or any other text to make the output readable. In some printf
statement you may find only a text string that has to be displayed on screen.
Reference Specifiers:-‐
Reference specifiers are also known as Placeholders or Conversion Character or
Format Code or Format specifier. Are used at the time of display the value of the
specified variable is substituted at the place of placeholder. The printf function uses
different placeholders to display different type of data items therefore placeholders
have specific format with specific parts are,
%[flags] [width] [.precision] [length]Specifier
Each placeholder string must being with a % sign. The % character specifies how
the next variable in the list of variables has to be printed. After % sign follows.
Flags specify output justification such as decimal point, numerical sign, training
zeros or octal, or hexadecimal prefixes.
Flags Description
- Left-‐justify within the data given field width
+ Displays the data with its numeric sign either + or -‐
Used to provide additional specifiers like o, x, X, 0, 0x, or 0X for octal
#
and hexadecimal values, respectively, for values different than zero.
0 The number is left-‐padded with zeros (0) instead of spaces.
Width specifies the minimum number of characters to print after being padded
with zeros or blank spaces i.e. it specifies the minimum number of positions in the
output. If data needs more space than specified, then printf overrides the width
specified by the user. Width is a very important field especially when you have to align
output in columns.
Length Description
h When the argument is a short int or unsigned short int.
l When the argument is along int or unsigned long int for integer specifiers.
L When the argument is a long double(used for floating point specifiers)
Specifier is used to define the type and the interpretation of the value of the
corresponding documents.
Type Qualifying input
c For single character.
d For decimal value.
f For floating point numbers.
E, e Floating point numbers in exponential format.
G, g Floating point numbers in the shorter of e format.
i For single decimal number.
o For octal number.
s For a sequence of (string of) characters.
u For unsigned decimal value.
X, x For hexadecimal value.
ii. Variable List:-‐
● Variable list is nothing bit the list of all variable names or identifiers in same sequence
as place specifiers in the control string.
Following are the some of the examples of the printf function.
char ch = ‘A’
printf(“\n %c \n%3c \n%5c”, ch, ch, ch);
A
A
A
i. Control String:-‐
● The parameter control string specify the type and format of the data that has to
be obtained from the arguments Name, RollNo and Percentage i.e. the
arguments are actually the variable addresses where each piece of data are to
be stored.
● The prototype of the Control string can be given as,
“%[*][width][modifiers]type”
Here * is an optional argument that suppresses assignment of the input field, i.e. it
indicates data should be read from the stream but ignored (not stored in memory
location)
Width is an optional argument that specifies the maximum number of characters to be
read. However, fewer characters will be read if the scanf function encounters a white
space or an inconvertible character because the moment scanf encounters a white
space character it will stop processing further.
Modifiers are an optional argument that can be h, l, or L for the data pointer by the
corresponding additional arguments.
Modifier
Description
s
h When the argument is a short int or unsigned short int.
l When the argument is along int or unsigned long int for integer specifiers.
L When the argument is a long double(used for floating point specifiers)
Type specifies the type of data that has to be read. It also indicates how this data is
expected to be read from the user. The type specifiers for scanf function are given in
below table.
Rule 3: There must be a variable address for each conversion specification. Therefore,
the following scanf statement will generate an error as no variable address is given
for the third conversion specification.
scanf(“%d %d %d”, &num1, &num2,);
Remember that the ampersand operator (&) before each variable name specifies the
address of that variable name.
Rule 4: A fatal error would be generated if the format string is ended with a white
space character.
Rule 5: The data entered by the user must match the character specified in the control
string, otherwise an error will be generated and scanf will stop its processing. For
example, consider the scanf statement given below.
scanf(“%d / %d”, &num1, &num2,);
Here, the slashes in the control string are neither white space characters not a part of
conversion specification, so the user must enter data of the form 21/46.
Rule 6: Input data values must be separated by spaces.
Rule 7: Any unread data value will be considered as a part of the data input in the next
call to scanf.
Rule 8: When the field width specifier is used, it should be large enough to contain
the input data size. Look at the code given below that show how we input values in
variable of different data types.
int num;
scanf(“ %d ”, &num);
The scanf function reads an integer value into the address or the memory
location pointed by num.
float salary;
scanf(“ %f ”, &salary);
The scanf function reads a floating point number into the address or the
memory location pointed by salary.
char ch;
scanf(“ %c ”, &ch);
The scanf function reads a single character into the address or the
memory location pointed by salary.
char str[10];
scanf(“ %s ”, str);
The scanf function reads a string or a sequence of characters into the
address or the memory location pointed by str. Note that in case of
string, we do not use the & sign in the scanf function.
Look at the code given below which combines reading of variable of different data types in one
single statement.
int num;
float salary;
char ch;
char str[10];
scanf(“ %d %f %c %s ”, &num, &salary, &ch, str);
Following are the some of the examples of the scanf / printf function.
int num;
scanf(“ %d ”, &num);
printf(“\n Number: %d”, num);
32455
Number: 32455
float salary;
scanf(“ %f ”, &salary);
printf(“\n Salary: %.2f”, salary);
12345.500
Salary: 12345.50
char ch;
scanf(“ %c ”, &ch);
printf(“\n Character: %c”, ch);
A
Character: A
char str[10];
scanf(“ %s ”, str);
printf(“\n String: %s”, str);
ABCDef
String: ABCDef
1. Program 64: Find out the output of the program. Output Window
1 #include <stdio.h>
2 #include<conio.h> Enter two four Digit
3 int main() Numbers: 1234 5678
4 {
5 int a, b;
The two four Digit Numbers
6 printf(“\nEnter two four Digit Numbers:”);
are: 12 and 34
7 scanf(“%2d %4d”, &a, &b);
8 printf(“\nThe two four Digit Numbers are: %d
and %d”, a, b);
9 getch();
10 return 0;
11 }
2. Program 65: Program to calculate the area of a triangle using Hero’s Formula. Output Window
1 #include <stdio.h>
2 #include<conio.h>
3 #include<math.h> Enter the length of three
4 int main() sides of the triangle:33
5 { 40 50
6 float a, b, c, area, s;
The Area of Triangle:=
7 printf(“\nEnter the length of three sides of the
658.306519
triangle:”);
8 scanf(“%f %f %f”, &a, &b, &c);
9 s = (a + b + c) / 2;
10 area = sqrt(s * (s – a) * (s – b) * (s – c));
11 printf(“\nThe Area of Triangle:= %f”, area);
12 getch();
13 return 0;
14 }