Variables in C CMSC 104 Section 4 Richard
Variables in C CMSC 104, Section 4 Richard Chang 1
What Are Variables in C? l Variables in C are similar to variables in algebra. x=a+b z + 2 = 3(y - 5) l However, variables in C represent storage locations. 2
Rules for Variable Names l C variables can have multiple characters l Legal variable names in C l l 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) 3
Reserved Words (Keywords) in C l l l l auto case const default double enum float goto break char continue do else extern for if int register short signed sizeof struct typedef unsigned volatile long return static switch union void while 4
Naming Conventions l Begin variable names with lowercase letters l Use meaningful identifiers (names) l Separate “words” within identifiers with underscores or mixed upper and lower case. l Examples: surface. Area surface_area l Be consistent! 5
Case Sensitivity l It matters whether an identifier, such as a variable name, is uppercase or lowercase. l Example: area AREA Ar. Ea are all seen as different variables by the compiler. l Pick one. 6
Legal Identifiers vs. Naming Conventions l Legal identifiers refer to the restrictions C places on naming identifiers. l Naming conventions refer to the standards you must follow for this course. Just because you can, does not make it a good idea! 7
Which Are Legal Identifiers? AREA lucky*** Last-Chance x_yt 3 num$ area_under_the_curve 3 D num 45 #values pi %done 8
Which are good ideas? Area Last_Chance x_yt 3 finaltotal area_under_the_curve person 1 values pi num. Children 9
Declaring Variables l l l 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 ; 10
More About Variables C has three basic predefined data types: l Integers (whole numbers) l l Floating point (real numbers) l l int, long int, short int, unsigned int float, double Characters l char 11
Using Variables: Initialization l Variables may be be given initial values, or initialized, when declared. Examples: length 7 int length = 7 ; diameter double diameter = 5. 9 ; char initial = ‘A’ ; 5. 9 initial ‘A’ 12
Using Variables: Assignment l l l Values are assigned to variables using the assignment operator = This operator does not denote equality Examples: diameter = 5. 9 ; l area = length * width ; Only single variables may appear on the left side of the assignment operator. 13
Example: Declarations and Assignments inches 1. #include <stdio. h> 2. int main( ) { int inches, feet, fathoms ; 3. 4. 5. 6. 7. fathoms = 7 ; feet = 6 * fathoms ; inches = 12 * feet ; garbage feet garbage fathoms 7 feet 42 inches 504 14
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) ; 8. 9. 10. 11. 12. 13. } return 0 ; 15
- Slides: 15