Variables and C Data Types PowerPoint PPT Presentation

presentation player overlay
1 / 24
About This Presentation
Transcript and Presenter's Notes

Title: Variables and C Data Types


1
Variables and C Data Types
2
Variables
  • Programs like Hello World seen in the last
    lecture has little use in practice
  • There is no room for change. It simply prints a
    predetermined string onto the screen every time
    it is executed.
  • A more interesting program might have different
    behavior under different circumstances.

3
Variables (cont)
  • For instance, a program that asks for two numbers
    and outputs their sum is much more useful than
    the previous.
  • This program needs placeholders for the incoming
    values.
  • These placeholders are called variables

4
Variables (cont)
  • Variables are used to hold data within a program
    (the data is held in your computers main
    memory).
  • A program can read-from and write-to variables.
    That is, their values can vary.
  • Every variable consists of two parts
  • The name of a variable is tied to a location in
    memory.
  • Its data type.

5
Data Type Integers
  • An integer value is any number that has no
    decimal point.
  • 123 -45000 1432431 0
  • are all valid integers.
  • 1,244 is not a valid integer in C no commas
    are used. Neither is 12 because is not a valid
    part of an integer.

6
Data Type Integers (cont)
  • The largest and smallest integers supported is
    dependent of the computer on which the program is
    compiled.
  • Most of todays computers use 32-bits to
    represent an integer, so 232 values can be
    represented.
  • Integers can be signed or unsigned.
  • Unsigned integers only account for 0 and positive
    numbers.
  • Signed integers need also account for negative
    numbers.
  • So if 232 numbers can be represented in 32-bit
    systems, will the sign factor affect the range of
    supported integers???
  • Yes Indeed it will.
  • Unsigned Integers will range from 0 to
    4294967295.
  • Signed Integers will range from -2147483648 to
    2147483647.

7
Data Types Floating Point Numbers
  • Floating-point numbers have a decimal point, and
    they can also be signed or unsigned.
  • There are three types float, double, or long
    double.
  • The differences between these are their supported
    range and precision.

8
Data Types Floating Point Numbers (cont)
  • To represent a floating point number
  • float uses 32 bits (4 bytes)
  • double uses 64 bits (8 bytes)
  • long double uses 128 bits (16 bytes)
  • The tradeoff is storage vs. precision and range.
  • What exactly is the precision and range, and how
    are floating point numbers represented in binary
    format?

9
Data Types Floating Point Numbers (cont)
  • Floating-point numbers can be written in
    exponential notation
  • 134.56 or 1.3456e2
  • -0.00345 or -3.45e-3
  • Here, e is short for times ten to the power of,
    just like in scientific notation.
  • float Range /- 3.4e /- 38 (7 digits)
  • double Range /- 1.7e /- 308 (15 digits)
  • long double Range /- 1.7e /- 308 (15 digits)

10
Data Type Characters
  • Characters include
  • All letters of the alphabet (upper and lower
    case).
  • The symbolic representation of digits 0 9.
  • All various symbols such as , !
  • A character value is simply one of these in
    single quotes, such as A or 8.
  • Note here, though, 8 is NOT the same as 8
    because 8 is the symbolic representation of the
    character, 8, and 8 is the integer 8

11
Data Type Characters (cont)
  • Characters usually take up 8 bits (1 byte)
  • That means there are 28 (or 256) different
    characters, and every number within the range of
    0, 255 is mapped onto some character.
  • So a character really boils down to a numerical
    representation known as ASCII encoding.
  • See http//www.asciitable.com for a list of all
    256 characters and their corresponding codes.

12
Data Type Boolean
  • The Boolean data type is used for just two
    values true and false
  • Like characters, they only consume 1 byte of
    storage.
  • It is worth noting that when dealing with Boolean
    values in C, any number other than 0 is always
    interpreted as true.

