Lecture2 DataTypesOperators
Lecture2 DataTypesOperators
to
Programming
CSC1102 &1103
Lecture-2
American International University Bangladesh
Dept. of Computer Science
Faculty of Science and Technology
Lecture 2: Outline
• Variables, Data Types, and Arithmetic Expressions
• Working with Variables
– Understanding Data Types and Constants
• The Basic Integer Type int
• The Floating Number Type float
• The Extended Precision Type double
• The Single Character Type char
• The Boolean Data Type _Bool
• Storage sizes and ranges
• Type Specifiers: long, long long, short, unsigned, and signed
– Working with Arithmetic Expressions
• Integer Arithmetic and the Unary Minus Operator
• The Modulus Operator
• Integer and Floating-Point Conversions
– Combining Operations with Assignment: The Assignment Operators
– Types _Complex and _Imaginary
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
Variables - Examples
int a; // declaring a variable of type int
L-value R-value
letter = ‘A'; /* OK */
letter = A; /* NO! Compiler thinks A is a variable */
letter = “A"; /* NO! Compiler thinks “A" is a string */
letter = 65; /* ok because characters are really
stored as numeric values (ASCII code),
but poor style */
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.
Working with arithmetic expressions
• Basic arithmetic operators: +, -, *, /
• Precedence: one operator can have a higher priority, or precedence,
over another operator.
– Example: * has a higher precedence than +
– a+b*c
– if necessary, you can always use parentheses in an expression to force the
terms to be evaluated in any desired order.
• 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