Formatted Input and Output     Chapter 4


Objectives of this chapter:

Printf Function

#include <stdio.h> 

main() 

{ 

int number=1; 

printf("My favourite number is %d.\n",number); 

printf("Because it is first.\n"); 

}

Output: 

My favourite nunber is 1. 

because it is first.

the control string of the printf function is a character string describing how the data or items are to be printed. 

the control string may contain conversion specifiers for each data or item to be printed. 

for example, in the following statement:

printf("My favourite number is %d.\n",number);

the control string is the phrase in double qoutation marks, and it contains a conversion specifier that corresponds to a number.

conversion specification

printf("My favourite number is %d.\n",number);

literal characters

the control string contains two definite forms of information:

the literal characters are the characters actually printed. It may also contain escape sequence which represent the newlinte character which causes output to continue on the next line.

conversion specifications starts with the % character.

for example, %d is used to print out the contents of a variable in decimal form..
Conversion Specification Type  Output
%c single character
%d signed decimal integer
%e floating-point number, e-notation 
%E floating-point number, E-notation. 
%f floating-point number, decimal notation. 
%g, %G use %f or %e, whichever is shorter. 
%i signed decimal integer
%o  unsigned octal integer. 
%p pointer
%s character string
%u unsigned decimal integer 
%x unsigned hexadecimal integer, using hex digits a,b,c,d,e,f. 
%X unsigned hexadecimal integer, using hex digits A,B,C,D,E,F. 
the conversion specification can be modified by inserting modifiers between the % and the defining conversion character.
Modifier Meaning 
flag There are five flags that can be used: 
- the item is printed left-justified 
+ signed values are displayed with a plus if positive and with a minus sign if negative. 
space  a blank space will precede each positive signed numerical data item. This flag is overridden by the + flag if both are present. 
# causes data items printed in octal form to be preseced by a 0 and data items printed in hexadecimal form to be preceded by a 0x or 0X.

causes a decimal point to be printed in all floating-point numbers even if the data item is a whole number. Also prevents the truncation of trailing zeros in g-type conversion. 
%g, %G 0 For numeric data item, pad the output field with loading zeroes instead of blank spaces. This flag is overridden by the - flag if both are present or if, for an integer form, a precision is specified. 
width The minimum output field width. A wider output field will be used if the printed data item won't fit into the specified field width.

 A * means to take the next argument as the output field width. 
.precision The precision of the output.

For e, E, and f conversions, it is the number digitis to be printed to the right of the decimal point.

For g and G conversions, it is the maximum number of significant digits.

For s conversions, it is the maximum numner of characters to be printed.

For integer conversions, it is the minimum number of digits to appear and leading zeroues are used if necessary to meet this minimum.

 A .* means to take the next argument as the precision size.

Using the . (period) alone is the same as using .0 
h Used with an integer conversion to indicate a short int or unsigned short int value. 

eg: %hu, %hx, 6.4hd 
l Used with an integer conversion to indicate a long int or unsigned long int.

 eg. %ld, %8lu 
L Used with floating point conversion to indicate a long double value. 

eg. %Lf, %10.4Le 

Sample programs

#include <stdio.h> 

main() 

{ 

double a = 5000.0, b=0.0025;

printf("%f %f %f %f \n\n",a,b,a*b,a/b); 

printf("%e %e %e %e \n\n",a,b,a*b,a/b); 

}

Output:

5000.000000 0.002500 12.500000 2000000.000000

5.00000e+03 2.50000e-03 1.25000e+01 2.00000e+06 


#include <stdio.h> 

main() 

{ 

int i = 12345; 

float x = 123.456;

printf("%3d %5d %8d\n\n",i,i,i); 

printf("%3f %10f %13f\n\n",x,x,x); 

printf("%3e %13e %16e\n\n",x,x,x); 

}

Output:

123451234512345 

123.456001123.456001123.456001 

1.23456e+021.23456e+021.23456e+02



#include <stdio.h>

main() 

{ 

float x = 123.456; 

printf("%7f %7.3f %7.1f\n\n",x,x,x); 

printf("%12e %12.5e %12.3e\n\n",x,x,x);

printf("%f %.3f %7.1f\n\n",x,x,x); 

printf("%e %.5e %.3e\n",x,x,x);

}

Output: 

123.456001123.456123.5

1.23456e+021.2346e+021.23e+02

123.456001123.456123.5

1.23456e+021.2346e+021.23e+02




#include <stdio.h>

main() 

{ 

int a = 0x80ec; 

float b = 0.3e-12; 

printf("%4x %10.2e\n\n",a,b); 

printf("%4X %10.2E\n\n",a,b); 

}

Output: 

80ec3.0e-13

80EC3.0E-13 


#include <stdio.h> 

main() 

{ 

int i = 123; 

float x =12.0, y=-3.3;

printf("%6d %7.0f %10.1e \n\n",i,x,y); 

printf("%-6d %-7.0f %-10.1e \n\n",i,x,y); 

printf("%+6d %+7.0f %+10.1e \n\n",i,x,y); 

printf("%-+6d %-+7.0f %-+10.1e \n\n",i,x,y); 

printf("%7.0f %#7.0f %7g %#7g\n\n",x,x,y,y); 

} 

12312-3.3e+00 


12312 -3e+00

+123+12-3e+00

