Slot 02 03 04 Basic Computation
Slot 02 03 04 Basic Computation
Objectives
After studying this section, you should be able to:
24/02/2025 2
Contents
Variables and Data types Expressions
Data Types Arithmetic,
Integral Types Relational,
Floating-Point Types Logical
Declarations Bit Operators
Basic Memory Operations Shorthand Assignment Operators
Literals Mixing Data Types
Constants Casting
Assignment Operator Precedence
Output
Input
24/02/2025 3
Variables and Data types
Introduction
Instruction: A task that hardware must perform on data.
Data can be: constants, variables.
Constants: Fixed values that can not be changed when the program
executes.
Variables: Values can be changed when the program execute.
Data must be stored in the main memory (RAM).
2 basic operations on data are READ and WRITE.
Numerical data can participate in expressions.
24/02/2025 5
Variables
A variable is a name referencing to a memory
location (address) 0000 1001
1100 0011
• Holds binary data
c
• Two basic operations: set value, get value.
• When the program is compiled, the compiler will
b
determine the position where the variable is allocated.
a
Questions:
• Where is it? It’s Address
Memory
• How many bytes does it occupy? Data type
24/02/2025 6
Data Types
The C language associates a data type with each variable. Each
data type occupies a compiler-defined number of bytes.
24/02/2025 7
Arithmetic Types
The four most common types of the C language for performing
arithmetic calculations are: char, int, float, double
char: Occupies one byte and can store a small integer value, a single character or a
single symbol:
int: occupies one word and can store an integer value. In a 32-bit environment, an int occupies 4 bytes:
24/02/2025 8
Arithmetic Types (cont.)
float: typically occupies 4 bytes and can store a single-precision, floating-point number:
double: typically occupies 8 bytes and can store a double-precision, floating-point number:
24/02/2025 9
int Type Size Specifiers
Specifying the size of an int ensures that the type contains a
minimum number of bits. The three specifiers are:
• short: at least 16 bits
• long: at least 32 bits
• long long: at least 64 bits
long double type only ensures that it contains at least as many bits
as a double
24/02/2025 11
const Qualifier
Any type can hold a constant value.
A constant value cannot be changed.
To qualify a type as holding a constant value we use the keyword
const
A type qualified as const is unmodifiable.
If a program instruction attempts to modify a const qualified type,
the compiler will report an error.
24/02/2025 12
Representing Values
Hardware manufacturers distinguish integral types from
floating-point types and represent integral data and floating-
point data differently.
• Integral types: char, int
24/02/2025 13
Exercise 1:
Convert the following decimal integers to binary:
0011 1111
63 _________________________________________________
1101 1011
219 ________________________________________________
Convert the following binary notation to decimal:
117
0111 0101 ___________________________________________
59
0011 1011 ___________________________________________
24/02/2025 15
Negative and Positive Values
Computers store negative integers using encoding schemes:
• Two's complement notation,
• One's complement notation, and
• Sign magnitude notation.
24/02/2025 16
Two's complement notation
To obtain the two's complement of an integer, we
• Flip the bits
• Add one
24/02/2025 17
Exercise 2: Use signed 1-byte integral number
What is the two's complement notation of
-63_______________________________________________
1100 0001
0010 0101
-219______________________________________________
Convert the following binary notation to decimal:
1111 0101_________________________________________
-11
-69
1011 1011_________________________________________
24/02/2025 18
Unsigned Integers
We can use all of the bits available to store the value of a variable.
With unsigned variables, there is no need for a negative-value
encoding scheme.
24/02/2025 19
Cultural Symbols (characters)
We store cultural symbols using an integral data type.
We store a symbol by storing the integer associated with the symbol.
Over 60 encoding sequences have already been defined.
24/02/2025 21
Exercise 3:
You are tasked to write a program to manage a small library system. Choose the most
suitable data type for each variable based on the information provided.
The library needs to store the following information:
• Total number of books in the library (e.g., 10,000).
• Average price of books in dollars (e.g., 15.75).
• A single character indicates the book's genre ('F' for Fiction, 'N' for Non-fiction, etc.).
• The unique ID of a book (e.g., 2345678901).
• Whether a book is available or not (1 for available, 0 for not available).
• Number of pages in a book (e.g., 500).
Tasks:
• Choose an appropriate data type for each variable and explain your choice.
• Declare variables using the chosen data types and initialize them with sample values.
• Print all the values along with their data types.
24/02/2025 22
The range of values of data types
24/02/2025 23
Declaring (Creating) Variables
To create a variable, specify the data type and assign it a value:
Syntax: data_type identifier [= initial value];
For example:
char section
int numberOfClasses;
double cashFare = 2.25;
24/02/2025 24
Reserved words
24/02/2025 25
Exercise 4:
You are tasked to create a C program that calculates the area and
perimeter of a rectangle. As part of this exercise, ensure that all variable
names:
1. Follow the rules for valid naming in C.
2. Are descriptive and meaningful to improve readability.
3. Follow common naming conventions (e.g., camelCase or snake_case).
Tasks:
1. Identify the rules for valid variable names in C and apply them.
2. Choose appropriate and meaningful variable names for the program.
3. Write the program using the chosen names.
24/02/2025 26
Some operations on variables:
Assign a constant value to a variable,
24/02/2025 27
Example 1: Variable & Data Types
Output:
24/02/2025 28
Example 2: Variable & Data Types
Conversion Specifiers
& String format
Where are variables stored and how many bytes do they occupy?
24/02/2025 32
Literals: Characters, Strings
4 ways for representing a character literal:
• Enclose the character single quotes ‘A’
• Decimal ASCII code of the character 65 for ‘A’
• Octal ASCII code of the character 0101 for ‘A’,
• Hexadecimal ASCII code of the character 0x41 for ‘A’,
24/02/2025 33
Literals: Escape Sequences
Pre-defined literals for special actions:
24/02/2025 34
Literals: Escape Sequences (cont.)
Error! Why?
Modify
then run it
24/02/2025 35
Literals: Numbers
The compiler will convert directly numeric literals (constants) to binary
numbers and put them in the executable file. How long of binary
constants? They depend on their data types specified by programmers.
24/02/2025 36
2. Named Constants
Use const keyword
Syntax: const data_type constant_name = value;
For example:
Output:
24/02/2025 37
2. Named Constants (cont.)
Use the pre-processor (pre-compiled directive) #define
Syntax: #define constant_name value
For example:
Output:
24/02/2025 38
2. Named Constants (cont.)
Attention when the directive replace
#define is used:
• The compiler will not
allocate memory block for
values but all pre-defined
names in the source code
will be replaced by their
values before the translation
performs (The MACRO
REPLACEMENT)
• A name is call as a MACRO.
24/02/2025 39
3. Input/ Output variables
Monitor is
a character 3
3
device
Binary
code of 3 Convert
for 00110011
ASCII calculatin
code of 00110011 g 00000011
the digit convert
00110011
3
Keyboard is 3 3
a character
device
24/02/2025 40
Conversion Specifiers
24/02/2025 41
Example 1:
4210784 n
main
4199056
2293620 m
Format
string
24/02/2025 42
Example 2: Input a value to a variable:
scanf (“input format”, &var1, &var2,…)
Output the value of a variable:
printf (“output format”, var1, var2,…)
Format string
Data holders
24/02/2025 43
Question
Explain means of parameters of the scanf(…) and the printf(…)
functions.
Use words “left” and “right”. The assignment x=y; will copy the
value in the ….. side to the ….. side.
24/02/2025 44
Exercise 5:
Develop a C program in which 2 integers, 2 float numbers and 2 double
numbers are declared. Ask user for values of them then print out values and
addresses of them. Write down the memory map of the program.
24/02/2025 45
Expressions
Expressions
Expression is a valid association of constants, variables,
operators and functions and returns an only result.
Examples:
• 32-x+y/6 16.5 + 4/sqrt(15) * 17 – 8
• 45 > 5*x y = 17 + 6*5/9 –z*z
24/02/2025 47
1. Arithmetic Operators
Op. Syntax Description Example
+ +x leaves the variable, constant or expression y = +x ; y = x;
unchanged
++ ++x --x Increase/decrease the value of a variable Demo in the next slide.
-- x++ x-- (prefix/postfix operators)
24/02/2025 48
1. Arithmetic Operators - Example 1
24/02/2025 49
1. Arithmetic Operators - Example 2
Statistic:
•Multiply > Division
•Integral operations > floating-point ones.
24/02/2025 50
2. Relational Operators
For comparisional operators.
< <= == >= > !=
Return 1: true/ 0: false
24/02/2025 51
3. Logical Operators
Operator for association of conditions
&& (and), || (or) , ! (not)
Return 1: true, 0: false
24/02/2025 52
4. Bitwise Operators
& (and), | (or) , ^ (xor): Will act on a pair of bits at the same position in 2 operands.
<< Left shift bits of the operand (operands unchanged)
>> Right shift bits of the operand (operands unchanged, the sign is preserved.)
~ : Inverse bits of the operand.
n&m
0000 0000 0000 1100
n=12: 0000 0000 0000 1100 0000 0000 0000 1000
m= 8: 0000 0000 0000 1000 0000 0000 0000 1000 8
n|m
0000 0000 0000 1100
0000 0000 0000 1000
0000 0000 0000 1100 12
n^m
0000 0000 0000 1100
0000 0000 0000 1000
0000 0000 0000 0100 4
24/02/2025 53
4. Bitwise Operators (cont.)
n=12: 0000 0000 0000
1100
n<<1:
0000 0000 0000 1100
0000 0000 0001 100 0
0000 0000 0001 1000 (2410)
Left shift 1 bit: multiply by 2
24/02/2025 54
5. Assignments Operators
Variable = expression
Shorthand assignments:
24/02/2025 55
6. Mixing Data Types
Although the ALU does not perform operations on operands of
differing data type directly, C compilers can interpret expressions
that contain operands of differing data type.
If a binary expression contains operands of differing type, a C
compiler changes the data type of one of the operands to match the
other.
Data type hierarchy: double, float, long, int , char .
24/02/2025 56
6. Mixing Data Types (cont.)
Casting Data Type: x = y;
24/02/2025 57
6. Mixing Data Types (cont.)
Implicit Casting for the assignment
If the data type of the variable on the left side
of an assignment operator differs from the
data type of the right side operand, the 0000 0000
compiler: 0000 0000
0000 0000
• Promotes the right operand to the data type of the 0100 0001 0100 0001
left operand if the left operand is of a higher data
type than the right operand, 0000 0000
0000 0000
• Truncates the right operand to the data type of
0000 0000
the left operand if the left operand is of a lower 0001 1001 0001 1001
data type than the right operand.
24/02/2025 58
6. Mixing Data Types (cont.)
Implicit Casting for arithmetic and relational expressions
• If the operands in an arithmetic or relational expression differ in data type, the
compiler promotes the value of lower data type to a value of higher data type before
implementing the operation.
Explicit Casting
• We may temporarily
change the data type
of any operand in any
00000000
expression to obtain a 00000000
result of a certain data 00000001
00000000 00000000
type.
c n
24/02/2025 60
7. Operator Precedence
In a expression containing some more than one operator. Which operator
will perform first? Pre-defined Precedence.
We can use ( ) to instruct the compiler to evaluate the expression within
the parentheses first
24/02/2025 61
Summary
Variable is ……
Basic memory operations are…..
Expression is ……….
• Which of the following operators will change value of a variable? + - * / % ++
• Which of the following operators can accept only one operand? + - * / % --
• 13 & 7 = ?
• 62 | 53 = ?
• 17 ^ 21 = ?
• 12 >> 2 = ?
• 65 << 3 = ?
24/02/2025 62
Summary (cont.)
Expressions
• Arithmetic operators
• Relational operators
• Logical operators
• Bit operators
• Shorthand Assignment Operators
• Casting
• Precedence
24/02/2025 63