Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables & Constants Lesson 2 Outline

Similar presentations


Presentation on theme: "Variables & Constants Lesson 2 Outline"— Presentation transcript:

1 Variables & Constants Lesson 2 Outline
The Difference Between a Variable and a Constant Categories of Constants: Literal & Named Literal Constants Literal Constant Example Program Named Constants Name Constant Example Program The Value of a Named Constant Can’t Be Changed Why Literal Constants Are BAD BAD BAD 1997 Tax Program with Numeric Literal Constants 1999 Tax Program with Numeric Literal Constants Why Named Constants Are Good 1997 Tax Program with Named Constants 1999 Tax Program with Named Constants Output via printf Placeholders Placeholders for Various Data Types Mixing Literal Text and Variables’ Values #1 Mixing Literal Text and Variables’ Values #2 Placeholder & Variable in Same Statement Placeholder/Variable Same Statement: Example Input via scanf Input via scanf : Ampersand Before Variable Input via scanf Example Input via scanf Example’s Flowchart Reading Multiple Variables with a Single scanf Multiple Variables per scanf Example #1 Multiple Variables per scanf Example #2 printf vs scanf Programming Exercise Variables & Constants Lesson #2 CS1313 Spring 2006

2 Variables & Constants Lesson #2
In mathematics, a constant is a value that cannot change. In programming, a constant is like a variable, except that its value cannot change. Variables & Constants Lesson #2 CS1313 Spring 2006

3 The Difference Between a Variable and a Constant
a variable’s value can vary, but a constant’s value is constant. Variables & Constants Lesson #2 CS1313 Spring 2006

4 Categories of Constants: Literal & Named
There are two categories of constants: literal constants, whose values are expressed literally; named constants, which have names. Variables & Constants Lesson #2 CS1313 Spring 2006

5 Variables & Constants Lesson #2
Literal Constants A literal constant is a constant whose value is specified literally: int literal constants (e.g., 5, 0, -127, , ) float literal constants (e.g., 5.2, 0.0, , , e+08) char literal constants (e.g., ’A’, ’7’, ’?’) character string literal constants (e.g., "A", "Henry", "What’s it to ya?") Variables & Constants Lesson #2 CS1313 Spring 2006

6 Literal Constant Example Program
% cat tax1997_literal.c #include <stdio.h> int main () { /* main */ float income, tax; printf("I’m going to calculate the federal income\n"); printf(" tax on your 1997 income.\n"); printf("What was your 1997 income in dollars?\n"); scanf("%f", &income); tax = (income - ( )) * 0.15; printf("The 1997 federal income tax on $%2.2f\n", income); printf(" was $%2.2f.\n", tax); } /* main */ % gcc -o tax1997_literal tax1997_literal.c % tax1997_literal I’m going to calculate the federal income tax on your 1997 income. What was your 1997 income in dollars? 20000 The 1997 federal income tax on $ was $ Variables & Constants Lesson #2 CS1313 Spring 2006

7 Variables & Constants Lesson #2
Named Constants A named constant is a constant that has a name. A named constant is exactly like a variable, except that its value is set at compile time and CANNOT change at runtime. A named constant is exactly like a literal constant, except that it HAS A NAME. In a named constant declaration, we indicate that it’s a constant via the const attribute, and we MUST initialize it: const float pi = ; Variables & Constants Lesson #2 CS1313 Spring 2006

8 Name Constant Example Program
% cat circlecalc.c #include <stdio.h> int main () { /* main */ const float pi = ; const float diameter_factor = 2.0; const float area_power = 2.0; float radius, circumference, area; printf("I’m going to calculate a circle’s\n"); printf(" circumference and area.\n"); printf("What’s the radius of the circle?\n"); scanf("%f", &radius); circumference = pi * radius * diameter_factor; area = pi * radius * radius; printf("The circumference is %f\n", circumference); printf(" and the area is %f.\n", area); } /* main */ % gcc -o circlecalc circlecalc.c % circlecalc I’m going to calculate a circle’s circumference and area. What’s the radius of the circle? 5 The circumference is and the area is Variables & Constants Lesson #2 CS1313 Spring 2006

