0% found this document useful (0 votes)
24 views

Input & Output Formatting

Uploaded by

niyab10039
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)
24 views

Input & Output Formatting

Uploaded by

niyab10039
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
You are on page 1/ 5

Week2_Input & Ouput Formatting:

Output (printf) Formatting:


% Flag Minimum Precision Size Code
Width
Code:
Code is the conversion code used to describe the datatype i.e., for example for int it is d, for char c,
float f and so on.

Size Modifier:
Used to modify the type specified by the conversion code. Example:
short, long etc.

Width Modifier:
Used to specify minimum number of positions in the output. (If the data to be displayed requires more
space, then printf overrides the width.) ie., In case , you give 2 as minimum width (%2d)
(i)you need to print 450, printf will not eliminate any digit, 450 is printed.
(ii)you need to print 5, printf will display 05 or (space)5 is displayed.

If a floating point number is being printed, then we may specify the number of decimal places to be
printed with precision modifier. The precision modifier has the format (dot) .m where m is the
number of digits after decimal point.

When both width & precision are used, the width must be large enough to contain the integral value
of the number, the decimal point and the number of digits in the decimal position.

Example: %7.2f maximum value stored in 9999.99


If 99999.9999 has to be stored for %7.2f, then 99999.99 only will be displayed.

Flag modifier:
Used for (i) justification (ii) padding and (iii)sign

(i)Justification controls the placement of a value when it is shorter than the specified width.
Justification can be left/right.
The default is right justification.
To left justify a value, the flag is set to minus (-).

(ii)Padding defines the character that fills the unused space when the value is smaller than the print
width. It can be space (default) or zero. If there is no flag defined for padding, the unused width is
filled with spaces.
If the flag is 0, the unused width is filled with zeros.
The zero flag is ignored if it is used left justification because adding zeros after a number changes its
values.
(iii) Sign Flag defines the use or absence of a sign in a numeric value , there are 3 formats.
(a)default formatting: inserts a sign only when the value is –ve . Positive values are formatted without
a sign.
(b) Flag set to plus: signs are printed for both +ve and -ve values
(c ) Flag is space : Then +ve numbers are printed with a leading space and –ve numbers with a minus
effort.

#include<stdio.h>
void main() int
a,b; float c,d;
a=15; b=a/2;
printf(“%d\n”,b); --->7
printf(“%3d\n”,b); ---> _ _ 7 ( here in this statement _
means space) printf(“%03d\n”, b); ---> 007
printf(“%-03d\n”, b); ---> 7 (padding is overruled because left justification)

c=15.3; d=c/3;
printf(“%4.2f\n”,d); ---> 5.10
}

Sl. No. Statement Output


1 printf(“%d”, 12345); 12345

2 printf(“%04d”,25); 0025

3 printf(“%i”,1234); 1234

4 printf(“%4.2f”,3.14159); 3.14

5 printf(“%x”, 255); FF
(Hexadecimal)
6 printf(“%o\n”, 255); 377 (Octal)

7 printf(“%08.2f”, -1.2); -0001.20

8 printf(“%0-8.2f”, -1.2); -1.20

9 printf(“%-7.2f\n”, 23.15); 23.15

10 printf(“% -+7.2f\n”, 23.15); +23.15

11 printf(“% +07.2f\n”, 23.15); +023.15

12 printf(“% 0+7.2f\n”, 23.15); +023.15


13 printf(“%-7.2f\n”, -23.15); -23.15

14 printf(“% -+7.2f\n”, -23.15); -23.15

15 printf(“% +07.2f\n”, -23.15); -023.15

16 printf(“% 0+7.2f\n”, -23.15); -023.15

17 printf(“%d”, printf(“cpgm”)); cpgm4

18 printf(“%d”, printf(“amrita”)); amrita6

19 printf(“%d”, (scanf(“%d%d”,&a,&b))); 2

20 printf(“%d”, (scanf(“%d%d%d”,&a,&b,&c))); 3

Input (scanf) Formatting:


% Flag Maximum Size Code
Width
3 Differences between the conversion codes for input and output formatting
1. No precision is specified in input format specification. It is an error to include a precision , if
scanf finds a precision, it stops processing and the input stream is in the error state.

2. There is only 1 flag for input formatting, the assignment suppression (*). (*) tells scanf that the
next input field is to be read but not stored. It is discarded.
Example: scanf(“ %d %*c %f ”, &x, &y);

3. Width Specification:
• Width specification with input formatting is maximum and not minimum(as in printf)
• The width modifier specifies the maximum number of characters that are to be read
for one format code (%d or %c or %f, etc)
• When a width specification is included ,scanf reads until the maximum number of
characters have been processed or until scanf finds a white space character.
• If scanf finds a white space character before max is reached , it stops.
#include<stdio.h>
void main()
{ float a;
float a;
printf(“Enter a”);
scanf(“%f”,&a); --->2.1
printf(“a=%f\n”,a); ---
>2.100000
}
After scanning, print the variable values , make observations and note them down.

Sl. No. Statement/s


1 scanf(“%d%d%c”,&a,&b,&c); Try giving the following inputs
(i) 12 15c
(ii) 12 15 c
And print the values of a,b and
c

2 scanf(“%d%d %c”,&a,&b,&c); Try giving the following input


12 15 c

3 float a=2.1; scanf(“%5.2f”,&a); ERROR: precision should not


be specified

4 int a=1,b=2,c=3; scanf(“%d%*d%d”,&a,


&b, &c); printf(“a=%d\t b=%d\t”, &a, 10 20 30 (input)
&b); 10 30 (output may vary , but
note 20 is not stored)

5 scanf(“%2d%3d”, &a, &b); printf(“a=%d\tb= 12345 a=12


%d”, a,b); b=345

6 scanf(“%3d%4d”, &a, &b); printf(“a=%d\tb= 12345 a=123


%d”, a,b); b=45

7 scanf(“%3d%4d”, &a, &b); printf(“a=%d\tb= 1234 567


%d”, a,b); a=123 b=4

8 scanf(“%3d%4d”, &a, &b); printf(“a=%d\tb= 123456789


%d”, a,b); a=123 b=4567
Program for getchar() and putchar()

#include <stdio.h>
#include <stdlib.h>
main( )
{
char ch;
printf("Enter a character: ");
ch=getchar();
printf("You have entered a character: ");
putchar(ch);
return 0;
}

more questions can be added on this topic.

Netxt seesion operators and control flow strustures.

You might also like