You are on page 1of 26

INTRODUCTION TO COMPUTER PROGRAMMING

BTC 1122/ETC 1112


Uthpala Samaratunga
Lecturer
Dept. of ICT, Faculty of Technology
University of Sri Jayewardenepura
uthpalas@sjp.ac.lk

INPUT AND OUTPUT TOPIC 3


A SIMPLE C PROGRAM

Output: Welcome to C!
THE OUTPUT STATEMENT
The following line instructs the computer to perform an action,
namely to print on the screen the string of characters marked by
the quotation marks.

A string is sometimes called a character string, a message or a


literal.
The entire line, including the printf function (the “f” stands for
“formatted”).
THE OUTPUT STATEMENT
When the preceding printf statement is executed, it prints the
message Welcome to C! on the screen. The characters normally
print exactly as they appear between the double quotes in the
printf statement.
ESCAPE SEQUENCES
In the previous example, \n was not printed on the screen. The
backslash (\) as used here is called an escape character.
When encountering a backslash in a string, the compiler looks
ahead at the next character and combines it with the backslash to
form an escape sequence.
ESCAPE SEQUENCES
ESCAPE SEQUENCES: EXAMPLE 1

Output: Welcome to C!
ESCAPE SEQUENCES: EXAMPLE 2

Output: Welcome
to C!
ESCAPE SEQUENCES: EXAMPLE 3

Output: Welcome
to
C!
QUESTION: STATE WHETHER THE FOLLOWING
STATEMENTS ARE TRUE/FALSE. IF FALSE, EXPLAIN WHY.
1. Function printf always begins printing at the beginning of a new
line.
2. Comments cause the computer to display the text after // on
the screen when the program is executed.
3. The escape sequence \n when used in a printf format control
string causes the cursor to position to the beginning of the next
line on the screen.
4. A program that prints three lines of output must contain three
printf statements.
QUESTION: WRITE A C PROGRAM TO PRINT THE
FOLLOWING OUTPUT.
A SIMPLE C PROGRAM: PRINTING A VARIABLE

Output: 10
PRINTING WITH A FORMAT CONTROL STRING

This printf has two arguments, “%d" and a.


“%d” is the format control string. It contains some literal
characters to be displayed and the conversion specifier %d
indicating that an integer will be printed.
The second argument specifies the value to be printed.
PRINTF CONVERSION SPECIFIERS (FORMAT
SPECIFIERS)
PRINTING VARIABLES: EXAMPLE 1

Output: 10 1.5 4.5 u


PRINTING VARIABLES: EXAMPLE 2

Output: The value of a is 10.


It is an integer.
PRINTING VARIABLES: EXAMPLE 3

Output: b is 7.6, a is 10.


A SIMPLE C PROGRAM: SCANNING VALUES INTO
VARIABLES (USER INPUT)
THE SCANF FUNCTION AND FORMATTED INPUTS

This scanf has two arguments, "%d" and &a.


The first, the format control string, indicates the type of data that
should be entered by the user. The %d conversion specifier
indicates that the data should be an integer (the letter d stands for
“decimal integer”). The % in this context is treated by as a special
character that begins a conversion specifier.
The conversion specifier for an integer is the same in both printf
and scanf – this is true for most C data types.
THE SCANF FUNCTION AND FORMATTED INPUTS
The second argument of scanf begins with an ampersand (&) –
called the address operator – followed by the variable name.
The &, when combined with the variable name, tells scanf the
location (or address) in memory at which the variable ‘a’ is stored.
The computer then stores the value that the user enters for variable
‘a’ at that location.
THE SCANF FUNCTION AND FORMATTED INPUTS
When the computer executes the preceding scanf, it waits for the
user to enter a value for variable ‘a’. The user responds by typing
an integer, then pressing the Enter key (sometimes labeled as the
Return key) to send the number to the computer.
Functions printf and scanf facilitate interaction between the user and
the computer. This interaction resembles a dialogue and is often
called interactive computing.
CONVERSION SPECIFIERS (FORMAT SPECIFIERS)
FOR INTEGER DATA TYPES
CONVERSION SPECIFIERS (FORMAT SPECIFIERS)
FOR FLOATING DATA TYPES
SCANNING VALUES INTO VARIABLES: EXAMPLE
PRINTF AND SCANF: EXAMPLE

Execution: Enter an integer: 50


Enter another integer: 60
The two numbers entered are 50 and 60
Q&A

You might also like