Input and Output statements in C
Input and Output statements in C
Input and Output statements in C
A stream is the source of data as well as the destination of data. Streams are associated with a physical
device such as a monitor or a file stored on the secondary memory. C uses two forms of streams—text and
binary, as shown below,
In a text stream, sequence of characters is divided into lines with each line being
terminated with a newline character (\n).
A binary stream contains data values using their memory representation.
We can take input/output from the keyboard/monitor or from any file but assume
that the source of data is the keyboard and destination of the data is the monitor.
FORMATTING INPUT/OUTPUT
type specifier
Each control string must begin with a % sign. The % character specifies how the next
variable in the list of variables has to be printed. After % sign follows:
Flags is an optional argument which specifies output justification such as
numerical sign, trailing zeros or octal, decimal, or hexadecimal prefixes.
Width is an optional argument which specifies the minimum number of
positions in the output.
Precision is an optional argument which specifies the maximum number of
characters to print.
Format Specifiers:
Format specifiers are the character string with % sign followed with a character. It
specifies the type of data that is being processed. It is also called conversion specifier.
When data is being output or input it must be specified with identifier (variable) and
their format specifier.
%c Character
%d Signed integer
%f Float values
%g or %G Similar as %e or %E
%i Unsigned integer
%lf Double
%o Octal representation
%p Pointer
%s String
%u Unsigned int
%x or %X Hexadecimal representation
#include <stdio.h>
void main()
{
//printing character data
char ch = 'B';
printf("%c", ch);
//print decimal or integer data with d and i
int x = 45, y = 90;
printf("%d", x);
printf("%i", y);
//print float value
float f = 12.67;
printf("%f", f);
//print in scientific notation
printf("%e", f);
//print in octal format
int a = 67;
printf("%o", a);
//print in hexdecimal format
printf("%x", a);
//to print string
char str[] = "Hello World";
printf("%s", str);
//shift to the right 20 characters including the string i.e right align (default)
printf("%20s", str);
//left align
printf("%-20s", str);
//shift to the right 20 characters including the string, and print string up to 5 character
printf("%20.5s", str);
//left align and print string up to 5 character
printf("%-20.5s", str);
}
Output:
B
45
90
12.670000
1.267000e+001
103
43
Hello World
Hello World
Hello World
Hello
Hello
scanf()
The scanf() function stands for scan formatting and is used to read formatted data
from the keyboard.
The scanf function takes a text stream from the keyboard, extracts and formats data
from the stream according to a format control string and then stores the data in
specified program variables.
The control string specifies the type and format of the data that has to be obtained
from the keyboard and stored in the memory locations pointed by arguments arg1,
arg2,…, argn, i.e., the arguments are actually the variable addresses where each piece
of data is to be stored.
%[*][width][modifier]type
Here * is an optional argument that suppresses assignment of the input field, i.e., it
indicates that data should be read from the stream but ignored (not stored in the
memory location).
Modifier is an optional argument that can be h, l, or L for the data pointed by the
corresponding additional arguments. Modifier h is used for short int or unsigned
short int, l is used for long int, unsigned long int, or double values. Finally, L is used
for long double data values. 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 scanf function ignores any blank spaces, tabs, and newlines entered by the user.
The function simply returns the number of input fields successfully scanned and
stored. the scanf function is used to store values in memory locations associated with
variables. For this, the function should have the address of the variables. The address
of the variable is denoted by an ‘&’ sign followed by the name of the variable.