Formatted Output

Formatting Output using printf() function:

 

 printf() function:

 

The printf() is a library function to send formatted output to the screen. The printf() function is declared in “stdio.h” header file. It can be used to print any number of arguments. The format of the output can be modified using different format specification string or we can say control strings.

If you want to format the characters, then you need to know a little more about the printf function’s format specification string.

 

The syntax of printf function contains format specification string and argument list.

Syntax:
printf("format specification string", arg1, arg2….argn);

 


 

Format specification string contains:

1. Character or string to print as it is on the output screen.

eg: printf(“Hello”); or printf(“Sum= %d”, arg1);

“Hello” and “Sum=” will get print as it is on output screen

2. escape sequence

eg: printf(“\n\t”);

ECS sequence Meaning
\’ single quote
\” double quote
\\ Backslash
\b Backspace
\f form feed
\n new line
\r carriage return
\t horizontal tab

Table 1.1

3. format specifier

eg: printf(“%d”, a);


 

Format Specifier:

 

The format string may consist both of characters and literals, and determines how the arguments arg1 – argn will be formatted.

Each of those arguments requires a format specifier, that begins with the character % and has the following syntax:

%[-][+][0][x[.y]]conv

Here the terms put in brackets are optional. The meaning of these terms is as follows:

Term Meaning
left justify the argument
+ print a number with its sign, + or –
0 pads a number with leading zeros
w minimum field width
.p number of decimal places

Table 1.2

The conv is a conversion character and it depends on the datatype of arguments.

Let’s discuss different conversion characters for different datatypes.

 

Conversion characters for Integers

Integer variable contains

  • Whole number (no decimal point): 25, 0, -9
  • Positive, negative, or zero

 

Examples of printf with different Integer format specifiers

 

printf(“%d\n”, 455 );   

OUTPUT: 455

REASON: %d is for Integer Value

 

printf(“%i\n”, 455 );    

OUTPUT: 455

REASON: %d and %i works same in case of printf

 

printf(“%d\n”, +455 );

OUTPUT: 455

REASON: + sign is a default sign for POSITIVE NUMBER

 

printf(“%d\n”, -455 ); 

OUTPUT: -455

REASON: – sign for NEGATIVE NUMBER

 

printf(“%hd\n”, 32000 );           

OUTPUT: 32000

REASON: h specifies a SHORT number.

 

printf(“%ld\n”, 2000000000 );

OUTPUT: 2000000000

REASON: l is for long integer.

 

printf(“%o\n”, 455 ); 

OUTPUT: 707

REASON: o is for octal representation

[455 converted to octal number]

 

printf(“%u\n”, -455 );

OUTPUT: 65081

REASON: u is for unsigned integer. [0 – 65535]

 

printf(“%u\n”, 455 );   

OUTPUT: 455

REASON: No change if number is positive in case of unsigned value

 

printf(“%x\n”, 455 );   

OUTPUT: 1c7

REASON: x is for hexadecimal representation and character in output is small as “x” is in small case

[455 is converted into hexadecimal number]

 

printf(“%X\n”, 455 );  

OUTPUT: 1C7

REASON: X is for hexadecimal representation and character in output is in uppercase as “X” is in uppercase

[455 is converted into hexadecimal number]

 

Conversion characters for Float
Floating Point Number contains

  • Have a decimal point (33.5)
  • Exponential notation (computer’s version of scientific notation)

150.3 is 1.503 x 10² in scientific

150.3 is 1.503E+02 in exponential

 

Examples of printf with different Floating point format specifiers

printf(“%f\n”, 1234567.89 );   

OUTPUT: 1234567.890000

REASON: %f display floating point value in fixed point notation.

 

printf(“%e\n”, 1234567.89 );   

OUTPUT: 1.234568e+06

REASON: %e display floating point value in exponential notation.

 

printf(“%e”, -1234567.89 );   

OUTPUT: -1234568e+06

REASON: %e display floating point value in exponential notation. Minus(-) sign is considered as separate character in case of negative numbers.

 

printf(“%E”, 1234567.89 );   

OUTPUT: 1.234568E+06

REASON: %E display floating point value in exponential notation.

 

printf(“%g”, 1234567.89 );   

OUTPUT: 1.23457e+06

REASON: %g display either in floating point form or in exponential notation based on the magnitude of the value

 

printf(“%g”, 123.89 );   

OUTPUT: 123.89

REASON: %g display either in floating point form or in exponential notation based on the magnitude of the value

 

printf(“%G”, 1234567.89 );   

OUTPUT: 1.23457e+06

REASON: %G display either in floating point form or in exponential notation based on the magnitude of the value

Conversion characters for Character and String
Character (c)

  • Prints char argument
  • Cannot be used to print the first character of a string

 

