Download presentation
Presentation is loading. Please wait.
Published byRonald Payne Modified over 6 years ago
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
ECE 160 - Introduction to Computer Engineering I
02/09/2005 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). (c) 2005, P. H. Viall
12
ECE 160 - Introduction to Computer Engineering I
02/09/2005 scanf() function Used to get input from user returns number of items assigned first argument if format specifiers remaining arguments are addresses of variables (c) 2005, P. H. Viall
13
ECE 160 - Introduction to Computer Engineering I
02/09/2005 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 (c) 2005, P. H. Viall
14
ECE 160 - Introduction to Computer Engineering I
02/09/2005 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 (c) 2005, P. H. Viall
15
ECE 160 - Introduction to Computer Engineering I
02/09/2005 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); } (c) 2005, P. H. Viall
16
scanf() function - Payroll Ver 2
ECE Introduction to Computer Engineering I 02/09/2005 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 (c) 2005, P. H. Viall
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.