CS113: Introduction To Programming
CS113: Introduction To Programming
Lecture 4
Todays Lecture
Input function
Output function
Variables
Operators
Input / Output
I/O
Output printf ()
Input scanf ()
printf() function
printf ( String constant , additional arguments);
optional
print formatted
1. Displays text put inside the double quotes
2. Requires backslash character - an escape
sequence - to display some special characters e.g.
\n
3. Uses conversion specifier % character as a place
holder to be filled during printing
4. Returns the number of characters printed
printf()
"% format specifier
specify both the relative location
and the type of output that the
function should produce
Output: sum = 8
Conversion specifiers
%.2f
.2 No. of decimal places to the right of decimal
point
No compilation error
Escape Sequences
Added within strings
Performs special printing
\n newline
\r carriage return (not common)
\t tab
\a alert (beep)
\\ backslash
\ single quote
\ double quote
scanf()
Like a pattern-matching
function
Matches input with conversion
specifiers
Ignores white-space characters
Space, tab, new line character
scanf () Example
printf(Enter the value of integer);
scanf( %d , &a);
Output
Variable x
Variable name
(Identifier)
Small mailbox (memory)
x 5
Integer Variables
int means the variables can hold integers
or whole numbers (-1, 3, 0, 47)
x = 10; int x;
int x; x = 10;
// Error // Correct
Variable names
(Identifiers)
A variable name must begin with a
1. Letter
2. Underscore _ (Not Recommended)
3. The rest can be letters, digits, and
underscores
2. Type
3. Size
4. Value
117
Assignment Operator
=
Assigns a value to a variable
Is a binary operator (has two operands)
sum = variable1 + variable2;
sum gets variable1 + variable2;
Variable receiving value on left
printf( "Sum is %d\n", sum );
Similar to scanf()
%d means decimal integer will be printed
sum specifies what integer will be printed
X
2
L-value
Variables are also known as l-
values.
L-values are values that can be on
the left side of an assignment
statement.
When we do an assignment, the
left hand side of the assignment
operator must be an l-value.
Assignment
5
Operator
=6 Incorrect
X + 3 = y + 4 Incorrect
Z = x + 4 Correct
x + 4 = Z
Incorrect
When an l-value has a value
assigned to it, the current
value is overwritten.
X = 10 ; X 10
X = 30 ;
X 30
Reading variables from memory does not change them
X = X + 1;
X
10 +1
X = 11
R-values (Opposite of L-
values)
An r-value refers to any value that can be assigned
to an l-value.
R-values are always evaluated to produce a single
value.
Examples
Single numbers (5, which evaluates to 5)
Variables (x, which evaluates to whatever number was last
assigned to it)
Expressions (2+x, which evaluates to the last value of x
plus 2).
#include <stdio.h> // Pre-processor directive
int x, y, z ;
int x; int y; int z
;
Initialize Variables
int x; // Variable declaration
int x = 0; // Initialization
5%2=1
2%2=0
Highest: ()
Next: * , / , %
Lowest: +,-
Decision making:
Equality & relational
operators
Executable statements
Perform actions (calculations, input/output of
data)
Perform decisions
May want to print "pass" or "fail" given the value of a
test grade
if control statement
Simple version in this section, more detail later
If a condition is true, then the body of the if
statement executed
0 is false, non-zero is true
Control always resumes after the if structure
Decision making: Equality &
relational operators
Precedence and
Associativity of operators
Keywords in C