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

TL01.1 IPL-DataTypesOperators 1

The document outlines the course 'Introduction to Programming' focusing on variables, data types, and arithmetic expressions, taught by Mashiour Rahman at AIUB. It covers topics such as the history of C/C++, data storage concepts, variable concepts, and basic data types in C, including their declaration and memory management. The document emphasizes the importance of understanding data types and variable naming conventions in programming.

Uploaded by

nazilrahman992
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views24 pages

TL01.1 IPL-DataTypesOperators 1

The document outlines the course 'Introduction to Programming' focusing on variables, data types, and arithmetic expressions, taught by Mashiour Rahman at AIUB. It covers topics such as the history of C/C++, data storage concepts, variable concepts, and basic data types in C, including their declaration and memory management. The document emphasizes the importance of understanding data types and variable naming conventions in programming.

Uploaded by

nazilrahman992
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Data and their types_1

Variables, Data Types, and Arithmetic Expressions


Course Title: Introduction to Programming

Course: Introduction to Programming (Theory)


Code: CSC 1102
Category: Theory – 1.5 hours ⨯ 2 = 3 hours/week

Lecturer No: 1.1 Week No: 1 Semester: Summer 24-25

Course Instructor: Mashiour Rahman


Rank: Associate Professor and Dean
Email: [email protected]
Room: DNGA06 - D-Building – Ground (Mezzanine) floor

Department of Computer Science


Faculty of Science and Technology

© Copyright – American International University-Bangladesh (AIUB) 1/x


Lecture 1.1: Outline
⌘ Natural vs Programming Language
⌘ History of C/C++
⌘ Data Storage Concepts
⌘ Variable Concepts
▦Variable Names (Identifier)
▦Data storage
▦Understanding Data Types
▧ Storage sizes and ranges
▧ Memory Management
▦Operators
▧ Assignment Operator
▧ Different operators and their precedence and associativity
▦Working with Arithmetic Expressions
▦Combining Operations with Assignment: The Assignment Operators

© Copyright – American International University-Bangladesh (AIUB) 2/x


Natural language vs. programming language

⌘ A language is a tool for expressing and recording human thoughts


⌘ A programming language is defined by a certain set of rigid rules, much
more inflexible than any natural language

© Copyright – American International University-Bangladesh (AIUB) 3/x


4
History of C and C++
⌘ History of C
▦ Evolved from two other programming languages
▧ BCPL and B
▤ “Typeless” languages
▦ Dennis Ritchie (Bell Laboratories)
▧ Added data typing, other features
▦ Development language of UNIX
▦ Hardware independent
▧ Portable programs
▦ 1989: ANSI standard
▦ 1990: ANSI and ISO standard published
▧ ANSI/ISO 9899: 1990

© Copyright – American International University-Bangladesh (AIUB) 4/x


5
History of C and C++
⌘ History of C++
▦ Extension of C
▦ Early 1980s: Bjarne Stroustrup (Bell Laboratories)
▦ “Spruces up” C
▦ Provides capabilities for object-oriented programming
▧ Objects: reusable software components
▤ Model items in real world
▧ Object-oriented programs
▤ Easy to understand, correct and modify
▦ Hybrid language
▧ C-like style
▧ Object-oriented style
▧ Both

© Copyright – American International University-Bangladesh (AIUB) 5/x


6
Structured Programming
⌘ Structured programming (1960s)
▦ Disciplined approach to writing programs
▦ Clear, easy to test and debug, and easy to modify
⌘ Pascal
▦ 1971: Niklaus Wirth
⌘ Ada
▦ 1970s - early 1980s: US Department of Defense (DoD)
▦ Multitasking
▧ Programmer can specify many activities to run in parallel

© Copyright – American International University-Bangladesh (AIUB) 6/x


Data Storage Concept

Input: takes Output: produces


Process: any well-defined some value or set
some value or
computational procedure of values
set of values

