Data Types - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Data Types

Description:

Review: Class Exercise 1. Write a short C program that will print your full name to the screen. ... file needs to be included in the program. Class Exercise 2 ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 26
Provided by: bridges
Category:
Tags: data | types

less

Transcript and Presenter's Notes

Title: Data Types


1
Data Types
  • Reading Chapter 2

2
Review Class Exercise 1
  • Write a short C program that will print your
    full name to the screen.

3
Data Types
  • A program operates on data
  • Stored internally in memory and externally on
    disk
  • Each data item must be of a specific type
  • Type determines how the data is represented
  • Data Types
  • Standard (Built-In) Data Types
  • Derived Data Types
  • User Defined Data Types

4
Variables
  • Represent a location or locations in memory
  • Values are placed there to be used in a program
  • Values can change, thus are variable
  • Variable names are used to reference

5
Variable Declarations
  • All variables must be declared before being used
  • datatype identifier initialization
  • int quizScore
  • float wages 6.5
  • A declaration does a number of things
  • Tells the compiler to allocate space
  • Associates the variable name with the allocated
    memory space
  • May initialize it, giving it an initial value

6
Variable Declarations (Cont ..)
16 Bits
  • Examples
  • short num
  • short sum 120
  • int product

product
num
16 bits
32 bits
120
sum
7
Variable Names
  • Length depends on compiler (at least 31)
  • Characters allowed
  • Alphabets A to Z, a to z
  • Numeric 0 to 9
  • The underscore character _
  • Must begin with alpha or underscore
  • C is case-sensitive
  • Total is different from total and toTal

8
Variable Names (Cont ..)
  • Reserved words can not be used
  • A list of reserved words is given in Appendix A
    (page 1443)
  • Avoid naming variables with the same names as
    functions

9
Variable Names (Cont ..)
  • int xyz

float _x1y
starts with digit
short int 1sum
integer not a reserved word
int integer
"for" is a reserved word
double for
float SUM
int s123
10
Variable Names (Cont ..)
  • Use meaningful and descriptive variable names
  • int s
  • int sum

Avoid the first one and try to use the second one
if your variable is supposed to store the value
of some sum
11
Variable Names (Cont ..)
  • Begin a variable name with a lowercase letter
  • Words after the first word in a variable name are
    capitalized

Good Names int test1, test2, test3 float
averageTestScore 0 int sumOfTests 0
Bad Names (but legal) int NumStudents float c,
d, e int average_grade
12
Standard Data Types
  • Integral types
  • Integers int,short,long
  • Character char
  • Boolean bool
  • Floating point types
  • float,double,long double

13
Integers
  • Three types based on the size, the size can be
    system specific
  • int 32 bits
  • short int 16 bits
  • long int 32 bits
  • Each can be
  • signed (default)
  • unsigned

14
Integers (Cont ..)
  • signed allows the value to be negative
  • unsigned allows only positive (and zero)
  • The range is approximately double on the positive
    side

15
Summary of Integers
16
Character Data Type
  • Eight bits long
  • Declared with char
  • Used to store individual characters, but really
    numbers (integers)
  • Value of ASCII character set, interpreted as
    integer, provides ordering (collating sequence).

17
Character Data Type (Cont ..)
  • Some valid declarations
  • char grade
  • char topGrade A,
  • bottomGrade F
  • char topGrade 65,
  • bottomGrade 70

18
Floating Point Data Types
  • Stored in scientific notation
  • Example
  • 0.0235 2.35 10?2
  • Expressed in standard or scientific notation
  • Can use 7146.0 or 7.146e3

Mantissa
Base
Exponent
19
Floating Point Types (Cont ..)
  • Three flavors
  • float 32 bits
  • double 64 bits
  • long double 64 bits
  • Some valid declarations
  • float wageRate 12.75
  • double area, volume
  • long double humongous

20
Floating Point Types (Cont ..)
  • A value with a decimal point stored as double
  • Type can also be assigned explicitly
  • Use F or f for a float
  • 3.806e-3f or 0.003806f
  • 3.806e-3F or 0.003806F
  • Use L or l for a long double
  • 7.16e-4l or 0.000716l
  • 7.16e-4L or 0.000716L

21
Comparison offloat and double
22
The string type
  • The char type can represent a single character.
  • The string type can represent a sequence of
    characters.
  • The string type is not a built-in type, rather is
    a user-defined type found in C standard
    library.
  • Example
  • string firstName
  • string lastName "Hossain"
  • In order to use the string type, the ltstringgt
    header file needs to be included in the program.

23
Class Exercise 2
  • Write a short C program that declares an int,
    double, char, and string.
  • Store the last digit of your phone number in the
    int, your height to the nearest .5 inch in the
    double, your first initial in the char, and your
    full name in the string.
  • Print out all information stored to the screen

24
Constants
  • Values can not be changed
  • Can be defined/declared in two ways
  • Using the define directive
  • define PI 3.14159
  • Declaring it with the reserved word const. In
    that case, it has to be initialized during
    declaration
  • const int MAX_GRADE 100
  • const double PI 3.14

25
Constants (Cont ..)
  • As with variables, use meaningful and descriptive
    names for constants
  • Use all uppercase letters for the name of each
    constant
  • Separate multiple words with underscore

Bad Names (but legal) const int numOfStudents
60 const float c define pi 3.14
Good Names const int NUM_OF_STUDENTS
60 define MAX_GRADE 100
Write a Comment
User Comments (0)
About PowerShow.com