Download presentation
Presentation is loading. Please wait.
1
Variables and Declarations 02/06/15
2
Objectives Create and use variables in a program. Use correct identifiers for the variables' names.
3
Identifiers Names of things Way to name a variable Rules 1. Begin with letter or underscore 2. Contain letters, underscores and digits 3. Cannot be a keyword Examples: num1 Ghost51 mill_house _river_flow TAX
4
Keywords Words with special meaning in C Cannot be used as variable names float, return See p.44, Table 2.1
5
Invalid Identifiers Why are these invalid? 12noon potato# green-tea double
6
Variables Place to store a value in memory Declare: float cost; Change Contents cost = 15.00; cost = 10.00; Can only store one number at a time.
7
Variable Declarations Variable type Followed by identifier that is the variable's name. int counter; float x; char answer;
8
Example Program with Variables lincoln.c
9
Initialization Give variable initial value Don't know what is stored there otherwise. lbsFood.c Count is initialized to 3.
10
Write a Program Look at exercise #4, p. 89 The distance that light travels in one year is called a light year. Given that light travels at a speed of 3 X 10 8 meters in one second, determine the distance of a light year. The distance formula is d = rt. Output the answer in kilometers. I will submit the program.
11
Assignments On your own: p. 52 #1, p. 65 #1,4, p. 72 #1, #2a,c,d,f,e, #3a,c,d,f,h, p.84 #4 a,b, #7, p. 113#1a,c, 2 a,c,e,g, p.128 #2 a,b,c Put programs for the problems in a directory and submit them together as program 3: P. 89 #6
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.