Variable Names - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Variable Names

Description:

Skimmed the rest of Chapter 2, and. Visited a computer lab to try PA ... Hydra.c -- a program with variables. #include stdio.h main() int heads; int eyes; ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 22
Provided by: jack195
Category:
Tags: hydra | names | variable

less

Transcript and Presenter's Notes

Title: Variable Names


1
CS 110 Lecture 03Literals Variables
Jack Tumblinjet_at_cs.northwestern.edu
  • I hope you have already
  • Read through Chapter 2.4,
  • Skimmed the rest of Chapter 2, and
  • Visited a computer lab to try PA-0.

2
Recall That
  • C ignores / comments / and blank space //
    (C comments also OK, but NOT part of C!)
  • A statement is one complete rule, andC
    statements always end with semicolon
  • A block of statements is always enclosed within
    braces (curly brackets)
  • A function is a named block of statements
  • (more about functions later)
  • All C programs have a main( ) function
  • computing always begins and ends in main( )
  • Bits, Bytes Memory is a numbered list of bytes

3
How C is Organized Nesting
  • Literals and Variables (with their data
    types)
  • Operators
  • Expressions
  • Statements
  • Functions
  • Libraries
  • Programs
  • Systems

4
How C is Organized Nesting
  • Literals and Variables (with their data
    types)
  • Operators
  • Expressions
  • Statements
  • Functions
  • Libraries
  • Programs
  • Systems

5
Know these Definitions
  • LiteralA fixed number(data) written in a
    progm.
  • Variable A named, reserved piece of memory
  • Operator A non-numeric calculator button
  • Expression The smallest grain of computing
  • Statement Zero or more expressions ended by
  • Function A named block of statements
  • Library A collection of re-usable functions in a
    file
  • Program a main() function any functions it
    calls.
  • SystemA collection of program(s) that work
    together

6
Variables and Literals
  • Same idea used in algebra y 3x 5 x and
    y are the variables 3,5 are the constants
    oops! No! the literals (CS jargon
    disastermore about this on Friday)
  • But Generalized a variable is a placeholder
    for one piece of data numbers, letters, sets,
    arrays, etc. Its value may be unknown until the
    program is actually running (CS jargon at
    run-time).
  • Inside the computer a variable is a reserved
    location in memory, and holds data that are
    allowed to change.

7
Variables
  • ALWAYS declare a variable before use!
  • forgot? error message, or ugly weirdness
  • ALWAYS set a variables value before use!
  • forgot? starting value will be unpredictable
  • What does declaring a variable do?
  • reserve a memory location that will store the
    value of the variable (jargon assign an
    address)
  • assign the variable name to that location.

8
/ Hydr
a.c -- a program with variables.
/include lt stdio.h
gtmain()int heads int eyes heads
3 / assign a value / eyes heads
2 / compute a value / printf(It has d
heads and d eyes! \n", heads, eyes)
declare each variable write a statement that
to set its type and name
Output gt It has 3 heads have 6 eyes!
9
/ Hydr
a.c -- a program with variables.
/include lt stdio.h
gtmain()int heads int eyes heads
3 / assign a value / eyes heads
2 / compute a value / printf(It has d
heads and d eyes! \n", heads, eyes)
After that, set values of variables
Output gt It has 3 heads have 6 eyes!
10
How to declare Variables
  • Make declaration statement (always ends with
    )
  • The format data_type var, var,
  • Example int counter1, counter2
  • Where? Declare variables at the start of a
    function, right after the opening brace


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

12
Variable Names
  • Good, Valid variable names
  • totalArea _main
  • counter1 isEmpty
  • Count_trees pNuts
  • temp_in_F m_size
  • Invalid variable names
  • product main not-this
  • total 3rd
  • Legal, badly-chosen 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

13
Basic Types of Variables
  • There are 4 basic built-in data types in C

Type Integer Floating point Double Character
C keyword to use int float double char
14
Basic Types of Variables
  • int
  • Integer variables hold signed whole numbers (i.e.
    with no fractional parts). E.g. 10, -8439
  • In environments such as WinNT, integers typically
    store values in the range
  • From -2,147,483,648, or or about
  • to 2,147,483,648 or or about -

15
Basic Types of Variables
  • float and double
  • Used to store floating-point numbers, i.e.
    numbers with a fractional part. E.g. 12.475
  • double provides twice the precision of float
  • e.g. float can represent values up to about 2127
    whereas double can represent values up to about
    21023 .

16
Basic Types of Variables
  • float
  • size 4 bytes
  • approximate range () 10-44.85 to 1038.53
  • double
  • size 8 bytes
  • approximate range () 10-323.3 to 10 308.3

17
Basic Types of Variables
  • char
  • used to store single characters. E.g. a , R.
  • (Note that the value of the character is enclosed
    in single quotes).
  • Remember that the string in our earlier example
    was enclosed in double quotes. Therefore
  • characters single quotes
  • strings double quotes

18
More Types of Variables
  • How many bytes for each type?
  • A MESS! No standards in the language.
  • But nowadays its almost always
  • char 1 or 2 bytes (ASCII or Unicode)
  • int 4 bytes32 bits, signed.
  • float 4 bytes32 bits, ? (gtPentium? use double)
  • double 8 bytes64 bits.
  • Leftover keywords from the Dark Ages
  • short int, long int, unsigned int, signed int,
    unsigned char, signed char, double double.
  • Dont use in new programs (except by gunpoint)

19
Literals
  • Fixed values written into your program.
  • Same basic data types
  • int cnt ... for(cnt0 cntltmax cnt)
  • char lastKey ... lastKey y
  • float velocity ... velocity 0.0f
  • double atoms ... atoms 6.23E23
  • Can be Dangerous! Use Sparingly or not at
    all!(use define instead, coming next class)

20
Literals
  • Fixed values written into your program.
  • Details
  • 1-Character constants written with single
    quote keypressd y
  • Constant phrases, or strings (more about this
    later) written with double quotes printf(Huh.
    Dude typed a c!\n, keypressd)
  • Default decimal points make a double
    value.You can FORCE float data type like
    thisvelocity 3.141f

21
For Next Time
  • Be sure you know the definitions of the parts of
    C. Everybody uses these words. Dont ask me
    about the constant variable type thingie.
  • Read ahead. Skim chapter 3, so that my lectures
    will be boring.
  • Try things. Can you write a program that prints
    a 2D multiplication table?
Write a Comment
User Comments (0)
About PowerShow.com