Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
CS16 Week 2 Part 2 Kyle Dewey. Overview Type coercion and casting More on assignment Pre/post increment/decrement scanf Constants Math library Errors.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Lecture 1 The Basics (Review of Familiar Topics).
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,
Binary Arithmetic Math For Computers.
C PROGRAMMING LECTURE 17 th August IIT Kanpur C Course, Programming club, Fall by Deepak Majeti M-Tech CSE
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Introduction to Python
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Conditional Statement
Functions in C. Consider #include main() { int i; for(i=1; i
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
CSC 107 – Programming For Science. The Week’s Goal.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Variables & Operators 1 Variables Store information needed by the program Must have a TYPE int - can only store a number without a fractional part float,
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
CSc 352: Basic C Programming Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
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.
C is a high level language (HLL)
Announcements Quiz this Thursday 1. Multi dimensional arrays A student got a warning when compiling code like: int foo(char **a) { } int main() { char.
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Java Programming Language Lecture27- An Introduction.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Basic Data Types & Memory & Representation
User-Written Functions
The Machine Model Memory
Introduction to Computer Science / Procedural – 67130
Object Oriented Programming
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Introduction to C++ October 2, 2017.
Variables Store information needed by the program Must have a TYPE
Expressions and Assignment Statements
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Conditions and Ifs BIS1523 – Lecture 8.
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Differences between Java and C
Introduction to Primitives
Introduction to Primitives
Primitive Types and Expressions
DATA TYPES There are four basic data types associated with variables:
Expressions and Assignment Statements
Presentation transcript:

Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1

constants Constants in a c program can be defined using #define – #define PI 3.14 A constant is constant and unlike a variable can’t have a new value assigned to it. By convention in c constants are all CAPS Constants are used for readability and to make changes easier For example, suppose we had a program that used 3.14 as the value for pi, and it was decided that more accuracy was needed. If a constant was used, then you would only have to change the program in one place – #define PI

Constants continued 3 Two constants defined Notice formatting of output

Constants continued 4 Trying to assign a value to a constant.

Constants (last slide) 5 Gives us an error. What does it mean by lvalue?

Assignment: l -values 6 increment x twice? what’s going on?

Assignment: l -values The left-hand-side (destination) of an assignment has to be a location – this is referred to as an “ l -value” Consider an expression x x has a location and a value the ++ operator assigns the value x+1 to the location of x the expression ‘x ++’ has the value of x, but no location (as a side-effect, x gets incremented) the outer “++” operator attempts to increment x++ but x++ is an expression that has no location!  error

What can be an l -value? l -values: – names of variables of arithmetic type (int, char, float, etc.) – array elements: x [ y ] – also: structs, unions, pointers, etc. (to be discussed later) operations involving pointers (to be discussed later) Not l -values: – functions – result of an assignment – value returned by a function call 8

Formatted Input: scanf() – Another Look takes a variable no. of arguments: – scanf(“…fmtStr…”, arg 1, arg 2, …, arg n ) “… fmtStr…” is a string that can contain conversion specifiers the no. of conversion specifiers should be equal to n arg i are locations where values that are read in should be placed each conversion specifier is introduced by ‘%’ – similar to conversions for printf execution behavior: – uses format string to read in as many input values from stdin as it can – return value indicates the no. of values it was able to read return value of EOF (-1) indicates no further input (“end of file”). 9 text: Ch. 3 Sec. 3.2

scanf() Specifying where to put an input value: – in general we need to provide a pointer to the location where the value should be placed for a scalar variable X, this is typically written as &X – Examples: scanf(“%d”, &n) : read a decimal value into a variable n scanf(“%d %d %d”, &x, &y, &z) : read three decimal values and put them into variables x, y, z respectively – suppose the input contains the values Then: » x  12; y  3; z  71; return value = 3 – suppose the input contains the values Then: » x  19; y  23; z is unassigned; return value = 2 10

f2c revisited 11 Always same temperature, not very useful.

f2c revisited 12 Notice the error?

f2c revisited 13 Compilation got warning. When run result shows 0.0.

f2c revisited 14 Correct this time. Notice & before the f?

f2c revisited 15 Here we see the program works this time. However, we have not error handling. Also, what if we want multiple inputs?

Expressions and Statements A statement is a command to be executed when a program is run – printf(“hello\n”); – x = 5; An expression is something which computes to a value – 5 – x + y – x == 8 In c, all expressions can be statements just by adding ; – 5; – x + y; 16

Expressions and Statements In c many things you might not expect are expressions like assignment – x = 8 /* evaluates to the value 8 */ Loosely, a side effect is an action that happens while evaluating an expression – scanf(“%d”, &i) evaluates to the number of variables assigned values | side effect: stores value in i – x++ evaluates to x + 1 | side effect: stores the value of x + 1 in x – x = 8 evaluates to 8 | side effect stores the value 8 in x 17

Expressions and Statements So the assignment = in c is really an operator that returns the value to the right and has a side effect of storing that value in the variable to the left This is why x = y = z = 1 is legal in c. The assignment operator is evaluated from left to right. a = b += c++ - d + --e / -f – is legal, well defined, but BAD programming (DON’T DO IT) And don’t ask me to evaluate it without a table of precedence and a lot of time. 18

Expressions and Statements a = 5; c = (b = a + 2) – (a = 1); This is legal but NOT well defined. Sometimes it will evaluate to 6 and sometimes 2. Even on the same machine. 19

My Most Common c Error if (x = 5) {... } I had meant x == 5, but I typed it wrong. But in c, this is still legal. x = 5 stores the value of 5 in x and evaluates to 5 5 is considered “true” by c (everything not 0 is true), so the if statement ALWAYS executes. Fortunately, the –Wall option for gcc will warn about this error. 20

f2c Last Visit 21 Old code, no error checking.

f2c Last Visit 22 An error here First attempt to add error checking has a bug.

f2c Last Visit 23 Correct Code

Primitive data types: Java vs. C C provides some numeric types not available in Java – unsigned The C language provides only a “minimum size” guarantee for primitive types – the actual size may vary across processors and compilers Originally C did not have a boolean type – faked it with ints: 0  false; non-0  true hence code of the form: if ( (x = getnum()) ) { … } // if value read is nonzero – C99 provides some support for booleans 24

Primitive numeric types: Java vs. C 25 JavaCC sizeComments byte unsigned char typically (and at least) 8 bits signed char typically (and at least) 8 bits char typically (and at least) 8 bits signedness is implementation dependent short unsigned short int typically (and at least) 16 bits signed short int== unsigned short int unsigned int16 or 32 bits the “natural” size for the machine signed intsame as unsigned int ? unsigned long int typically (and at least) 32 bits signed long int== unsigned long long unsigned long long int typically (and at least) 64 bits signed long long int== unsigned long long Note: the keywords in gray may be omitted

Signed vs. unsigned values Essential idea: – “signed” : the highest bit of the value is interpreted as the sign bit (0 = +ve; 1 =  ve) – “unsigned” : the highest bit not interpreted as sign bit For an n -bit value: – signed: value ranges from  2 n -1 to +2 n -1  1 – unsigned: value ranges from 0 to 2 n  1 Right-shift operator ( >> ) may behave differently – unsigned values: 0s shifted in on the left – (signed) negative values: bit shifted in is implementation dependent 26

Booleans Originally, C didn’t have a separate boolean type – truth values were ints: 0 = false; non-0 = true – still commonly used in programming C99 provides the type _Bool – _Bool is actually an (unsigned) integer type, but can only be assigned values 0 or 1, e.g.: _Bool flag; flag = 5; /* flag is assigned the value 1 */ C99 also provides boolean macros in stdbool.h: #include bool flag; /* same as _Bool flag */ flag = true; 27

Arithmetic operators: Java vs. C Most of the common operators in C are as in Java – e.g.: +, ++, +=, -, -=, *, /, %, >>, <<, &&, ||, … C doesn’t have operators relating to objects: new, instanceof C doesn’t have >>> (and >>>=) – use >> on unsigned type instead 28