Programming
Fundamentals
Mufleh Al-Shatnawi, Ph.D., [Link].,
Output Functions
THE OUTPUT FACILITIES OF A PROGRAMMING LANGUAGE
CONVERT THE DATA IN MEMORY INTO A STREAM OF
CHARACTERS THAT IS READ BY THE USER.
Mufleh Al-Shatnawi, Ph.D., [Link]., 2
Buffering Output – 1
Standard output is line buffered. A program outputs its data to a
buffer. That buffer empties to the standard output device
separately. When it empties, we say that the buffer flushes.
screen consider output buffer : take time
Output buffering lets a program continue executing without having
to wait for the output device to finish displaying the characters it
has received. write on buffer and show in screen
Mufleh Al-Shatnawi, Ph.D., [Link]., 3
Buffering Output – 2
The output buffer flushes if
it is full newline is signal of to go back to buffer
it receives a newline (\n) character
the program terminates
Two functions in the stdio module that send characters to the
output buffer are
putchar() - unformatted
printf() - formatted
Mufleh Al-Shatnawi, Ph.D., [Link]., 4
Unformatted Output: putchar()
The putchar() function sends a single character to the
output buffer.
We pass the character as an argument to this function. The function
returns the character sent or EOF if an error occurred.
The prototype for putchar() is int putchar (int);
put
take integer
and retain
same
character
Mufleh Al-Shatnawi, Ph.D., [Link]., 5
Unformatted Output: putchar()
.
EOF argument
Note that putchar() can take EOF as an argument:
putchar(EOF);
Mufleh Al-Shatnawi, Ph.D., [Link]., 6
Formatted Output: printf()
The printf() function sends data to the output buffer under
format control and returns the number of characters sent.
The prototype for the printf() function is
int printf(format, argument, ... );
format is a set of characters enclosed in double quotes that
may contain any combination of plain characters and conversion
specifiers.
The function sends the plain characters as is to the buffer and uses the
conversion specifiers to translate each value passed as an argument in
the function call.
The ellipsis indicates that the number of arguments can vary.
Each conversion specifier corresponds to one argument.
Mufleh Al-Shatnawi, Ph.D., [Link]., 7
Conversion Specifiers
f double signal .
Mufleh Al-Shatnawi, Ph.D., [Link]., 8
Conversion Controls in printf
We refine the output by inserting control characters between the
% symbol and the conversion character.
Flags:
- : Left-justify the output within the given field width; Right justification is
the default (if this flag is not specified).
‘ ’ (space): If no sign is going to be written, a blank space is inserted
before the value.
0 (zero) : Left-pads the number with zeroes (0) instead of spaces when
padding is required.
Mufleh Al-Shatnawi, Ph.D., [Link]., 9
Conversion Controls in printf
We refine the output by inserting control characters between the
% symbol and the conversion character.
Width: Specifies the minimum number of characters to be
printed. If the value to be printed is shorter than this number, the
result is padded with blank spaces or zeros (if the `0` flag is
used). The width is not a maximum limit; if the value is wider
than the width, it will still be printed in full.
print full value - be careful
Mufleh Al-Shatnawi, Ph.D., [Link]., 10
Conversion Controls in printf
We refine the output by inserting control characters between the
% symbol and the conversion character.
decimal point .
Precision: sets the number of digits to be printed after the
decimal point for f conversions and the minimum number of
digits to be printed for an integer (adding leading zeros if
necessary). A value of 0 suppresses the printing of the decimal
point in an f conversion
For ‘s’: Precision specifies the maximum number of characters to be
printed. By default, all characters are printed until the ending null
character is encountered.
Mufleh Al-Shatnawi, Ph.D., [Link]., 11
Conversion Controls in printf
We refine the output by inserting control characters between the
% symbol and the conversion character.
Size: Indicates the size of the type. For example, ‘h’ indicates
that the argument is to be treated as a short int or unsigned
short int (for integer specifiers), ‘l’ indicates a long int or
unsigned long int, and ‘ll’ indicates a long long int or
unsigned long long int.
Mufleh Al-Shatnawi, Ph.D., [Link]., 12
Example Usage of printf Controls – 1
rightjustificaiton
\
precision !!
@@
Mufleh Al-Shatnawi, Ph.D., [Link]., 13
Example Usage
of printf
Controls – 2
Mufleh Al-Shatnawi, Ph.D., [Link]., 14
Example1: What is the output from the following C
program fragment?
Mufleh Al-Shatnawi, Ph.D., [Link]., 15
Example2: What is the output from the following C
program fragment?
Mufleh Al-Shatnawi, Ph.D., [Link]., 16
how many spaces we need
Example3: Fill
in The Blank
Given this Output
Mufleh Al-Shatnawi, Ph.D., [Link]., 17
Example3: Fill
in The Blank
Given this Output
Mufleh Al-Shatnawi, Ph.D., [Link]., 18
Switch to Visual Studio
Reference Material
1. Introduction to C by Seneca College
2. C Programming by Rajiv Chopra
o Available through Seneca Libraries: C Programming
3. Kernighan, & Ritchie, D. M. (1988). The C programming
language (2nd ed.). Prentice Hall.
o Available through Seneca Libraries: The C programming
language
4. Lippman, Lajoie, J., Lajoie, J., & Moo, B. E. (2013). C++ primer
(5th ed.). Addison Wesley.
o Available through Seneca Libraries: C++ primer
Mufleh Al-Shatnawi, Ph.D., [Link]., 20