0% found this document useful (0 votes)
5 views62 pages

Slot 02 03 04 Basic Computation

This document covers basic computation concepts in programming, focusing on data types, variables, and operations. It explains the characteristics of various data types in C, including integral and floating-point types, and provides guidelines for declaring constants and variables. Additionally, it discusses memory operations, input/output processes, and arithmetic expressions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views62 pages

Slot 02 03 04 Basic Computation

This document covers basic computation concepts in programming, focusing on data types, variables, and operations. It explains the characteristics of various data types in C, including integral and floating-point types, and provides guidelines for declaring constants and variables. Additionally, it discusses memory operations, input/output processes, and arithmetic expressions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Basic Computation

Objectives
After studying this section, you should be able to:

 Understand what is a data type

 Declare constants and variables of a program

 Express operations on data

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.

 A data type defines:


• How the values are stored and

• How the operations on those values are performed.

 Typed languages defined some primitive data types.

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

 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.
24/02/2025 10
double Type Size Specifier
 The size of a long double depends on the environment and is
typically 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

• Floating-point types: float, double

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.

 All of these schemes represent non-negative integers identically.


 The most popular scheme is two's complement.
 To obtain the two's complement of an integer, we:
• Flip the bits (1-complement)
• Add one  2-complement

24/02/2025 16
Two's complement notation
 To obtain the two's complement of an integer, we
• Flip the bits
• Add one

 For example, we represent the integer -92 by 101001002

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.

We use the ASCII encoding sequence throughout this course


24/02/2025 20
ASCII
table for
characters

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;

 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

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,

 Assign the value of another variable to a variable,

 Output the value of a variable,

 Input a fresh value into a variable's memory location.

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?

The operator & will get the address of a variable or code.


The operator sizeof(var/type) return the size (number of byte) occupied by a variable/type
24/02/2025 29
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.
24/02/2025 30
Basic Memory Operations
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)

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’,

Assign value to a variable:


The operator =

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

Change \ to \\ 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.

 Default: Integral value  int, real number  double

 Specifying data type of constants: Suffixes after numbers.

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

Conversion rules are pre-defined in C

24/02/2025 40
Conversion Specifiers

24/02/2025 41
Example 1:
4210784 n

main
4199056

2293620 m

Format
string

scanf( “%d%d”, &n, &m) 


scanf( “%d%d”, 4210784, 2293620) means that
get keys pressed then change them to decimal
integers and store them to memory locations
4210784, 2293620.

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,…)

The function scanf receive the BLANK or


ENTER KEYS as separators.

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.

 Run the following program:


Why user do not have a
chance to stroke the ENTER
key before the program
terminate?

Modify and re-run:


getchar();

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

 Hardware for calculating expressions: ALU


 Operations that can be supported by ALU: Arithmetic, relational and
logic operations.

24/02/2025 47
1. Arithmetic Operators
Op. Syntax Description Example
+ +x leaves the variable, constant or expression y = +x ;  y = x;
unchanged

- -x reverses the sign of the variable y= -x;


+ - x+y x-y Add/substract values of two operands z= x+y; t = x-y;

*/ x*y x/y Multiplies values of two operands z= x-y;


Get the quotient of a division z = 10/3;  3
z = 10.0/3;  3.3333333
% x%y Get remainder of a integral division 17%3  2
15.0 % 3  ERROR

++ ++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

? Explain yourself the output

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

n=12: 0000 0000 0000 k=-1:


1100 1: 0000 0000 0000 0001
n>>1: -1: 1111 1111 1111 1111 (2-complement)
Sign: 0 Sign: 1
0000 0000 0000 1100
0000 0000 0000 110 1111 1111 1111 1111
Add the sign to the left” 111 1111 1 111 1111
0000 0000 0000 110  6 Add the sign to the left:
1111 1111 1111 1111  (-110)

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.

int n=3; long t=123; double x=5.3;


3*n + 620*t – 3*x
(int*int) + (int*long) - (int*double)
int + long - double
long - double
double
24/02/2025 59
6. Mixing Data Types (cont.)

 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

int m=3, k=2, n=4;


What is the results?
m<n
k<m<n
k>m>n
m<n>k
m&& k<n

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

You might also like