0% found this document useful (0 votes)
3 views

Lecture 06

Uploaded by

imalshaamashan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 06

Uploaded by

imalshaamashan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Fundamentals of Programming

COSC 11023 / COST 11023

Input / Output in C

Sachintha Pitigala
© 2023
Input/Output in C
● C has no built-in statements for input or output.

● A library of functions is supplied to perform these x

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.
2
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).

3
Types of Streams
● 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.
4
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 ;
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.

You might also like