Slot 02 03 04 Basic Computation
Slot 02 03 04 Basic Computation
Objectives
22/01/2025 2
Programming Fundamentals using C
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
22/01/2025 3
Variables and Data types
Programming Fundamentals using C
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.
22/01/2025 5
Programming Fundamentals using C
Variables
A variable is a name referencing to a memory
0000 1001
location (address) 1100 0011
c
• Holds binary data
• 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
22/01/2025 6
Programming Fundamentals using C
Data Types
The C language associates a data type with each variable. Each
data type occupies a compiler-defined number of bytes.
A data type defines:
• How the values are stored and
Typed languages defined some primitive data types.
22/01/2025 7
Programming Fundamentals using C
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:
22/01/2025 8
Programming Fundamentals using C
double: typically occupies 8 bytes and can store a double-precision, floating-point number:
22/01/2025 9
Programming Fundamentals using C
Standard C does not specify that a long double must occupy a
minimum number of bits, only that it occupies no fewer bits than a
double.
22/01/2025 10
Programming Fundamentals using C
long double type only ensures that it contains at least as many bits
as a double
22/01/2025 11
Programming Fundamentals using C
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.
22/01/2025 12
Programming Fundamentals using C
Representing Values
Hardware manufacturers distinguish integral types from
floating-point types and represent integral data and floating-
point data differently.
• Integral types: char, int
• Floating-point types: float, double
22/01/2025 13
Programming Fundamentals using C
Exercise 1:
Convert the following decimal integers to binary:
0011 1111
63 _________________________________________________
1101 1011
219 ________________________________________________
Convert the following binary notation to decimal:
0111 0101 ___________________________________________
117
0011 1011 ___________________________________________
59
22/01/2025 15
Programming Fundamentals using C
22/01/2025 17
Programming Fundamentals using C
22/01/2025 18
Programming Fundamentals using C
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.
22/01/2025 19
Programming Fundamentals using C
ASCII
table for
characters
22/01/2025 21
Programming Fundamentals using C
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.
22/01/2025 22
Programming Fundamentals using C
22/01/2025 23
Programming Fundamentals using C
For example:
char section
int numberOfClasses;
double cashFare = 2.25;
Rules for naming variables are:
• Names can contain letters, digits and underscores.
• Names must begin with a letter or an underscore (_)
• Names are case-sensitive (myVar and myvar are different variables)
• Names cannot contain whitespaces or special characters like !, #, %, etc.
• Must not be a C reserved word
22/01/2025 24
Programming Fundamentals using C
Reserved words
22/01/2025 25
Programming Fundamentals using C
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.
22/01/2025 26
Programming Fundamentals using C
22/01/2025 27
Programming Fundamentals using C
Output:
22/01/2025 28
Programming Fundamentals using C
Conversion Specifiers
& String format
Where are variables stored and how many bytes do they occupy?
Questions as Summary
What is a variable?
What is a data type?
Characteristics of a data type are …… and …
The size of the int data type is …. Bytes.
Choose the wrong declarations:
int n = 10;
char c1, c2 = ‘A’;
int m = 19; k = 2;
char c3; int t;
Float f1; f2 = 5.1;
Explain little-endian ordering and big-endian ordering.
22/01/2025 30
Basic Memory Operations
Programming Fundamentals using C
1. Literals
Constant values are specified
directly in the source code.
They can be
• Character literals (constant
characters)
• String literals(constant strings)
• Number literals (constant
numbers)
22/01/2025 32
Programming Fundamentals using C
22/01/2025 33
Programming Fundamentals using C
22/01/2025 34
Programming Fundamentals using C
Modify
then run it
22/01/2025 35
Programming Fundamentals using C
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.
Default: Integral value int, real number double
Specifying data type of constants: Suffixes after numbers.
22/01/2025 36
Programming Fundamentals using C
2. Named Constants
Use const keyword
Syntax: const data_type constant_name = value;
For example:
Output:
22/01/2025 37
Programming Fundamentals using C
Output:
22/01/2025 38
Programming Fundamentals using C
22/01/2025 39
Programming Fundamentals using C
Keyboard is 3 3
a character
device
22/01/2025 40
Programming Fundamentals using C
Conversion Specifiers
22/01/2025 41
Programming Fundamentals using C
Example 1:
4210784 n
main
4199056
2293620 m
Format
string
22/01/2025 42
Programming Fundamentals using C
Format string
Data holders
22/01/2025 43
Programming Fundamentals using C
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.
22/01/2025 44
Programming Fundamentals using C
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.
Run the following program:
Why user do not have a
chance to stroke the ENTER
key before the program
terminate?
22/01/2025 45
Expressions
Programming Fundamentals using C
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
Hardware for calculating expressions: ALU
Operations that can be supported by ALU: Arithmetic, relational and
logic operations.
22/01/2025 47
Programming Fundamentals using C
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)
22/01/2025 48
Programming Fundamentals using C
22/01/2025 49
Programming Fundamentals using C
Statistic:
•Multiply > Division
•Integral operations > floating-point ones.
22/01/2025 50
Programming Fundamentals using C
2. Relational Operators
For comparisional operators.
< <= == >= > !=
Return 1: true/ 0: false
22/01/2025 51
Programming Fundamentals using C
3. Logical Operators
Operator for association of conditions
&& (and), || (or) , ! (not)
Return 1: true, 0: false
22/01/2025 52
Programming Fundamentals using C
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
22/01/2025 53
Programming Fundamentals using C
k=-1:
n=12: 0000 0000 0000
1: 0000 0000 0000 0001
1100
-1: 1111 1111 1111 1111 (2-complement)
n>>1:
Sign: 1
Sign: 0
0000 0000 0000 1100
1111 1111 1111 1111
0000 0000 0000 110
111 1111 1 111 1111
Add the sign to the left”
Add the sign to the left:
0000 0000 0000 110 6
1111 1111 1111 1111 (-110)
22/01/2025 54
Programming Fundamentals using C
5. Assignments Operators
Variable = expression
Shorthand assignments:
22/01/2025 55
Programming Fundamentals using C
22/01/2025 56
Programming Fundamentals using C
22/01/2025 57
Programming Fundamentals using C
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
22/01/2025 60
Programming Fundamentals using C
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
22/01/2025 61
Programming Fundamentals using C
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 = ?
22/01/2025 62
Programming Fundamentals using C
Summary (cont.)
Expressions
• Arithmetic operators
• Relational operators
• Logical operators
• Bit operators
• Shorthand Assignment Operators
• Casting
• Precedence
22/01/2025 63