9 The Value of a Named Constant Can’t Be Changed
% cat paramassign.c #include <stdio.h> int main () { /* main */ const float pi = ; pi = 3.0; } /* main */ % gcc -o paramassign paramassign.c paramassign.c: In function ‘main’: paramassign.c:7: warning: assignment of read-only variable ‘pi’ Variables & Constants Lesson #2 CS1313 Spring 2006

10 Why Literal Constants Are BAD BAD BAD
When you embed numeric literal constants in the body of your program, you make it much harder to maintain and upgrade your program. Variables & Constants Lesson #2 CS1313 Spring 2006

11 1997 Tax Program with Numeric Literal Constants
% cat tax1997_literal.c #include <stdio.h> int main () { /* main */ float income, tax; printf("I’m going to calculate the federal income\n"); printf(" tax on your 1997 income.\n"); printf("What was your 1997 income in dollars?\n"); scanf("%f", &income); tax = (income - ( )) * 0.15; printf("The 1997 federal income tax on $%2.2f\n", income); printf(" was $%2.2f.\n", tax); } /* main */ % gcc -o tax1997_literal tax1997_literal.c % tax1997_literal I’m going to calculate the federal income tax on your 1997 income. What was your 1997 income in dollars? 20000 The 1997 federal income tax on $ was $ Variables & Constants Lesson #2 CS1313 Spring 2006

12 1999 Tax Program with Numeric Literal Constants
% cat tax1999_literal.c #include <stdio.h> int main () { /* main */ float income, tax; printf("I’m going to calculate the federal income\n"); printf(" tax on your 1999 income.\n"); printf("What was your 1999 income in dollars?\n"); scanf("%f", &income); tax = (income - ( )) * 0.15; printf("The 1999 federal income tax on $%2.2f\n", income); printf(" was $%2.2f.\n", tax); } /* main */ % gcc -o tax1999_literal tax1999_literal.c % tax1999_literal I’m going to calculate the federal income tax on your 1999 income. What was your 1999 income in dollars? 20000 The 1999 federal income tax on $ was $ Variables & Constants Lesson #2 CS1313 Spring 2006

13 Why Named Constants Are Good
When you use named constants in the body of your program instead of literal constants, you isolate the constant values in the declaration section, making them trivial to find and to change. Variables & Constants Lesson #2 CS1313 Spring 2006

14 1997 Tax Program with Named Constants
% cat tax1997_named.c #include <stdio.h> int main () { /* main */ const float standard_deduction = ; const float single_exemption = ; const float tax_rate = 0.15; const int tax_year = 1997; float income, tax; printf("I’m going to calculate the federal income tax\n"); printf(" on your %d income.\n", tax_year); printf("What was your %d income in dollars?\n", tax_year); scanf("%f", &income); tax = (income - (standard_deduction + single_exemption)) * tax_rate; printf("The %d federal income tax on $%2.2f\n", tax_year, income); printf(" was $%2.2f.\n", tax); } /* main */ % gcc -o tax1997_named tax1997_named.c % tax1997_named I’m going to calculate the federal income tax on your 1997 income. What was your 1997 income in dollars? 20000 The 1997 federal income tax on $ was $ Variables & Constants Lesson #2 CS1313 Spring 2006

