You are on page 1of 19

Input / Output in C

SCP 1103
Programming C
Input/Output in C
C has no built-in statements for input or output.

A library of functions is supplied to perform these


operations. The I/O library functions are listed the
“header” file <stdio.h>.

You do not need to memorize them, just be familiar


with them.
Streams
All input and output is performed with streams.

A "stream" is a sequence of characters organized into


lines.

Each line consists of zero or more characters and ends


with the "newline" character.

ANSI C standards specify that the system must support


lines that are at least 254 characters in length (including
the newline character).
Types of Streams in C
Standard input stream is called "stdin" and is normally
connected to the keyboard

Standard output stream is called "stdout" and is


normally connected to the display screen.

Standard error stream is called "stderr" and is also


normally connected to the screen.
Formatted Output with printf
printf ( ) ;
This function provides for formatted output to the
screen.
The syntax is:
printf ( “format”, var1, var2, … ) ;
The “format” includes a listing of the data types of the
variables to be output and, optionally, some text and
control character(s).
Example:
float a ; int b ;
scanf ( “%f%d”, &a, &b ) ;
printf ( “You entered %f and %d \n”, a, b ) ;
Input/Output in C
 Example:

printf(“Thank you”);
printf(“Total sum is: %d\n”, SUM);

%d is a placeholder (format specifier)


marks the display position for a type integer variable
\n is an escape sequence
moves the cursor to the new line
Formatted Output with printf
Format specifier (placeholder)
Tells the printf() function the format of the output to be
printed put.
Format
Output Type
Specifier
%d for integers
%c for characters
%f, %lf for floating-point values in conventional
notation
%e for floating-point values in scientific
notation
%s for sequence of characters (string)
Input/Output in C
Examples

Output statements Prints on the screen


printf(“%d”, 15); 15
printf(“Hello”); Hello
printf(“%s”, “Hello”); Hello
printf(“%c”, ‘a’); a
printf(“%f”, 1.25); 1.250000
printf(“%e”, 1.25); 1.250000e+00
printf(“%s”, name); John D
(assume name[20]=“John
D”)
Input/Output in C
Escape Sequence
is used in the printf() function to do something to the
output.
The common escape Escape
Meaning
sequence Sequence
… and more \n New line
\t Horizontal tab
\v Vertical tab
\b Backspace
\0 NULL
\\ Prints a
backslash (\)
Input/Output in C
scanf ( ) ;
This function provides for formatted input from the
keyboard.
The syntax is:
scanf ( “format” , &var1, &var2, …) ;
The “format” is a listing of the data types of the
variables to be input and the & in front of each
variable name tells the system WHERE to store the
value that is input. It provides the address for the
variable.
Example:
float a; int b;
scanf (“%f%d”, &a, &b);
Input/Output in C
Common format specifier used in printf() and
scanf() functions.

printf scanf
int %d %d
float %f %f
double %f %lf
char %c %c
string %s %s
Input/Output in C
Example

#include <stdio.h>

main()
{
int Age;

printf(“Enter your age: “);


scanf(“%d”, &Age);

printf(“Your age is: %d”,Age);


}

Output?
Input/Output in C
If you want the user to enter more than one value, you
serialise the inputs.
Example:

float height, weight;

printf(Please enter your height and


weight“:”);
scanf(“%f%f”, &height, &weight);

Input/Output in C
getchar ( ) ;

This function provides for getting exactly one


character from the keyboard.
Example:

char ch;
ch = getchar ( ) ;
Input/Output in C
putchar (char) ;

This function provides for printing exactly one


character to the screen.
Example:

char ch;
ch = getchar ( ) ; /* input a character from kbd*/
putchar (ch) ; /* display it on the screen */
Input/Output in C
 getchar() - read a character from standard input
 putchar() - write a character to standard output
 Example:
#include <stdio.h>
main()
{
char my_char;
printf(“Please type a character: “);
my_char = getchar();
printf(“\nYou have typed this character: “);
putchar(my_char);
printf(“\n”);
}
Input/Output in C
getc ( *file ) ;

This function is similar to getchar( ) except the input


can be from the keyboard or a file.
Example:
char ch;
ch = getc (stdin) ; /* input from keyboard */
ch = getc (fileptr) ; /* input from a file */
Input/Output in C
putc ( char, *file ) ;

This function is similar to putchar ( ) except the output


can be to the screen or a file.
Example:
char ch;
ch = getc (stdin) ; /* input from keyboard */
putc (ch, stdout) ; /* output to the screen */
putc (ch, outfileptr) ; /*output to a file */
Today’s Assignment
1. Write a complete C program to prompt a user for
input the quantity of each items, receive the input, do
a calculation and print the input and the answer on
the screen based on table item below.
Item Price / Item
Book RM 12.50
Pencil RM 0.80
Multifunction paper RM 10.50
Then modify the program to add another calculation
and display the new result.

You might also like