Variables & Constants Lesson 2 Outline

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Pointer Lesson 2 CS1313 Spring Pointer Lesson 2 Outline 1.Pointer Lesson 2 Outline 2.Pass by Reference Bad Example 3.Pass by Reference Good Example.
Constants Lesson CS1313 Spring Constants Lesson Outline 1.Constants Lesson Outline 2.What is a Constant? 3.The Difference Between a Variable and.
Standard I/O Lesson Outline
Standard I/O Lesson CS1313 Spring Standard I/O Lesson Outline 1.Standard I/O Lesson Outline 2.Output via printf 3.Placeholders 4.Placeholders for.
Computer Programming w/ Eng. Applications
Arithmetic Expressions Lesson #1 CS1313 Spring Arithmetic Expressions Lesson #1 Outline 1.Arithmetic Expressions Lesson #1 Outline 2.A Less Simple.
C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
I/O with scanf()/printf() I/O stands for “Input/Output” In CSE 142, you will use: –scanf() (reading from the keyboard) –printf() for output (printing to.
CSC Programming for Science Lecture 5: Actual Programming.
Introduction to C Programming
C Introduction Lesson CS1313 Spring C Introduction Lesson Outline 1.C Introduction Lesson Outline 2. hello_world.c 3.C Character Set 4.C is Case.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
C Introduction Lesson CS1313 Spring C Introduction Lesson Outline 1.C Introduction Lesson Outline 2. hello_world.c 3.C Character Set 4.C is Case.
TDBA66, VT-03 Lecture - Ch. 21 A complete C-program Display Fig. 2.1 and comment on different things such as Preprocessor directives Header files Identifiers.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
41 A Depth Program #include int main(void) { int inches, feet, fathoms; //declarations fathoms = 7; feet = 6 * fathoms; inches = 12 * feet; printf(“Wreck.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
User Defined Functions Lesson 2 CS1313 Spring User Defined Functions 2 Outline 1.User Defined Functions 2 Outline 2.Argument Order When Passing.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
LAB 2 : PROBLEM SOLVING TECHNIQUES, ALGORITHM: PSEUDO CODE AND FLOWCHART LAB INTRODUCTION Prepared by: Cik Noor Syazana bt Arshad.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
Array Lesson 1 CS1313 Spring Array Lesson 1 Outline 1.Array Lesson 1 Outline 2.Mean of a List of Numbers 3.mean: Declarations 4.mean: Greeting,
Yu Yuanming CSCI2100B Data Structures Tutorial 3
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Numeric Data Types Lesson CS1313 Spring Numeric Data Types Outline 1.Numeric Data Types Outline 2.Data Types 3.Integers in Mathematics 4.Integers.
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Array Lesson 2 CS1313 Spring Array Lesson 2 Outline 1.Array Lesson 2 Outline 2.Reading Array Values Using for Loop #1 3.Reading Array Values Using.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
CSCI 130 Chapter 3. Variables & Names Variable Declarations: –reserve a storage location in memory –identify the name of the variable –identify the type.
C OMPUTER P ROGRAMMING 1 Assignment. A SSIGNMENT We have used gets to input a value into variable The second way to give a variable a value is known as.
Structures Lesson CS1313 Spring Structures Lesson Outline 1.Structures Lesson Outline 2.Beyond Arrays 3.A Company and Its Employees #1 4.A Company.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
Khalid Rasheed Shaikh Computer Programming Theory 1.
Boolean Data Lesson CS1313 Fall Boolean Data Outline 1.Boolean Data Outline 2.Data Types 3.C Boolean Data Type: char or int 4.C Built-In Boolean.
CSE1301 Computer Programming: Lecture 6 Input/Output.
User Defined Functions Lesson 1 CS1313 Spring User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Arithmetic Expressions Lesson #2 Outline 1.Arithmetic Expressions Lesson #2 Outline 2.Named.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Idiotproofing Lesson CS1313 Spring Idiotproofing Outline 1.Idiotproofing Outline 2.Idiotproofing 3.Idiotproofing Quotes 4.Idiotproofing Example.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Lecture2.
Arithmetic Expressions
Formatted Input and Output
Zhang Hongyi CSCI2100B Data Structures Tutorial 3
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Numeric Data Types Outline
Array Lesson 1 Outline Array Lesson 1 Array Lesson 1 Outline
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Lecture3.
CS150 Introduction to Computer Science 1
Arithmetic Expressions Lesson #2 Outline
Variables Lesson Outline
Arithmetic Expressions Lesson #1 Outline
Getting Started With Coding
Presentation transcript:

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

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

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

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

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

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_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 $20000.00 was $1980.00. Variables & Constants Lesson #2 CS1313 Spring 2006

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

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 % circlecalc I’m going to calculate a circle’s circumference and area. What’s the radius of the circle? 5 The circumference is 31.415924 and the area is 78.539810. Variables & Constants Lesson #2 CS1313 Spring 2006

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.c paramassign.c: In function ‘main’: paramassign.c:7: warning: assignment of read-only variable ‘pi’ Variables & Constants Lesson #2 CS1313 Spring 2006

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

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_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 $20000.00 was $1980.00. Variables & Constants Lesson #2 CS1313 Spring 2006

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 - (4300.0 + 2750.0)) * 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 $20000.00 was $1942.50. Variables & Constants Lesson #2 CS1313 Spring 2006

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

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_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 $20000.00 was $1980.00. Variables & Constants Lesson #2 CS1313 Spring 2006

1999 Tax Program with Named Constants % cat tax1999_named.c #include <stdio.h> int main () { /* main */ const float standard_deduction = 4300.0; const float single_exemption = 2750.0; 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 $20000.00 was $1942.50. Variables & Constants Lesson #2 CS1313 Spring 2006

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

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

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

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

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

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

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=3.750000, i1=6, GOOD! f2=3.750000, i2=134513662, BAD! f2=5.250000, i2=8, GOOD! Variables & Constants Lesson #2 CS1313 Spring 2006

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

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

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

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

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

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

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? 7 1.75 There are 7 silly people with an average height of 1.750000 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

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

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