15 1999 Tax Program with Named Constants
% cat tax1999_named.c #include <stdio.h> int main () { /* main */ const float standard_deduction = ; const float single_exemption = ; const float tax_rate = 0.15; const int tax_year = 1999; float income, tax; printf("I’m going to calculate the federal income tax\n"); printf(" on your %d income.\n", tax_year); printf("What was your %d income in dollars?\n", tax_year); scanf("%f", &income); tax = (income - (standard_deduction + single_exemption)) * tax_rate; printf("The %d federal income tax on $%2.2f\n", tax_year, income); printf(" was $%2.2f.\n", tax); } /* main */ % gcc -o tax1999_named tax1999_named.c % tax1999_named I’m going to calculate the federal income tax on your 1999 income. What was your 1999 income in dollars? 20000 The 1999 federal income tax on $ was $ Variables & Constants Lesson #2 CS1313 Spring 2006

16 Variables & Constants Lesson #2
Output via printf In C, we output to standard output using a printf statement: printf("This will be output to stdout.\n"); A printf statement can output a string literal, but it can also output the value of a variable, a literal constant or a named constant: printf("%d", number_of_students); The statement above outputs to stdout (the terminal screen) the value of a variable named number_of_students of type int (presumably declared previously in the program that contains this printf statement). Variables & Constants Lesson #2 CS1313 Spring 2006

17 Variables & Constants Lesson #2
Placeholders printf("%d", number_of_students); The statement above outputs the value of a variable named number_of_students of type int (declared previously in the program that contains this printf statement). The %d is known as a placeholder: it holds the place of the value of the variable that we actually want to output. Variables & Constants Lesson #2 CS1313 Spring 2006

18 Placeholders for Various Data Types
int: %d printf("%d", number_of_students); float: %f printf("%f", pi); char: %c printf("%c", middle_initial); Variables & Constants Lesson #2 CS1313 Spring 2006

19 Mixing Literal Text and Variables’ Values #1
We now know that we can output a string literal: printf("This will be output to stdout.\n"); We also know that we can output the value of a variable: printf("%d", number_of_students); Not surprisingly, we can mix and match the two: printf(" on your %d income.\n", tax_year); We can even mix and match while outputting the values of multiple variables of various data types: printf("The %d federal income tax on $%f\n", tax_year, income); Variables & Constants Lesson #2 CS1313 Spring 2006

20 Mixing Literal Text and Variables’ Values #2
We can mix and match literal text and variables’ values while outputting the values of multiple variables of various data types: printf("The %d federal income tax on $%f\n", tax_year, income); This statement means: Output to stdout (the terminal screen) the literal text "The ", and then the value of the int variable named tax_year, and then the literal text " federal income tax on ", and then the value of the float variable named income, and then a newline. Variables & Constants Lesson #2 CS1313 Spring 2006

21 Placeholder & Variable in Same Statement
When you use a placeholder inside the string literal of a printf statement, the variable whose place is being held by the placeholder MUST MUST MUST be in the same printf statement as the placeholder. Putting the placeholder in one printf statement and the variable in a different printf statement is BAD BAD BAD! /* These printfs are GOOD GOOD GOOD! */ printf("f1=%f, ", f1); printf("i1=%d, GOOD!\n", i1); /* These printfs are BAD BAD BAD! */ printf("f2=%f, i2=%d, "); printf("BAD!\n", f2, i2); Variables & Constants Lesson #2 CS1313 Spring 2006

22 Placeholder/Variable Same Statement: Example
% cat placeholder.c #include <stdio.h> int main () { /* main */ float f1, f2; int i1, i2; f1 = 3.75; f2 = 5.25; i1 = 6; i2 = 8; /* These printfs are GOOD GOOD GOOD! */ printf("f1=%f, ", f1); printf("i1=%d, GOOD!\n", i1); /* These printfs are BAD BAD BAD! */ printf("f2=%f, i2=%d, "); printf("BAD!\n", f2, i2); /* This printf is GOOD GOOD GOOD! */ printf("f2=%f, i2=%d, GOOD!\n", f2, i2); } /* main */ % gcc -o placeholder placeholder.c % placeholder f1= , i1=6, GOOD! f2= , i2= , BAD! f2= , i2=8, GOOD! Variables & Constants Lesson #2 CS1313 Spring 2006

