Bits, bytes and memory - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Bits, bytes and memory

Description:

Data is stored by setting these bits to 1s and 0s. Each cell has an address. ... A variable is a reserved location in memory that. has a name ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 13
Provided by: vdou8
Category:
Tags: address | bits | by | bytes | locate | memory | name

less

Transcript and Presenter's Notes

Title: Bits, bytes and memory


1
Bits, bytes and memory
  • Your computer's memory can be seen as a sequence
    of cells.
  • Each cell is 8 bits (one byte) large. Data is
    stored by setting these bits to 1s and 0s.
  • Each cell has an address. You don't need to know
    (or care to know) what this address is. Your
    system uses the address to locate the data stored
    there.

0
1
2
Address ? ? ?
storedbytes
3
4
5
2
Variables
  • We need to be able to store data in memory,
    during the execution of our program.
  • We also need to be able to access and even modify
    this data.
  • Solution variables
  • A variable is a reserved location in memory that
  • has a name
  • has an associated type (for example, integer)
  • holds data which can be modified

3
Variables
  • In order to use a variable in our program we must
    first declare it.
  • HOW?
  • A declaration statement has the format type
    variable_name
  • type what kind of data will be stored in that
    location (integer? character? floating point?)
  • variable_name what is the name of the variable?
  • semi-colon this is a statement!
  • WHERE?
  • At the beginning of a function, right after the
    opening brace.

4
Variable names
  • In C, variable names are built from
  • the letters of the alphabet
  • the digits 0 through 9
  • the underscore
  • A variable name must start with a letter or an
    underscore
  • A variable name must not be the same as a
    reserved word used by the C language.
  • Only the first 31 characters of a variable name
    are significant. The rest are ignored.

5
Variable names
  • Selecting good variable names is important for
    program readability.
  • A variable name must be descriptive of the data
    that will be stored in the variable.
  • It must not be too long.
  • It must not be a single character (there is one
    allowed exception to this rule, which we will
    talk about later)

6
Variable names
  • Good, legal variable names
  • totalArea temp_in_F
  • counter1 isEmpty
  • num_trees pNuts
  • Legal, but bad variable names
  • l11 (is it L11, L1L, LL1, or LLL?)
  • x (what does it mean?)
  • maximum_number_of_students_in_my_class
  • a23456789_123456789_123456789_12345678
  • Illegal variable names
  • product main not-this
  • total 3rd

7
Variable types
  • We've already seen a type, int, which is used for
    integers (more types coming up...)
  • Here's a sample program that declares a variable
    that will hold an integer

/ program that demonstrates a variable
declaration. / include ltstdio.hgt int main ()
int num_students return 0
8
Variable values
  • After a variable has been declared, its memory
    location contains randomly set bits. In other
    words, it does not contain valid data.
  • The value stored in a variable must be
    initialized before we can use it in any
    computations.
  • There are two ways to initialize a variable
  • by reading its value from the keyboard using
    scanf
  • by assigning a value using an assignment
    statement (more on that later)

9
Keyboard input scanf()
  • scanf() will scan formatted input from the
    keyboard.
  • It uses special format specifiers that specify
    the type of the variable whose value is to be
    read from the keyboard.
  • To read an integerint num_studentsscanf("d",
    num_students)

Place value into this variable
Formal specifier for read an integer value
VERY IMPORTANT special symbol
10
Printing variable values
  • printf() will print formatted output to the
    screen.
  • To print a message printf("This is a
    message\n")
  • How do we print the value of a variable?
  • Answer Use format specifiers depending on the
    type of the variable (similar to scanf)

11
Printing variable values
  • To print an integer

printf("The temperature is d degrees.",
degreesF)
Specifier for print an integer value
Read value from this variable
IMPORTANT degreesF MUST be initialized so that
it contains a valid value.
12
Example
/ sample program that demonstrates declaring a
variable, assigning a value to it using scanf
and printing that value using printf
/ include ltstdio.hgt int main () int
num_students printf("How many students are
there? ") scanf("d", num_students)
printf("There are d students.\n") return 0
Write a Comment
User Comments (0)
About PowerShow.com