Variables in C Declaring Naming and Using Variables
Variables in C Declaring , Naming, and Using Variables CMSC 104 1
Using Variables You may declare variables in C. l The declaration includes the data type you need. l Examples of variable declarations: l int meatballs ; float area ; CMSC 104 2
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 3
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 CMSC 104 4
Naming Conventions Begin variable names with lowercase letters l Use meaningful identifiers l Separate “words” within identifiers with underscores or mixed upper and lower case. l Example: surface. Area surface_Area or surface_area CMSC 104 l Be consistent !! l 5
Naming Conventions (continued) l Use all uppercase for symbolic constants ( #define ) Example: PI (#define PI 3. 14159 ) l Function names follow the same rules as variables CMSC 104 6
Case Sensitive l C is case sensitive o It matters whether something is upper or lower case o Example: area is different than Area which is different than AREA CMSC 104 7
More About Variables C has 3 basic predefined data types l Integers l o int, long int, short int, unsigned int l Floating point numbers o float, double l Characters o char CMSC 104 8
Initializing Variables l Variables may be initialized o int x = 7; o float y = 5. 9; o char c = ‘A’; l CMSC 104 Do not “hide” the initialization o put initialized variables on a separate line o a comment is probably a good idea o int y = 6; /* feet per fathom */ o NOT int x, y = 6, z; 9
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 10
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 11
Declarations and assignments wreck. c #include <stdio. h> inches main ( ) { int inches, feet, fathoms ; feet fathoms = 7 ; feet = 6 * fathoms ; inches = 12 * feet ; fathoms 7 feet 42 inches 504 l l l CMSC 104 } 12
wreck. c (cont’d) main ( ) { l l l } 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 CMSC 104 that location. 13
Floating point numbers Are numbers that 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 14
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); } CMSC 104 15
- Slides: 15