Variables in C Declaring Naming Using Variables CMSC
Variables in C Declaring , Naming, Using Variables CMSC 104 1
What is a “Variable” in C? A variable is a storage location in memory. l It is not: l o a number o a value l CMSC 104 “X = X+1” means “Add 1 to the contents of X and then store the result in X” not “X is a number such that X = X + 1. ” 2
Using Variables l l l You must declare variables in C so the compiler can reserve space. The declaration includes the type of the variable. Examples of variable declarations: int meatballs ; /* 32 bit fixed */ float area ; /* decimal pt */ long salary; /* 64 bit fixed */ (“fixed” numbers have no decimal point. ) CMSC 104 3
Declaring Variables l When we declare a variable: o space in memory is set aside to hold that data type o That space is associated with the variable name o Visualization of the declaration int meatballs ; meatballs CMSC 104 FE 07 4
Declaring Variables l CMSC 104 Compiler Table: Name Type Address Location Spade int 0 x 44 A 3 b Quinn float 0 x 432 E 81456654 Mango long 0 x 432 D 3. 1415926 Shannon char 0 X 434 F 42 Oteri int 0 x 433 A a Update char 0 x 4217 98 5
Legal Variable Names l Variable names in C must be valid identifiers o Consists of letters, digits and underscores. o May be as long as you like, but only the first 31 characters are significant. o May NOT begin with a number o May not be a C keyword l CMSC 104 Restriction is to simplify compiler design 6
Naming Conventions l Variable names are for people to read make them understandable! o Pick meaningful names: �“taxable. Income” not “I” �“number. Of. Dependents” not “n” or “d” or “nd” �“altitude. In. Meters” not “altitude” �Use single-letter variables (I, j, k, m, n, x, y, etc) only for temporary “throwaway” results y = years. Of. Experience + years. Of. School; printf(“Total equivalent years equals: “ %d, y); CMSC 104 7
Naming Conventions (Continued) l Begin variable names with lowercase letters: o height o separation l Separate “words” within identifiers with underscores or mixed upper and lower case. o patient. Height o channel. Separation. In. Db o surface. Area, surface_area l CMSC 104 Be consistent !! (or have a reason for inconsistency) 8
Naming Conventions (continued) l Use all uppercase for symbolic constants #define PI 3. 14159 #define FUN_COURSE CMSC 104 l Use quotes to enclose spaces or punctuation #define PGM_ID “CMSC 104 Prj. 1: P. C. Olsen 701” #define IDENT “$$NITF v 0. 9 991013 pcolsen@draper. com$$” l CMSC 104 Function names follow the same rules as variables 9
Naming Conventions (continued) l Function names follow the same rules as variables: o “calculate. Area(float radius)” o “degrees. Kelvin(float degrees. Roentgen)” o “predict. Velocity(int time, float acceleration) l Compare to o c. A(float r) o K(float R) o pred. V(int t, float a) CMSC 104 10
Case Sensitive l C is case-sensitive Lowercase letters and uppercase letters are different: area is different than Area which is different than AREA CMSC 104 11
Predefined Types of Variables l Integers o int, long int, short int l Floating point o float, double l Characters o char CMSC 104 12
Initializing Variables l Variables MUST be initialized before they are used: o when they are declared: �int x = 7; /* default hat size */ �float y = 5. 9; /* standard picnic table radius */ �char c = ‘A’; /* expected grade */ o before first use: CMSC 104 �x = 7; /* default hat size */ �y = 5. 9; /* standard picnic table radius */ �c = ‘A’ /* expected grade */ 13
Initializing Variables (continued) l Do not “hide” the initialization o put initialized variables on a separate line o always comment o Examples: �int y = 6; /* feet per fathom */ �float initial. Velocity = 0. 0; /* in m/s before start */ o NOT �int x, y = 6, z; CMSC 104 14
Keywords in C l l l l CMSC 104 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 15
Which Are Legal Identifiers ? AREA 3 D Last-Chance x_yt 3 num$ lucky*** CMSC 104 area_under_the_curve num 45 #values pi %done 16
Declarations and assignments wreck. c #include <stdio. h> inches main ( ) { int inches, feet, fathoms ; feet } CMSC 104 fathoms = 7 ; feet = 6 * fathoms ; inches = 12 * feet ; fathoms 7 feet 42 inches 504 17
wreck. c (cont’d) main ( ) { } printf (“Its depth at sea: n”) ; printf (“ %d fathoms n”, fathoms) ; printf (“ %d feet n”, feet); printf (“ %d inches n”, inches); %d is a place holder - indicates that the value of the integer variable is to be printed in decimal form (rather than binary or hex) at that location. CMSC 104 18
Floating point numbers Can contain decimal points. l What if the depth were really 5. 75 fathoms ? . . . Our program, as it is, couldn’t handle it. l We can declare floating point variables like this : float fathoms ; float feet ; l CMSC 104 19
Floating point version of wreck. c (works for any depth shipwreck) #include <stdio. h> main ( ) { float fathoms, feet; printf (“Enter the depth in fathoms : ”); scanf (“%f”, &fathoms); feet = 6. 0 * fathoms; printf (“She’s %f feet down. n”, feet); } (“%f” syntax does the same thing for floats as %d does for ints --- prints them out in decimal form. ) CMSC 104 20
- Slides: 20