C-1 University of Washington Computer Programming I Lecture 3: Variables, Values, and Types © 2000 UW CSE.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Lecture 2 Introduction to C Programming
Introduction to C Programming
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.
Introduction to C Programming
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
B-1 Lecture 2: Problems, Algorithms, and Programs © 2000 UW CSE University of Washington Computer Programming I.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
110-D1 Variables, Constants and Calculations(1) Chapter 3: we are familiar with VB IDE (building a form…) to make our applications more powerful, we need.
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Input, Output, and Processing
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
C++ Programming: Basic Elements of C++.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CSC 107 – Programming For Science. The Week’s Goal.
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
9/29/99B-1 CSE / ENGR 142 Programming I Variables, Values, and Types © 1998 UW CSE.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
A Sample Program #include using namespace std; int main(void) { cout
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
CSC Programming for Science Lecture 5: Actual Programming.
Introduction to Computer Science / Procedural – 67130
What's a Computer? Monitor Disk Main mouse Memory Keyboard Network
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
Variables, Expressions, and IO
Lecture2.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
CS111 Computer Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
University of Washington Computer Programming I
Primitive Types and Expressions
Variables in C Topics Naming Variables Declaring Variables
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Presentation transcript:

C-1 University of Washington Computer Programming I Lecture 3: Variables, Values, and Types © 2000 UW CSE

C-2 Overview Concepts this lecture: Variables Declarations Identifiers and Reserved Words Types Expressions Assignment statement Variable initialization

C-3 Review: Computer Organization Central Processing Unit Main Memory Monitor Network Disk Keyboard mouse

C-4 Review: Memory Memory is a collection of locations called variables In a programming language, we get at the location by using a variable Each variable has A name (an identifier) A type (the kind of information it can contain)

C-5 Memory and Variables Memory is a collection of locations called variables In a programming language, we get at the location by using a variable Each variable has A name (an identifier) A type (the kind of information it can contain) Basic types include int (integers – whole numbers: 17, -42) double (floating-point numbers with optional fraction and/or exponent: , 6.02e23) char (character data: ‘a’, ‘?’, ‘N’, ‘ ’, ‘9’) Note: ‘9’ is a character; 9 is an integer – they are different and have different types

C-6 Memory example Variable declarations in C int i = 12; double gasPrice = 1.799; char bang = ‘!’; Picture: i gasPrice bang ‘!’ (int) (double) (char)

C-7 Declaring Variables int months; Integer variables represent whole numbers: 1, 17, -32, 0Not 1.5, 2.0, ‘A’ double pi; Floating point variables represent real numbers: 3.14, -27.5, 6.02e23, 5.0Not 3 char first_initial, middle_initial, marital_status; Character variables represent individual keyboard characters: 'a', 'b', 'M', '0', '9', '#', ' ' Not "Bill"

C-8 Variable Names "Identifiers" are names for things in a program for examples, names of variables In C, identifiers follow certain rules: use letters, numerals, and underscore ( _ ) do not begin with a numeral cannot be “reserved words” are "case-sensitive" can be arbitrarily long but... Style point: Good choices for identifiers can be extremely helpful in understanding programs Often useful: noun or noun phrase describing variable contents

C-9 Reserved words Certain identifiers have a "reserved" (permanent, special) meaning in C intWe’ve seen int already Will see a couple of dozen more eventually These words always have that special meaning, and cannot be used for other purposes. Cannot be used names of variables Must be spelled exactly right Sometimes also called “keywords”

C-10 Under the Hood All information in the CPU or memory is actually a series of ‘bits’: 1’s and 0’s Known as ‘binary’ data Amazingly, all kinds of data can be represented in binary: numbers, letters, sounds, pictures, etc. The type of a variable specifies how the bits are interpreted Normally we ignore the underlying bits and work with C types BinaryC type (sample) value int161 char‘A’ double10.73

C-11 Assignment Statements int area, length, width; length= 16; width= 32; area = length * width; An assignment statement stores a value into a variable. The assignment may specify a simple value to be stored, or an expression Execution of an assignment statement is done in two distinct steps: Evaluate the expression on the right hand side Store the value of the expression into the variable named on the left hand side /* declaration of 3 variables */ /* "length gets 16"*/ /* "width gets 32"*/ /* "area gets length times width" */

