Lecture 06
Lecture 06
Input / Output in C
Sachintha Pitigala
© 2023
Input/Output in C
● C has no built-in statements for input or output.
3
Types of Streams
● Standard input stream is called "stdin" and is normally
connected to the keyboard
● Example:
float a ; int b ;
printf ( “You entered %f and %d \n”, a, b ) ;
5
Formatted Output with printf
Format Conversion Specifiers:
d -- displays a decimal (base 10) integer
f -- displays a floating point value
lf -- displays a “long float” or double
c -- displays a single character
s -- displays a string of characters
e -- displays a floating point value in exponential notation
u -- displays an unsigned decimal integer.
x or X -- displays an unsigned hexadecimal integer.
p -- display pointer value (address)
6
Printing Literals and Escape Sequences
Printing Literals
● Most characters can be printed
● Certain "problem" characters, such as the quotation
mark "
● Must be represented by escape sequences
● Represented by a backslash \ followed by an escape
character
(please refer lecture 04 for additional information)
7
Input 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);
8
Formatting Input with Scanf
● d -- Read an optionally signed decimal integer. The corresponding argument is a pointer to
integer.
● u -- Read an unsigned decimal integer. The corresponding argument is a pointer to unsigned
integer.
● x or X -- Read a hexadecimal integer. The corresponding argument is a pointer to unsigned
integer.
● f -- Read a floating-point value. The corresponding argument is a pointer to a floating-point
variable.
● lf -- Place before any of the floating-point conversion specifiers to indicate that a double or
long double value is to be input.
● c -- Read a character. The corresponding argument is a pointer to char, no null ('\0') is added.
● s -- Read a string. The corresponding argument is a pointer to an array of type char that is
large enough to hold the string and a terminating null ('\0') character—which is automatically
added.
● p -- Read an address of the same form produced when an address is output with %p in a
printf statement.