1 / 11

Variables and Declarations

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.

robinhickey
Download Presentation

Variables and Declarations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 • Begin with letter or underscore • Contain letters, underscores and digits • 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 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.

  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

More Related