160 likes | 316 Views
Variables. have a type have an address (in memory) have a value have a name (for now). Variables - name. must start with a-z, A-Z ( _ allowed, but not recommended) other characters may be a-z, A-Z, 0-9, _
E N D
Variables • have a type • have an address (in memory) • have a value • have a name (for now)
Variables - name • must start with a-z, A-Z ( _ allowed, but not recommended) • other characters may be a-z, A-Z, 0-9, _ • upper case/lower case are not equal (i.e. ECE, ece, Ece, EcE, eCe would be five different variables) • max length is system dependent (usually at least 32) • unique-ness length usually at least 32
Variables - legal names • GrossPay • Carpet_Price • carpet_price • a_very_long_variable_name • i • ______strange___one_____ • _ (not recommended)
Variables - legal names(but not recommended) • l (that's lower case L) • O (that's capitol O) • l1 (that's lower case L, and digit one) • O0Oll11 (oh,zero,oh,el,el,one,one) • _var (many system variables begin with _ )
Variables - declaring var name memory loc main(){ float hours; float payrate; float grosspay; int j; hours ? 4278 payrate ? 427C 4280 grosspay ? j ? 4284
Variables - assigning varname = expression Declared variable single variable on left side of = expression any legal expression In general, the type of the declared variable (int, float, etc) and the resultant type of the expression should be the same. Note this is not a requirement...BUT, you should be aware that you are changing types across the equals and understand the ramifications.
Variables - declaring var name memory loc main(){ float hours; float payrate; float grosspay; int j; hours = 40.0; hours 40.0 4278 payrate ? 427C 4280 grosspay ? j ? 4284
Variables - declaring var name memory loc main(){ float hours; float payrate; float grosspay; int j; hours = 40.0; payrate = 20.00; hours 40.0 4278 payrate 20.0 427C 4280 grosspay ? j ? 4284
Variables - declaring var name memory loc main(){ float hours; float payrate; float grosspay; int j; hours = 40.0; payrate = 20.00; grosspay = hours * payrate hours 40.0 4278 payrate 20.0 427C 4280 grosspay 800.00 j ? 4284 note: referencing a variable only "reads" it (non-destructive). Assigning to a variable overwrites whatever was there (destructive).
Variables - declaring var name memory loc main(){ float hours; float payrate; float grosspay; int j; hours = 40.0; payrate = 20.00; grosspay = hours * payrate j = 5; hours 40.0 4278 payrate 20.0 427C 4280 grosspay 800.00 j 5 4284 note: referencing a variable only "reads" it (non-destructive). Assigning to a variable overwrites whatever was there (destructive).
Variables - declaring var name memory loc main(){ float hours; float payrate; float grosspay; int j; hours = 40.0; payrate = 20.00; grosspay = hours * payrate j = 5; j = j + 1; hours 40.0 4278 payrate 20.0 427C 4280 grosspay 800.00 j 5 6 4284 note: referencing a variable only "reads" it (non-destructive). Assigning to a variable overwrites whatever was there (destructive).
scanf() function • Used to get input from user • returns number of items assigned • first argument if format specifiers • remaining arguments are addresses of variables
scanf() function Documentation info: int scanf(const char *format [,argument] ...) format - is format specifiers similar to printf() specifiers arguments - are ADDRESSES of where to store what the user enters
scanf() function int hours;float rate; scanf("%d %f",&hours,&rate); If user types: 34 5.7 hours ? 1284 rate ? 1288 hours 34 1284 rate 5.7 1288
scanf() function #include <stdio.h> int main(){ int hours; float rate; float grosspay; printf("Enter hours: "); scanf("%d",&hours); printf("Enter pay rate: "); scanf("%f",&rate); grosspay = hours * rate; printf("You earned $%8.2f\n",grosspay);}
scanf() function - Payroll Ver 2 #include <stdio.h> int main(){ double hours; double rate; double grosspay; printf("Enter hours: "); scanf("%lf",&hours); printf("Enter pay rate: "); scanf("%lf",&rate); grosspay = hours * rate; printf("You earned $%8.2lf\n",grosspay);} could use %8.2f, but really should use lf