Variables and Declarations
110 likes | 134 Vues
Learn how to create and use variables with correct identifiers in your C programs. Understand the rules for naming variables, declare variables with their types, and initialize them for effective programming. Practice with examples and assignments to enhance your skills in variable usage.
Variables and Declarations
E N D
Presentation Transcript
Variables and Declarations 02/06/15
Objectives • Create and use variables in a program. • Use correct identifiers for the variables' names.
Identifiers • Names of things • Way to name a variable • Rules • Begin with letter or underscore • Contain letters, underscores and digits • Cannot be a keyword • Examples: num1 Ghost51 mill_house _river_flow TAX
Keywords • Words with special meaning in C • Cannot be used as variable names • float, return • See p.44, Table 2.1
Invalid Identifiers • Why are these invalid? 12noon potato# green-tea double
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.
Variable Declarations • Variable type • Followed by identifier that is the variable's name. int counter; float x; char answer;
Example Program with Variables • lincoln.c
Initialization • Give variable initial value • Don't know what is stored there otherwise. • lbsFood.c • Count is initialized to 3.
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 108 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.
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