Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMSC 1041 Variables in C Declaring, Naming, Using Variables.

Similar presentations


Presentation on theme: "CMSC 1041 Variables in C Declaring, Naming, Using Variables."— Presentation transcript:

1 CMSC 1041 Variables in C Declaring, Naming, Using Variables

2 CMSC 1042 What is a “Variable” in C? l A variable is a storage location in memory. l It is not: oa number oa value l “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.”

3 CMSC 1043 Using Variables l You must declare variables in C so the compiler can reserve space. l The declaration includes the type of the variable. l Examples of variable declarations: int meatballs ; /* 32 bit fixed */ float area ; /* decimal pt */ long salary;/* 64 bit fixed */ (“fixed” numbers have no decimal point.)

4 CMSC 1044 Declaring Variables l When we declare a variable: o space in memory is set aside to hold that data type oThat space is associated with the variable name oVisualization of the declaration int meatballs ; meatballs FE07

5 CMSC 1045 Declaring Variables l Compiler Table: Name Spade Mango Quinn Shannon Oteri Update Address 0x44A3 0x432D 0x432E 0X434F 0x433A 0x4217 Type int float long char int char Location b 3.1415926 81456654 42 a 98

6 CMSC 1046 Legal Variable Names l Variable names in C must be valid identifiers oConsists of letters, digits and underscores. oMay be as long as you like, but only the first 31 characters are significant. oMay NOT begin with a number oMay not be a C keyword l Restriction is to simplify compiler design

7 CMSC 1047 Naming Conventions l Variable names are for people to read - make them understandable! oPick meaningful names: “taxableIncome” not “I” “numberOfDependents” not “n” or “d” or “nd” “altitudeInMeters” not “altitude” Use single-letter variables (I,j,k,m,n,x,y, etc) only for temporary “throwaway” results y = yearsOfExperience + yearsOfSchool; printf(“Total equivalent years equals: “ %d, y); l Begin variable names with lowercase letters l Separate “words” within identifiers with underscores or mixed upper and lower case. l Example: surfaceArea surface_Area or surface_area l Be consistent !!

8 CMSC 1048 Naming Conventions (Continued) l Begin variable names with lowercase letters: oheight oseparation l Separate “words” within identifiers with underscores or mixed upper and lower case. opatientHeight ochannelSeparationInDb osurfaceArea, surface_Area, surface_area l Be consistent !! (or have a reason for inconsistency)

9 CMSC 1049 Naming Conventions (continued) l Use all uppercase for symbolic constants #define PI 3.14159 #define FUN_COURSE CMSC104 l Use quotes to enclose spaces or punctuation #define PGM_ID “CMSC 104 Prj. 1: P.C.Olsen 701” #define IDENT “$$NITF v0.9 991013 pcolsen@draper.com$$” l Function names follow the same rules as variables

10 CMSC 10410 Naming Conventions (continued) l Function names follow the same rules as variables: o“calculateArea(float radius)” o“degreesKelvin(float degreesRoentgen)” o“predictVelocity(int time, float acceleration) l Compare to ocA(float r) oK(float R) opredV(int t, float a)

11 CMSC 10411 Case Sensitive l C is case-sensitive Lowercase letters and uppercase letters are different: area is different than Area which is different than AREA

12 CMSC 10412 Predefined Types of Variables l Integers oint, long int, short int l Floating point ofloat, double l Characters ochar

13 CMSC 10413 Initializing Variables l Variables MUST be initialized before they are used: owhen they are declared: int x = 7;/* default hat size */ float y = 5.9;/* standard picnic table radius */ char c = ‘A’;/* expected grade */ obefore first use: x = 7; /* default hat size */ y = 5.9; /* standard picnic table radius */ c = ‘A’ /* expected grade */

14 CMSC 10414 Initializing Variables (continued) l Do not “hide” the initialization oput initialized variables on a separate line oalways comment oExamples: int y = 6; /* feet per fathom */ float initialVelocity = 0.0; /* in m/s before start */ oNOT int x, y = 6, z;

15 CMSC 10415 Keywords in C l auto break l case char l const continue l default do l double else l enum extern l float for l goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while

16 CMSC 10416 Which Are Legal Identifiers ? AREA area_under_the_curve 3D num45 Last-Chance #values x_yt3 pi num$ %done lucky***

17 CMSC 10417 Declarations and assignments wreck.c #include main ( ) { int inches, feet, fathoms ; fathoms = 7 ; feet = 6 * fathoms ; inches = 12 * feet ; } inches feet fathoms 7 feet 42 504 inches

18 CMSC 10418 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.

19 CMSC 10419 Floating point numbers l 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 ;

20 CMSC 10420 Floating point version of wreck.c (works for any depth shipwreck) #include 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.)


Download ppt "CMSC 1041 Variables in C Declaring, Naming, Using Variables."

Similar presentations


Ads by Google