⌘ We have already studied how computers represent information using binary number
system.
⌘ Now we will learn how to represent information in our programming, which is the
basis for our computational processes.
⌘ To do this, we need to decide on representations for numeric values and for symbolic
ones.
⌘ We will use the concept of variables.

© Copyright – American International University-Bangladesh (AIUB) 7/x


Variables concepts
⌘ Let me ask you to retain the number 5 in your mental memory, and then I ask you to
memorize also the number 2 at the same time. You have just stored two different values (5,
2) in your memory.
⌘ Now, if I ask you to add 1 to the first number, you should be retaining the numbers 6 (that is
5+1) and 2 in your memory. Now subtract the second value from the first and you obtain 4
as result.
⌘ The whole process that you have just done with your mental memory is a simile of what a
computer does with two variables. The same process can be expressed in C/C++ with the
following instruction set:
1. a = 5
2. b = 2
3. a = a + 1
4. result = a - b
⌘ Obviously, this is a very simple example since we have only used two small integer values but
consider that your computer can store millions of numbers like these at the same time and
conduct sophisticated mathematical operations with them.
⌘ Therefore, we can define a variable as a portion of memory to store a determined value.
⌘ Each variable needs an identifier that distinguishes it from the others. For example, in the
previous code the variable identifiers were a, b and result.
© Copyright – American International University-Bangladesh (AIUB) 8/x
Variables concepts
⌘ So, a variable is a storage location which contains some known or unknown quantity or
information, a value.
⌘ The associated symbolic name of the variable is an identifier (a, b and result in
previous example) that identifies that variable within the program.
⌘ The variable name is the usual way to reference the stored value; this separation of
name and content allows the name to be used independently of the exact information it
represents.
⌘ The identifier in computer source code can be bound to a value during run time, and
the value of the variable may thus change during the course of program execution.
⌘ In computing, a variable may be employed in a repetitive process: assigned a value in
one place, then used elsewhere, then reassigned a new value and used again in the
same way.
⌘ Compilers have to replace variables' symbolic names with the actual locations of the
data in the memory.

© Copyright – American International University-Bangladesh (AIUB) 9/x


Variables
⌘ Programs can use symbolic names for storing computation data
⌘ Variable: a symbolic name for a memory location
▦ programmer doesn’t have to worry about specifying (or even knowing) the value of
the location’s address
⌘ Variables have to be declared before they are used
▦ Variable declaration: [symbolic name(identifier), type]
⌘ Declarations that reserve storage are called definitions
▦ The definition reserves memory space for the variable, but doesn’t put any value
there.
⌘ Values get into the memory location of the variable by initialization or assignement

© Copyright – American International University-Bangladesh (AIUB) 10/x


Variable names
⌘ Rules for valid variable names (identifiers) in C :
▦ Name must begin with a letter or underscore ( _ ) and can be followed by any
combination of letters, underscores, or digits.
▦ Any name that has special significance to the C compiler (reserved words) cannot be
used as a variable name.
▦ Examples of valid variable names: Sum, pieceFlag, I, J5x7, _sysflag,
Number_of_moves
▦ Examples of invalid variable names: sum$value, 3Spencer, int.
▦ C is case-sensitive: sum, Sum, and SUM each refer to a different variable!
▦ Variable names can be as long as you want, although only the first 63 (or 31)
characters might be significant. (Anyway, it’s not practical to use variable names
that are too long)
▦ Choice of meaningful variable names can increase the readability of a program

© Copyright – American International University-Bangladesh (AIUB) 11/x


What data do we store in variables?
⌘ A computing device is an electronic device, and we know It can only store signals.
Signals which can be converted to binary number system and then to decimal which we
human can understand. Which also indicates that, we can ONLY store NUMBERS in a
computing device.
⌘ Then what about the symbols ‘A’, ‘b’, ‘6’, ’#’, etc. or floating-point
numbers 0.78, 65.71, 1092.3467, etc.
⌘ Each symbol have their ASCII value. For example, ‘A’ = 65. And ‘A’ is stored or
compute as 65. So, if we give two inputs - a number and a symbol, 65 A
Here, 65 is an integer, ‘A’ is a character but represented as 65. So, we have two 65, one
as integer, another as character. How to differentiate between the two.
Same conflict can arise among integer and floating-point numbers as well.
⌘ So, DATA TYPE is used for each variable to define what type of data it can hold/store
with defined memory size and format.

