1 / 15

Variables in C

Variables in C. CMSC 104, Section 4 Richard Chang. What Are Variables in C?. Variables in C are similar to variables in algebra. x = a + b z + 2 = 3(y - 5) However, variables in C represent storage locations. Rules for Variable Names. C variables can have multiple characters

Télécharger la présentation

Variables in C

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 in C CMSC 104, Section 4 Richard Chang

  2. What Are Variables in C? • Variables in C are similar to variables in algebra. x = a + b z + 2 = 3(y - 5) • However, variables in C represent storage locations.

  3. Rules for Variable Names • C variables can have multiple characters • Legal variable names in C • May only consist of letters, digits, and underscores • May be as long as you like, but only the first 31 characters are significant • May not begin with a number • May not be a C reserved word (keyword)

  4. Reserved Words (Keywords) in C • auto break • case char • const continue • default do • double else • enum extern • float for • goto if • int long • register return • short signed • sizeof static • struct switch • typedef union • unsigned void • volatile while

  5. Naming Conventions • Begin variable names with lowercase letters • Use meaningful identifiers (names) • Separate “words” within identifiers with underscores or mixed upper and lower case. • Examples: surfaceArea surface_Area surface_area • Be consistent!

  6. Case Sensitivity • It matters whether an identifier, such as a variable name, is uppercase or lowercase. • Example: area Area AREA ArEa are all seen as different variables by the compiler. • Pick one.

  7. Legal Identifiers vs. Naming Conventions • Legalidentifiers refer to the restrictions C places on naming identifiers. • Naming conventions refer to the standards you must follow for this course. Just because you can, does not make it a good idea!

  8. Which Are Legal Identifiers? • AREA 3D • lucky*** num45 • Last-Chance #values • x_yt3 pi • num$ %done • area_under_the_curve

  9. Which are good ideas? • Area person1 • Last_Chance values • x_yt3 pi • finaltotal numChildren • area_under_the_curve

  10. Declaring Variables • Before using a variable, you must give the compiler some information about the variable; i.e., you must declare it. • The declaration statement includes the data type of the variable. • Examples of variable declarations: • int meatballs ; • double area ;

  11. More About Variables C has three basic predefined data types: • Integers (whole numbers) • int, long int, short int, unsigned int • Floating point (real numbers) • float, double • Characters • char

  12. Using Variables: Initialization • Variables may be be given initial values, or initialized, when declared. Examples: int length = 7 ; double diameter = 5.9 ; char initial = ‘A’ ; length 7 diameter 5.9 initial ‘A’

  13. Using Variables: Assignment • Values are assigned to variables using the assignment operator = • This operator does not denote equality • Examples: diameter = 5.9 ; area = length * width ; • Only single variables may appear on the left side of the assignment operator.

  14. Example: Declarations and Assignments • #include <stdio.h> • int main( ) • { • int inches, feet, fathoms ; • fathoms = 7 ; • feet = 6 * fathoms ; • inches = 12 * feet ; inches garbage feet garbage fathoms garbage fathoms 7 feet 42 inches 504

  15. Example: Declarations and Assignments (cont’d) • printf (“Its depth at sea: \n”) ; • printf (“ %d fathoms \n”, fathoms) ; • printf (“ %d feet \n”, feet) ; • printf (“ %d inches \n”, inches) ; • return 0 ; • }

More Related