+123+12-3e+00

1212.-3.3-3.30000 


#include <stdio.h> 

main() 

{ 

int i = 1234, x =01777, k=0xab77;

clrscr(); 

printf("%8u %8o %8x\n\n",i,j,k); 

printf("%-8u %-8o %-8x\n\n",i,j,k); 

printf("%#8u %#8o %#8X\n\n",i,j,k); 

printf("%08u %08o %08X\n\n",i,j,k); 

} 


12341777ab77

12341777ab77

1234017770xAB77

00001234000017770000AB77 

Formatted Input - The scanf function


















scanf (control string, arg1, arg2, …, argn);





Example:

#incude <stdio.h> 

main() 

{ 

int age; 

float assets; 

printf ("Enter your age and assets : "); 

scanf("%d %d", &age, &assets); 

}

the scanf function uses conversion specifiers in the control string to convert the input to the desired formats before storing them into the variables. 



the variable names in the argument list is preceded with the & operator, which gives the memory address location of the variable. 
Example :
#include <stdio.h>
main()
{
   int value, count;
   printf("Enter the value and count :");
   scanf("%d%d",&value, &count);
}



Conversion Specification Type  Interpret input as
%c a single character
%d. %i signed decimal integer 
%e, %E, %f floating-point number 
%o  unsigned octal integer. 
%p pointer
%s character string
%u unsigned decimal integer 
%x, %X unsigned hexadecimal integer, using hex digits a,b,c,d,e,f. 
%[..]s Character enclosed within the brackets indicate that a string is to be read. The characters within the brackets indicate the permissible characters in the string. If any character other than that specified in the brackets is encountered, then the string will be terminated. 

By placing a ^ as the first character inside the brackets, then if any of the characters is found in the input, then the string will be terminated. 




scanf Conversion Modifiers

Modifier Meaning 
* Suppress assignment. 

eg:

 %*d 
integer-string The maximum field width, Input stops when the maximum field width is reached or when the first whitespace character is encountered whichever comes first. 

eg: %10s 
h,l, or L %hd and %hi indicate the value will be stored in a short int. 

%ho, %hx, and %hu indicate the value will be stored in an unsigned short int.

%ld and %li indicate the value will be stored in a long int.

%lo, %lx, %lu indicate the value will be stored in unsigned long.

%le, %lf, and %lg indicate the value will be stored in a double. 

Using L instead of l with e, f and g indicates the value will be stored in type long double.

In the absence of these modifiers, d, i, e, o, and x indicate type int; 

e,f, and g indicate type float
the conversion specification can be altered by placing the modifiers between the percent sign and the conversion letter.

the general format for the scanf conversion specification

is:

%[*][width][h][l][L] type

Optional fields are enclosed in the brackets, must appear in the order as shown above.

Examples on using the scanf function:
#include <stdio.h>

main()
{
        int items;
        float prices;



        printf ("Enter the number of items ");
        printf("and price below: \n");

        scanf("%d %f", &item, &price);
        
        printf("Number of items = %d", items);
        printf(" and the price = RM%.2f\n", price);

}


Program output:

Enter the number of items and price below.

550 2.5 

Number of items = 550 and the price - RM2.50

#include <stdio.h>

main()

{

unsigned int width, precision;

int number = 256;

double weight = 242.5;

printf("What field width ?"\n);

scanf("%d",&width);

printf("The number is :%*d:\n",width,number);

printf("Now enter a wodth and a precision:\n");

scanf("%d %d", &width, &precision);

printf("Weight = %*.*f\n",width, precision,weight);

}

Output:

What field width?

6 

The number is 256:

Now enter a width and precision:

8 3 

Weight = 242.500 
#include <stdio.h>
main()
{
        int n;
        printf("Please enter three integers:\n");
        scanf("%*d %*d %d", &n);
        printf("The last integer was %d\n",n);
}

Output: 

Please enter three integers: 

445 345 1212 

The last integer was 1212

#include <stdio.h>

main() 

{ 

int hour, minutes, seconds, percentage; 

int i1, i2; 

char c1, c2;

printf("Enter the time (hh:mm:ss): "); 

scanf("%d:%d:%d",&hour, &minutes, &seconds); 

printf("Enter your average (%): "); 

scanf("%d%%",&percentage);

printf("Enter an integer and character :"); 

scanf("%d%c",&i1,c1);

printf("Enter another integer and character :"); 

scanf("%d%c",&i2,c2);

printf("The time is %02d hours ", hour); 

printf("%02d minutes and %02d seconds\n ",

minutes, seconds); 

printf("Your percentage is %d%%\n", percentage); 

printf("i1 = %d \t c1 = %c, i1, c1); 

printf("i1 = %d \t c1 = %c, i2, c2); 

} 

Output:

Enter the time (hh:mm:ss): 20:50:9 

Enter your average (%): 78% 

Enter an integer and character : 120w 

Enter another integer and character : 240 v 

The time is 20 hours 50minutes and 09 seconds 

Your percentage is 78% 

i1 = 120 c1 = w 

i2 = 240 c2 = v

Character input/output - getchar and putchar functions

getchar and putchar are defined in the stdio.h file. 

the getchar function takes no arguments, and it returns the character read from the input.

for example: 

.. 

char ch; 

.. 

ch = get char(); 

.. ... 

putchar(ch); 

... 1