© Copyright – American International University-Bangladesh (AIUB) 12/x


Basic Data types
⌘ Basic data types in C: int, float, char, and _Bool.
▦ Data type int: can be used to store integer numbers (values with no decimal
places)
▦ Data type float: can be used for storing floating-point numbers (values
containing decimal places).
▦ Data type char: can be used to store a single character symbol, such as the letter
'a', the digit symbol '6', or a semicolon ‘;'.
▦ Data type _Bool: can be used to store just the values 0 or 1 (used for indicating a
true/false situation). This type has been added by the C99 standard (was not in
ANSI C)

© Copyright – American International University-Bangladesh (AIUB) 13/x


Declaring variables
⌘ Variable declaration: Data_type Variable_name;
▦ Examples:
What type of Which variable names
int a;
data + are allowed
float b;
memory size
char c;
int x, y, z; (multiple variables)
int a = 10; (with initialization)

⌘ Rules for Variable Names:


▦ Must begin with a letter or an underscore (_).
▦ Can contain letters, digits, and/or underscores.
▦ Cannot use reserved keywords (e.g., int, return).
▦ Case-sensitive (e.g., Age and age are different).

© Copyright – American International University-Bangladesh (AIUB) 14/x


Data types
By default, all data types are signed, i.e. 1 bit is used as signed bit (0 for positive, 1 for negative)
where the range is from lowest negative to highest positive number based on their size less 1 bit.
For only positive range need to write type specifier unsigned before the data type during
declaration where the range will be from 0 to highest positive number based on their total size.

Data Size Description


Type
Whole numbers, without decimals.
4 Signed (default) range: -231 to +(231-1); 1 bit used for sign bit; 31 bits for data.
int
bytes Unsigned (default) range: 0 to 232; 32 bits for data.
Modifiers: long int or just long (8 bytes).
float 4 bytes Fractional numbers, containing one or more decimals. Sufficient for storing 7
decimal digits. 1 bit used signed bit (default), 8 bits for exponent part, 23 bits for
significant part (9 bits for exponent part for unsigned).
double (8 bytes, signed bit: 1, exponent part: 11 bits, significant part: 52 bits ).
char 1 byte Single character/letter/number, or ASCII values
bool 1 byte true or false values. Requires 1 bit to represent 0 for false and 1 for true but
assigned 1 byte as 1 byte is lowest addressable unit.

© Copyright – American International University-Bangladesh (AIUB) 15/x


Storage sizes and ranges
⌘ Every type has a range of values associated with it.
⌘ This range is determined by the amount of storage that is allocated to store a
value belonging to that type of data.
⌘ In general, that amount of storage is not defined in the language. It typically
depends on the computer you’re running, and is, therefore, called
implementation- or machine-dependent.
▦ For example, an integer might take up 32 bits on your computer, or it might be
stored in 64. You should never write programs that make any assumptions about the
size of your data types !
⌘ The language standards only guarantees that a minimum amount of storage
will be set aside for each basic data type.
▦ For example, it’s guaranteed that an integer value will be stored in a minimum of 32
bits of storage, which is the size of a “word” on many computers.

© Copyright – American International University-Bangladesh (AIUB) 16/x


Memory Management
⌘ A variable is an area of memory that has been given a name. For example: the
declaration int x; acquires four bytes memory area and this area of memory that has
been given the name x.
⌘ One can use the name to specify where to store data. For example: x=10; is an
instruction to store the data value 10 in the area of memory named x.
⌘ The computer access its own memory not by using variable names but by using a
memory map with each location of memory uniquely defined by a number, called
the address of that memory location.
⌘ To access the memory of a variable the program uses the & operator. For Example: &x
returns the address of the variable x.