C-12 my_age = my_age+1 This is a “statement”, not an equation. Is there a difference? The same variable may appear on both sides of an assignment statement my_age = my_age + 1 ; balance = balance + deposit ; The old value of the variable is used to compute the value of the expression, before the variable is changed. You wouldn’t do this in math!

C-13 Program Execution A memory location is reserved by declaring a C variable You should give the variable a name that helps someone else reading the program understand what it is used for in that program Once all variables have been assigned memory locations, program execution begins The CPU executes instructions one at a time, in order of their appearance in the program (we will introduce more options later)

C-14 An Example /* calculate and print area of 10x3 rectangle */ #include int main(void) { intrectangleLength; intrectangleWidth; intrectangleArea; rectangleLength = 10; rectangleWidth = 3; rectangleArea = rectangleLength * rectangleWidth ; printf(“%d”, rectangleArea); return 0; }

C-15 Hand Simulation A useful practice is to simulate by hand the operation of the program, step by step. This program has three variables, which we can depict by drawing boxes or making a table We mentally execute each of the instructions, in sequence, and refer to the variables to determine the effect of the instruction

C-16 Tracing the Program rectangleLengthrectangleWidthrectangleArea after declaration ??? after statement 1 10?? after statement 2 103? after statement

C-17 Initializing variables Initialization means giving something a value for the first time. Anything which changes the value of a variable is a potential way of initializing it. For now, that means assignment statement General rule: variables have to be initialized before their value is used. Failure to initialize is a common source of bugs. Variables in a C program are not automatically initialized to 0!

C-18 Declaring vs Initializing int main (void) { double income; /*declaration of income, not an assignment or initialization */ income = ; /*assignment to income, initialization of income, not a declaration.*/ printf ("Old income is %f", income); income = ; /*assignment to income, not a declaration,or initialization */ printf ("After raise: %f", income); return 0; }

C-19 Example Problem: Fahrenheit to Celsius Problem (specified): Convert Fahrenheit temperature to Celsius

C-20 Example Problem: Fahrenheit to Celsius Problem (specified): Convert Fahrenheit temperature to Celsius Algorithm (result of analysis): Celsius = 5/9 (Fahrenheit - 32) What kind of data (result of analysis): double fahrenheit, celsius;

C-21 Fahrenheit to Celsius (I) An actual C program #include int main(void) { double fahrenheit, celsius; celsius = (fahrenheit ) * 5.0 / 9.0; return 0; }

C-22 Fahrenheit to Celsius (II) #include int main(void) { double fahrenheit, celsius; printf("Enter a Fahrenheit temperature: "); scanf("%lf", &fahrenheit); celsius = (fahrenheit ) * 5.0 / 9.0; printf("That equals %f degrees Celsius.", celsius); return 0; }

C-23 Running the Program Enter a Fahrenheit temperature: 45.5 That equals degrees Celsius Program trace fahrenheitcelsius after declaration?? after first printf?? after scanf45.5? after assignment after second printf

C-24 celsius = (fahrenheit-32.0) * 5.0 / 9.0 ; 1. Evaluate right-hand side a. Find current value of fahrenheit 72.0 b. Subtract b. Multiply by c. Divide by Assign 22.2 to be the new value of celsius (the old value of celsius is lost.) Assignment step-by-step

C-25 Fahrenheit to Celsius (III) #include int main(void) { double fahrenheit, celsius; printf("Enter a Fahrenheit temperature: "); scanf("%lf", &fahrenheit); celsius = fahrenheit ; celsius = celsius * 5.0 / 9.0 ; printf("That equals %f degrees Celsius.", celsius); return 0; }

C-26 Does Terminology Matter? Lots of new terminology today! "variable", "reserved word", "initialization", "declaration", “statement”, "assignment", etc., etc. You can write a complicated program without using these words But you can't talk about your programs without them! Learn the exact terminology as you go, and get in the habit of using it.

C-27 Next Lecture: Expressions Each lecture builds on the previous ones, so... be sure you’re solid with this material before going on!