13
Arithmetic Operations
  • The basic operations of , - , , and / are
    available, as is which is used for modulus
    division (returns the remainder).
  • A simple arithmetic expression is of the form
  • operand operator operand
  • 5 3
  • So.. how can you use them in a program?

14
Arithmetic Operations (cont)
  • // Performs some arithmetic operations
  • include ltiostream.hgt
  • using namespace std
  • int main()
  • cout ltlt 5 3 ltlt (5 3)
  • cout ltlt \n 4 3 ltlt (4 3)
  • cout ltlt \n 5 / 2 ltlt (5 / 2)
  • return 0

15
Integer Division
  • In C, (15 / 2) is 7.
  • Remember that integers have no fractional parts
  • There is no rounding in integer division. If your
    program contained
  • cout ltlt (15 / 16)
  • The output would be 0.
  • The fractional part of an integer division is
    truncated so the result can be an integer.
  • If you need the remainder, use .
  • If you need a decimal answer, make your operands
    floating-point numbers
  • cout ltlt (15.0 / 16.0)

16
Expression Types
  • An expression that contains only integer operands
    is an integer expression.
  • An expression that contains only floating point
    operands is a floating-point expression.

17
Mixing Expression Types
  • A mixed-mode expression has both floating-point
    and integer data types.
  • The rules governing the data type of the result
    are
  • If both operands are integers, the result is an
    integer.
  • If one operand is a floating-point number, then
    the result is a double.

18
Operator Precedence andAssociativity
  • The minus sign can also be used as a unary
    operator that serves to negate the sign of a
    number.

19
Declaration Statements
  • Before you can use a variable, you must declare
    it.
  • This allows the computer to set aside the memory
    that will be needed for that variable.
  • Recalling that variables consist of a name and
    its data type, C variable declarations are of
    the form
  • dataType variableName
  • dataType can be int, float, char, double, long
    double, unsigned int, ...
  • variableName must obey the identifier rules we
    discussed.

20
Declaration Example
  • include ltiostream.hgt
  • using namespace std
  • int main()
  • int age
  • float wage
  • char initial
  • long double height
  • return 0

21
Variable Assignments
  • Variables can be assigned values during or after
    declaration, but never before (why?)
  • Assignment is done with the equals sign
  • height 67.34
  • initial E
  • //initial E will not work as expected.
  • double totalPay salary overtime
  • Variable assignment is NOT commutative! The
    variable must always be on the left side of an
    equals sign. The following is NOT valid
  • 67 x

22
Assignment Example
  • include ltiostream.hgt
  • using namespace std
  • int main()
  • double totalPaid
  • double numberPaid
  • double averagePaid
  • char initial
  • totalPaid 58.24
  • numberPaid 14.0
  • averagePaid (totalPaid / numberPaid)
  • initial D
  • cout ltlt My first initial is ltlt initial ltlt
    \n
  • initial C //Assigned a new value.
  • cout ltlt My last initial is ltlt initial ltlt
    \n
  • cout ltlt The average I paid is ltlt averagePaid
    ltlt \n
  • return 0

23
Multiple Declarations
  • In the previous example we had
  • double totalPaid
  • double numberPaid
  • double averagePaid
  • This can be done in one statement like this
  • double totalPaid, numberPaid, averagePaid
  • This is very useful when creating several
    variables of the same type.

24
Initialization
  • Variables can be assigned a value as they are
    declared. This is called initializing.
  • include ltiostream.hgt
  • using namespace std
  • int main()
  • double totalPaid 58.24
  • double numberPaid 14.0
  • double averagePaid (totalPaid / numberPaid)
  • char initial D
  • cout ltlt My first initial is ltlt initial ltlt
    \n
  • initial C
  • cout ltlt My last initial is ltlt initial ltlt
    \n
  • cout ltlt The average I paid is ltlt averagePaid
    ltlt \n
  • return 0
Write a Comment
User Comments (0)
About PowerShow.com