Pspc Lecture 4 - Variables Input and Output
Pspc Lecture 4 - Variables Input and Output
VARIABLES
Variablesare used to store or hold values in
the program.
They represent a small part of storage in the
computer memory (RAM).
HOW TO USE A VARIABLE?
Declaration:
datatype variable_name ;
Assigning a value to a variable:
variable_name=value;
Variable names are called as identifiers
A variable cannot be used in the program
without declaration
It is advisable to:
Give variable names that can easily convey their
meaning
Keep variable names short and simple
QUICK EXERCISE
Let’s
say we have the following code:
int x=5;
printf(“x”);
What will be the output?
x
But we are expecting the value:
5
HOW TO PRINT A VARIABLE?
Syntax of scanf:
scanf(“format specifier”, &variable_name);
Example:
float x;
scanf(“%f”,&x);
Let us see how this works in the program..
HOW TO INPUT MULTIPLE
VARIABLES?
Syntax:
scanf(“list of format specifiers”, list of
variables with each preceded by a &);
Example:
int x,y,z;
scanf (“%d%d%d”, &x,&y,&z);
<stdio.h> library also includes scanf along
with printf
SOME GENERAL POINTS TO NOTE
Same as printf, format specifiers must match
the variables.
The format specifiers are associated with
variables from left to right in scanf also.
All variable values entered through the
keyboard are stored in the variables from left
to right
All input values must be separated by a
“space” or “enter” on the output screen.
Make sure that every input is prompted by a
Find the output of this program
(Assume any value for a and b)