variables & constants lesson #2 cs1313 spring 2006 1 variables & constants lesson 2 outline...

Post on 13-Dec-2015

243 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Variables & Constants Lesson #2CS1313 Spring 2006 1

Variables & Constants Lesson 2 Outline1. Variables & Constants Lesson 2 Outline2. Constants3. The Difference Between a Variable and

a Constant4. Categories of Constants: Literal &

Named5. Literal Constants6. Literal Constant Example Program7. Named Constants8. Name Constant Example Program9. The Value of a Named Constant Can’t

Be Changed10. Why Literal Constants Are BAD BAD

BAD11. 1997 Tax Program with Numeric Literal

Constants12. 1999 Tax Program with Numeric Literal

Constants13. Why Named Constants Are Good14. 1997 Tax Program with Named

Constants15. 1999 Tax Program with Named

Constants

16. Output via printf17. Placeholders18. Placeholders for Various Data Types19. Mixing Literal Text and Variables’ Values

#120. Mixing Literal Text and Variables’ Values

#221. Placeholder & Variable in Same Statement22. Placeholder/Variable Same Statement:

Example23. Input via scanf24. Input via scanf : Ampersand Before

Variable25. Input via scanf Example26. Input via scanf Example’s Flowchart27. Reading Multiple Variables with a Single

scanf28. Multiple Variables per scanf Example #129. Multiple Variables per scanf Example #230. printf vs scanf31. Programming Exercise

Variables & Constants Lesson #2CS1313 Spring 2006 2

Constants

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 #2CS1313 Spring 2006 3

The Difference Between a Variable and a Constant

The difference between a variable and a constant: a variable’s value can vary, but a constant’s value is constant.

Variables & Constants Lesson #2CS1313 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 #2CS1313 Spring 2006 5

Literal Constants

A literal constant is a constant whose value is specified literally:

int literal constants(e.g., 5, 0, -127, 403298, -385092809)

float literal constants(e.g., 5.2, 0.0, -127.5, 403298.2348, -3.85092809e+08)

char literal constants(e.g., ’A’, ’7’, ’?’)

character string literal constants(e.g., "A", "Henry", "What’s it to ya?")

Variables & Constants Lesson #2CS1313 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 - (4150.0 + 2650.0)) * 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_literalI’m going to calculate the federal incometax on your 1997 income.What was your 1997 income in dollars?20000The 1997 federal income tax on $20000.00was $1980.00.

Variables & Constants Lesson #2CS1313 Spring 2006 7

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 = 3.1415926;

Variables & Constants Lesson #2CS1313 Spring 2006 8

Name Constant Example Program% cat circlecalc.c#include <stdio.h>

int main (){ /* main */ const float pi = 3.1415926; 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% circlecalcI’m going to calculate a circle’scircumference and area.What’s the radius of the circle?5The circumference is 31.415924and the area is 78.539810.

Variables & Constants Lesson #2CS1313 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 = 3.1415926;

pi = 3.0;} /* main */% gcc -o paramassign paramassign.cparamassign.c: In function ‘main’:paramassign.c:7: warning: assignment of read-only

variable ‘pi’

Variables & Constants Lesson #2CS1313 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 #2CS1313 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 - (4150.0 + 2650.0)) * 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_literalI’m going to calculate the federal income tax on your 1997 income.What was your 1997 income in dollars?20000The 1997 federal income tax on $20000.00 was $1980.00.

Variables & Constants Lesson #2CS1313 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 19991999 income.\n"); printf("What was your 19991999 income in dollars?\n"); scanf("%f", &income); tax = (income - (4300.04300.0 + 2750.02750.0)) * 0.15; printf("The 19991999 federal income tax on $%2.2f\n", income); printf(" was $%2.2f.\n", tax);} /* main */% gcc -o tax1999_literal tax1999_literal.c% tax1999_literalI’m going to calculate the federal income tax on your 1999 income.What was your 1999 income in dollars?20000The 1999 federal income tax on $20000.00 was $1942.50.

Variables & Constants Lesson #2CS1313 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 #2CS1313 Spring 2006 14

1997 Tax Program with Named Constants% cat tax1997_named.c#include <stdio.h>int main (){ /* main */ const float standard_deduction = 4150.0; const float single_exemption = 2650.0; 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_namedI’m going to calculate the federal income tax on your 1997 income.What was your 1997 income in dollars?20000The 1997 federal income tax on $20000.00 was $1980.00.

Variables & Constants Lesson #2CS1313 Spring 2006 15

1999 Tax Program with Named Constants% cat tax1999_named.c#include <stdio.h>int main (){ /* main */ const float standard_deduction = 4300.04300.0; const float single_exemption = 2750.02750.0; const float tax_rate = 0.15; const int tax_year = 19991999; 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_namedI’m going to calculate the federal income tax on your 1999 income.What was your 1999 income in dollars?20000The 1999 federal income tax on $20000.00 was $1942.50.

Variables & Constants Lesson #2CS1313 Spring 2006 16

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 #2CS1313 Spring 2006 17

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 #2CS1313 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 #2CS1313 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 #2CS1313 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 #2CS1313 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 #2CS1313 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% placeholderf1=3.750000, i1=6, GOOD!f2=3.750000, i2=134513662, BAD!f2=5.250000, i2=8, GOOD!

Variables & Constants Lesson #2CS1313 Spring 2006 23

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 #2CS1313 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 #2CS1313 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_variableWhat’s my height in centimeters?160My height is 160 cm.

Variables & Constants Lesson #2CS1313 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

End

Prompt for height in cm.

Input height in cm.

Output height in cm.

Variables & Constants Lesson #2CS1313 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 #2CS1313 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 #2CS1313 Spring 2006 29

Multiple Variables per scanf Example #2% gcc -o read_list read_list.c% read_listHow many silly people are there in CS1313,and what’s their average height in meters?7 1.75There are 7 silly peoplewith an average height of 1.750000 m.How many toys do I have, andwhat is my middle initial?43 JI have 43 toys.My middle initial is J.

Variables & Constants Lesson #2CS1313 Spring 2006 30

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 #2CS1313 Spring 2006 31

Programming Exercise

Create a program that:

1. Greets the user.

2. Prompts the user for their age in years.

3. Inputs the user’s age in years.

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

top related