String (s)

  • Requires a pointer to char as an argument
  • Prints characters until NULL (‘\0’) encountered
  • Cannot print a char argument

Point to remember:

  • Single quotes for character constants (‘z’). Using single quotes around character strings is a syntax error.
  • Using %c to print a string is an error.
  • Using %s to print a char argument, on some systems, causes a fatal execution-time error called an access violation.
  • A string is a pointer to char (i.e., a char *). The conversion specifier %s expects an argument of type pointer to char.
  • Double quotes for strings “z” (which actually contains two characters, ‘z’ and ‘\0’) Using double quotes around a character constant creates a pointer to a string.

 

Examples for character and string:

%c: character

%s: String

Lets suppose:

char c = ‘C’;

char *string=”Hello”;

char strArray[]= “World”;

 

printf(“%c”, c );   

OUTPUT: C

REASON: %c is use to character variable

 

printf(“%c”, string );   

OUTPUT: garbage value

REASON: %c is for character not for string.

 

printf(“%s”, string );   

OUTPUT: Hello

REASON: %s is for string variable.

 

printf(“%s”, strArray );   

OUTPUT: World

REASON: %s is for string variable.

 

Other Conversion Specifier:

1. %p:

It displays pointer value that means address

 int i, *ptr;

ptr=&i;

printf(“%p”, ptr );   

OUTPUT: FFF4 [ hexadecimal value]

REASON: output may vary system to system because it is an address value.

 2. %n

  • Stores number of characters already output by current printf statement
  • Takes a pointer to an integer as an argument
  • Nothing printed by a %n specification
  • Every printf call returns a value
  • Negative number if error occurs

 Example:

 int i

 printf(“Hello%n”, &i );   

 printf(“%d”, i );   

OUTPUT: 5

REASON: %n return the no. of character printed by its printf statement.

 


 

Minimum Field width: w [refer above Table 1.2]

The field width specifies the minimum width in which the data is to be output,

Syntax: %wconv

Rules:

  • If width larger than data, default right justified and it is padded to the right with blanks.
  • If field width too small, it increases to fit data
  • A minus sign denotes left adjustment of the data
  • If the digit is prefixed with a zero, the padding is done with zeros instead of blanks.
  • Integer width inserted between % and conversion specifier

Example: %4d – field width of 4

 

Examples with different field widths:

printf(“%d”, 9294);

OUTPUT: 9294

 

printf(“%2d”, 9294);

OUTPUT: 9294

REASON: if width is smaller than the number of digit then, it increases to fit data.

 

printf(“%10d”, 9294);

OUTPUT:       9294

REASON: by default, alignment of value is right justified so spaces will get add in front of the value

[right align: 6 spaces and 4 digits]

 

printf(“%-10d”, 9294);

OUTPUT: 9294

REASON: minus (-) sign in format specifier reverse the alignment of values

[Left align: 6 spaces and 4 digits]

 

printf(“%07d”, 9294);

OUTPUT: 0009294

REASON: (0) will get add in place of space, by default alignment is right justified.

 

printf(“%10d”, -9294);

OUTPUT:      -9294

REASON: minus(-) sign of negative value will be consider a character.

[right align:  5 spaces, 4 digits and 1(-) minus sign]

 

printf(“%15s”, string );   

OUTPUT:           Hello

REASON: %s is for string variable. By default, alignment is right justified.

 


 

Field width (w) with precision

Precision:

  • Meaning varies depending on data type
  • Integers (default 1)
    • Minimum number of digits to print
    • If data too small, prefixed with zeros
  • Floating point
    • Number of digits to appear after decimal (e and f)
    • For g – maximum number of significant digits
  • Strings
    • Maximum number of characters to be written from string

 

Format:

Use a dot (.) then precision number after %

%.3f

 

Examples:

        int i =873;

double f= 123.45678;

        char strArray[]= “Hello World”;

 

printf(“Using precision for integer”);

printf(“%.4d \t %.9d”, i,i );

OUTPUT: 0873           000000873

REASON: Precision for integers specifies the minimum number of characters to be printed, prefixed with zeros if data is too small.

 

printf(“Using precision for String”);

printf(“%.9s”, s );

OUTPUT: Hello Wor

REASON: Precision for strings specifies the maximum number of characters to be printed

 

printf(“Using precision for Floating Point”);

printf(“%.4f \t %.3e”, f, f );

OUTPUT: 123.4568               1.235e+02

REASON: Precision for f and e specifiers controls the number of digits after the decimal point

 

printf(“%.5g”, f );

OUTPUT: 123.46

REASON: Precision for the g specifier controls the maximum number of significant digits printed [Round off value]

Powered by themekiller.com anime4online.com animextoon.com apk4phone.com tengag.com moviekillers.com