Part 2
Variables and Constants
Declarations
Data Types
printf, scanf
Variables and Constants
Data can be stored in memory (RAM) to be used as
needed.
Variables and symbolic constants are names for these
memory locations.
Variables refer to memory locations in which the value stored
may change throughout the execution of the program.
Constants refer to memory locations in which the values do not
change.
Use a declaration to set aside memory space.
Declarations
Declarations are statements that tell the compiler the size of
the memory space to allocate (by specifying the type) and
the identifier(name) that will be used to refer to that space
A variable must be declared before it can be used!
Variable declarations have the syntax:
data_type identifier(name);
int salary;
short int age;
long int total_sum;
double circle_radius;
char letter;
It is also possible to declare multiple variables in one line:
data_type name1,name2,…….,nameN;
int x,y,z;
double d1,d2,Area;
Data Types
To allocate the memory space for a variable you
must state:
The type of the stored data (implies the size)
The identifier (name)
The classification of data types:
Integers(no fractions)
Real numbers (with fractional parts)
Characters (ASCII code) may be letters, numbers or any
other symbol.
Variable types in C++
Data Types : 1-Integer
An integer type is a number without a fractional
part
Designed to hold whole numbers
Can be signed or unsigned:
–12 -6 +3
Available in different sizes (number of bytes): short
int, int, and long int
Size of short int size of int size of long int
Data Types: 2-Floating-Point
A floating-point type is a number with a fractional
part
Designed to hold real numbers
12.45 -3.8
All numbers are signed
Available in different sizes (number of bytes): float,
double, and long double
Size of float size of double
Data Type: 3- character
Used to hold characters like ‘b’
Stored in memory as a numerical value:
Representing Letters and Symbols
Characters are represented inside the computer in a binary format
(like everything else!).
A number of coding systems exists to dictate this representation: the
mapping between letters and numbers
Most of today’s computers use the American Standard Code for
Information Interchange (ASCII code) to represent each letter or
character as an 8-bit (or 1-byte) binary code.
The ASCII code uses the 8-bits to represent:
26 uppercase English letters
26 lowercase English letters
A number of punctuation symbols and other special characters
For example, the ASCII representation for ‘A’ is 65, ‘B’ is 66, ‘a’ is 97
and b is 98. The complete table is shown next slide
ASCII Chart
Identifiers (Variables)
Identifiers are names (or symbols) used by the
programmer to refer to items such as variables,
constants, functions
Identifiers (variables) should be descriptive of what
they stand for
Examples: sum, total, area
The “Name” used for identifiers must follow specific
guidelines in C/C++ to be valid
Variable naming
Variable name must start with
Letter (a to z or A to Z)
or
Underscore (_)
or
$
Variable name cannot start with a digit
However, the rest of the variable name may contain
letters, underscores, dollar signs or digits
Variable naming (Cont.)
A variable name can not be any of the following
C++ reserved words:-
Valid and Invalid Identifiers/Variable
Assignment Statement
The assignment statement is used to store values in
memory locations
The general syntax is
identifier = expression;
Where expression may be simple or complex
expression (equation)
Example of a simple expression X = 5;
Example of a complex expression X = (5+4)/3;
The expression is evaluated first then the result is stored
at the identifier
Later we will discuss expressions in more details
Assignment statement (Cont.)
The assignment statement can be also used to
initialize variables
Examples:
int num1 = 15; // initialize to a constant value
int num2 = num1; // initialize to the value of another
variable
char letter = ‘A’; // initialize to a character
(Note the use of quotations with characters to differentiate it
from variables)
Constant declarations
Constants are used to store values that never change
during the program execution.
Using constants makes programs more readable and
maintainable.
Syntax:
const type identifier = expression;
Examples:
const double x = 7.8;
const double r = x * 2;
const double PI ; // error must specify a value
Constant (Cont.)
C-Style constant declaration
constants can also be specified using the preprocessor
directive #define
Example: #define PI 3.14159
The preprocessor(part of the compiler) replaces the
identifier PI by the text 3.14159 throughout the
program
The major drawback of #define is that the data
type of the constant is not specified
printf Statement
printf is used when we want to display formatted
information on the screen
The library stdio.h must be included
Consists of a format string following by a number
of values/arguments.
Example:
printf Statement (Cont.)
The format string contains the text to be displayed
It can optionally contain embedded format tags that are
substituted by the values specified in subsequent
argument(s) and formatted as requested
The number of arguments following the format string
should at least be as much as the number of format tags
What is displayed?
resut1=25 and result2=30
printf tags
The tags follow the following prototype:
printf-Precision
.precesion
For integer, precision specifies the minimum number of digits to be written. If the
value to be written is shorter than this number, the result is padded with leading
zeros. The value is not truncated even if the result is longer. A precision of 0 means
that no character is written for the value 0.
For e, E and f specifiers : this is the number of digits to be printed after the decimal
point.
For s: this is the maximum number of characters to be printed.
For c type: it has no effect.
If the period is specified without an explicit value for precision, 0 is assumed.
printf-Specifiers
What is the output?
scanf Statement
scanf is used when we want to read information
from the standard input (typically the keyboard)
The library stdio.h must be included
Consists of a format string following by a number of
values/arguments addresses.
The tags follow the following prototype:
%[width][modifier]type
scanf Statement (Cont.)
Example: the following code reads an integer and
stores the value in a variable x
int x;
scanf (“%d”, &x);
Example: in the following code:
int x;
scanf ("%6d", &x);
What is the value of x after you’ve entered 1234567?
Answer: 123456
scanf Statement (Cont.)
Example: in the following code
int x,y;
scanf ("%3d %3d", &x,&y);
What are the values of x and y after you’ve entered
12345678?
Answer: x=123 y=456
scanf Statement (Cont.)
Example: the following code reads a float and stores it in
variable x.
float x;
scanf (“ %f ”, &x)
Example: in the following code
double x, y;
scanf ("%4lf %4lf ",&x, &y);
What are the values of x and y after you’ve entered 1.2345.678?
Answer: x=1.23 y=45.6
Note that the decimal point [.] is counted
Note that we use %lf when using double
Example #1
Write a C++ program that reads two integers and
prints the result of their addition.
The program should tell the user when to enter each
number by printing a message like “Please enter the
first number”
The program must print the result in this manner :
The result = 12233……
Example #1
Example #2
What is wrong with the following code (part of a
program)?