&x represents the memory area of variable named x.

int
x;
x represents the value stored inside the area of variable named x.
© Copyright – American International University-Bangladesh (AIUB) 17/x
18
Assignment Operators
⌘ Basic Assignment Operator (=)
▦ Assigns the value on the right-hand side (R-value) to the variable on the left-hand (L-value)
side.
int a;
a = 10; // Assigns 10 to variable ‘a’
// stores 10 in the memory allocated for variable ‘a’
⌘ Compound Assignment Operators

Operator Example Equivalent


to
+= a += 5 a = a + 5
-= a -= 5 a = a - 5
*= a *= 5 a = a * 5
/= a /= 5 a = a / 5
%= a %= 5 a = a % 5
⌘ Chaining Assignment
int x, y, z;
x = y = z = 20; // All variables are assigned the value 20

© Copyright – American International University-Bangladesh (AIUB) 18/x


19
Assignment Operators
⌘ Lvalues
▦ Expressions that can appear on left side of equation
▦ Can be changed (I.e., variables)
▧ x = 4;
⌘ Rvalues
▦ Only appear on right side of equation
▦ Constants, such as numbers (i.e. cannot write 4 = x;)
⌘ Lvalues can be used as rvalues, but not vice versa

© Copyright – American International University-Bangladesh (AIUB) 19/x


Variables – Example for integer

int a; // declaring a variable of type int of size 4


bytes
int sum, a1, a2; // declaring 3 variables
int x = 7; // declaring and initializing a variable
a = 5; // assigning to variable a the value 5
a1
a1 =
= a;
a1 + 1; //// assigning
assigning toto variable
variable a1a1 the
the value
value ofof a
a1+1
L-value R-value // (increasing value of a1 by 1)

© Copyright – American International University-Bangladesh (AIUB) 20/x


Assigning values to char
char letter; // declare variable letter of type char

letter = 'A'; // OK
letter = A; // NO! Compiler thinks A is a variable
letter = "A"; /* NO! Compiler thinks "A" is a string (group
of
characters) */
letter = 65; /* ok because characters are basically stored
as
numeric values (ASCII code), but poor style
*/

© Copyright – American International University-Bangladesh (AIUB) 21/x


Working with arithmetic expressions
⌘ Basic arithmetic operators: +, -, *(×), /, % (mod)
⌘ Precedence: one operator can have higher precedence (priority), over another
operator.
▦ Example: * has a higher precedence than +
▦ a + b * c, here, b * c will be executed first. The result will be added to a.
▦ if necessary, you can always use parentheses in an expression to force the terms to
be evaluated in any desired order, (a + b) * c.
⌘ Associativity: Expressions containing operators of the same precedence are evaluated
either from left to right or from right to left, depending on the operator. This is known
as the associative property of an operator
▦ Example: + has a left to right associativity
⌘ In C++ there are many more operators (more later).

© Copyright – American International University-Bangladesh (AIUB) 22/x


Precedence of Operators
Category Operator Associativity
Postfix () [] -> . ++ -- Left to right
Unary + - ! ~ ++ -- (type) * & sizeof Right to left
Multiplicative * / % Left to right
Additive + - Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %= >>= <<= &= ^= |= Right to left
Comma , Left to right
© Copyright – American International University-Bangladesh (AIUB) 23/x
Working with arithmetic expressions
#include <iostream>
using namespace std;
int main (void){
int a = 100, b = 2, c = 25, d = 5;
int result;
result = a - b; // subtraction
cout<<"a - b = "<< result <<endl; //a – b = 98
result = b * c; // multiplication
cout<<"b * c = "<< result <<endl; //b * c = 50
result = a / c; // division
cout<<"a / c = "<< result <<endl; //a / c = 4
result = a + b * c / d; // precedence
cout<<"a + b * c / d = "<< result <<endl; //a + b * c / d
= 110
return 0;
}

© Copyright – American International University-Bangladesh (AIUB) 24/x

You might also like