23 The printf statement outputs to stdout (the terminal screen).
Input via scanf The printf statement outputs to stdout (the terminal screen). Likewise, the scanf statement inputs from stdin (a user typing at the keyboard). The scanf statement has a somewhat strange syntax: scanf("%d", &height_in_cm); This statement says: input from stdin (a user typing at the keyboard) an int value and place it into the memory location associated with the int variable named height_in_cm. Variables & Constants Lesson #2 CS1313 Spring 2006

24 Input via scanf : Ampersand Before Variable
The scanf statement has a somewhat strange syntax: scanf("%d", &height_in_cm); Notice the ampersand & before the name of the variable that you’re inputting into. For now, you must simply ACCEPT THIS ON FAITH. Variables & Constants Lesson #2 CS1313 Spring 2006

25 Input via scanf Example
% cat read_variable.c #include <stdio.h> int main () { /* main */ int height_in_cm; printf("What’s my height in centimeters?\n"); scanf("%d", &height_in_cm); printf("My height is %d cm.\n", height_in_cm); } /* main */ % gcc -o read_variable read_variable.c % read_variable What’s my height in centimeters? 160 My height is 160 cm. Variables & Constants Lesson #2 CS1313 Spring 2006

26 Input via scanf Example’s Flowchart
printf("What’s my height in centimeters?\n"); scanf("%d", &height_in_cm); printf("My height is %d cm.\n", height_in_cm); Start Prompt for height in cm. Input height in cm. Output height in cm. End Variables & Constants Lesson #2 CS1313 Spring 2006

27 Reading Multiple Variables with a Single scanf
C allows inputting multiple variables per scanf statement. At runtime, when the user types in the input values, they can separate the individual input values by blank spaces, and/or by tabs, and/or by carriage returns (newlines). Blank spaces, tabs and carriage returns, as a group, are known as white space. Variables & Constants Lesson #2 CS1313 Spring 2006

28 Multiple Variables per scanf Example #1
#include <stdio.h> int main () { /* main */ float average_height_in_m; int number_of_silly_people, number_of_toys; char middle_initial; printf("How many silly people are there in CS1313,\n"); printf(" and what’s their average height in meters?\n"); scanf("%d %f", &number_of_silly_people, &average_height_in_m); printf("There are %d silly people\n", number_of_silly_people); printf(" with an average height of %f m.\n", average_height_in_m); printf("How many toys do I have, and\n"); printf(" what is my middle initial?\n"); scanf("%d %c", &number_of_toys, &middle_initial); printf("I have %d toys.\n", number_of_toys); printf("My middle initial is %c.\n", middle_initial); } /* main */ Variables & Constants Lesson #2 CS1313 Spring 2006

29 Multiple Variables per scanf Example #2
% gcc -o read_list read_list.c % read_list How many silly people are there in CS1313, and what’s their average height in meters? There are 7 silly people with an average height of m. How many toys do I have, and what is my middle initial? 43 J I have 43 toys. My middle initial is J. Variables & Constants Lesson #2 CS1313 Spring 2006

30 Variables & Constants Lesson #2
printf vs scanf printf outputs to stdout CAN (and typically does) contain literal text as well as placeholders typically DOES end with a newline variable names after the string literal CANNOT be preceded by & scanf inputs from stdin CANNOT contain literal text, other than spaces to separate the placeholders (which are REQUIRED) CANNOT contain a newline variable names after the string literal MUST be preceded by & Variables & Constants Lesson #2 CS1313 Spring 2006

31 Variables & Constants Lesson #2
Programming Exercise Create a program that: Greets the user. Prompts the user for their age in years. Inputs the user’s age in years. Outputs the user’s age in years. Begin by drawing a flowchart, and then write the program. The program does not have to have comments. The data type for the age variable must be appropriate. Variables & Constants Lesson #2 CS1313 Spring 2006


Download ppt "Variables & Constants Lesson 2 Outline"

Similar presentations


Ads by Google