1 / 16

Variables

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, _

macey-vega
Télécharger la présentation

Variables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Variables • have a type • have an address (in memory) • have a value • have a name (for now)

  2. 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

  3. Variables - legal names • GrossPay • Carpet_Price • carpet_price • a_very_long_variable_name • i • ______strange___one_____ • _ (not recommended)

  4. 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 _ )

  5. Variables - declaring var name memory loc main(){ float hours; float payrate; float grosspay; int j; hours ? 4278 payrate ? 427C 4280 grosspay ? j ? 4284

  6. 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.

  7. 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

  8. 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

  9. 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).

  10. 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).

  11. 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).

  12. scanf() function • Used to get input from user • returns number of items assigned • first argument if format specifiers • remaining arguments are addresses of variables

  13. 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

  14. 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

  15. 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);}

  16. 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

More Related