0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,

Slides:



Advertisements
Similar presentations
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Advertisements

All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
C expressions (Reek, Ch. 5) 1CS 3090: Safety Critical Programming in C.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
JavaScript, Third Edition
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
CPS120: Introduction to Computer Science Lecture 8.
Types, Operators and Expressions CSE 2031 Fall /5/2015 3:59 PM.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
2440: 211 Interactive Web Programming Expressions & Operators.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Numeric Types, Expressions, and Output ROBERT REAVES.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Chapter 2: Using Data.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Lesson 3 – Primitive Data Types Objective: Students will investigate the definition and usage of objects and primitive data types in Java in order to practice.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
 All C programs are made up of functions that perform operations on variables.  In this lecture we examine variables  Variables are the basic building.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
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.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Operators & Expressions
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
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.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
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.
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.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
CSE 220 – C Programming Expressions.
Data types Data types Basic types
Chap. 2. Types, Operators, and Expressions
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Sachin Malhotra Saurabh Choudhary
Tokens in C Keywords Identifiers Constants
Data Types, Identifiers, and Expressions
Operators and Expressions
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Types, Operators and Expressions
More about Numerical Computation
Basics of ‘C’.
Introduction to C Programming
Chapter-3 Operators.
Lectures on Numerical Methods
Associativity and Prescedence
Expressions and Assignment
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Module 2 Variables, Data Types and Arithmetic
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Presentation transcript:

0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner, diuf.unifr.ch/pai/sp, Lecture 2 – 24 February 2015 KR – Chap. 2: ~45’; Tutorials: Memory Model I: ~45’, scanf: ~40’, AST&CS for expressions: ~30’; Exercises: ~20’ 2.5Arithmetic Operators 2.6Relational and Logical Operators 2.7Type Conversions 2.8Increment and Decrement Operators 2.9Bitwise Operators 2.10Assignment Operators and Expressions 2.11Conditional Expressions 2.12Precedence and Order of Evaluation

1 2.1 Variable Names Names are made of letters and digits, but : –the first character must be a letter –the underscore “_” counts as a letter (useful for improving readability) –don’t begin variable names with underscore (since library routines often use such names) Case sensitivity –Upper and lower case are distinct, so x and X are two different names –Traditional C pratice is to use : lower case for variable names upper case for symbolic constants Miscellaneous –At least the first 31 characters of an internal name are significant (!!!) –For external names, the standard guarantees uniqueness only for 6 characters (!!!) –Keywords like int, float, if, else, etc., are reserved: you can’t use them as variable names

2 2.2 Data Types and Sizes Basic data types –char, int, float, double Qualifiers –short int, long int short is often 16 bits, long 32 bits, and int either 16 or 32 bits –signed, unsigned : apply to char and any integer if chars are 8 bits, unsigned chars have values between 0 and 255 (i.e. 0 and ), while signed chars between -128 and 127 in a two’s complement machine (i.e and ) –long double Standard Header Files (see [KR88], p. 257) – defines constants for the sizes of integral types Examples: INT_MIN, INT_MAX, LONG_MAX, LONG_MIN – defines constants related to floating-point arithmetic

3 Signed integer (1/3) Signed magnitude The leftmost bit is the sign bit One’s complement To negate a number, replace each 1 by 0 and each 0 by Idea: the leftmost bit is used for positive resp. negative integers

4 Signed integer (2/3) Two’s complement First step: as one’s complement Second step: add one to the result

5 Signed integer (3/3) Excess 2 m-1 (here m=3) a number is stored as its true value plus 2 m

6 2.3 Constants 1234 : integer constant L : long constant 1234U : unsigned constant UL : unsigned long Complete set of escape sequences: \a \b \f \n \r \t \v \\ \? \' \" \ooo \xhh '\ooo' : ooo is one to three octal digits (0.. 7) '\xhh' : hh is 1 or 2 hexadecimal digits (0.. 9, a.. f, A.. F) 'x' : character constant (The value of ‘x’ is the numeric value of x in the machine’s character set) : octal (with a leading zero) 0X1234 : hexadecimal Enumeration constant : enum boolean {NO, YES} enum escape {BELL = '\a', \ TAB='\t'} "I am a string" : string constant "hello" "world" : string concatenation (a string constant is a array of characters, with a null character '\0' at the end; '\0' represents the character with value zero) Be careful : 'x' is a character constant and "x" is a string constant

7 2.4 Declarations All variables must be declared before use A declaration specifies a type, and contains a list of one or more variables of that type, as in : int lower, upper, step; char c, line[1000]; A variable may be initialized in its declaration, as in : char esc = '\\'; int i = 0, limit = MAXLINE + 1; float eps = 1.0e-5; External and static variables are intialized to zero by default Automatic variables for which there is no explicit initializer have undefined (garbage) values, e.g. non-static local variables declared inside the body of a function. const qualifier can be applied to the declaration of any variable to specify that its value will not be changed const double e = ; const char msg[] = "warning: "; int strlen(const char[]);

8 2.4 Declarations : enum variables enum {RED, GREEN, BLUE} type enum with values RED (=0), GREEN (=1), BLUE (=2) this is just a type, like char or int, but with different possible values enum {RED, GREEN, BLUE} c1 = RED, c2; variable declaration c1 and c2 of type enum {RED, GREEN, BLUE} variable c1 with value RED (=0) variable c2 without any value; later a value, e.g. BLUE, can be assigned to c2: c2=BLUE; enum color_tag {RED, GREEN, BLUE} c1; type enum {RED, GREEN, BLUE} with optional tag color_tag, and variable declaration c1 enum color_tag c2, c3=GREEN, c4=2; variable declaration of type enum color_tag : c2, without any value c3, with value GREEN c4, with value 2, i.e. BLUE

9 2.5 Arithmetic Operators +, -, *, /, % –The integer division / truncates any fractional part –The expression x%y, which reads “x modulus y”, produces the remainder when x is divided by y Machine dependance for negative integer operands –The direction of truncation for / –The sign of the result for % Precedence rules –The binary + and - operators have the same precedence, which is lower than the precedence of *, /, and %, which is in turn lower than the unary + and -. –Arithmetic operators associate left to right.

Relational and Logical Operators Relational operators: >, >=, <, <= Equality operators: ==, != Logical operators: &&, || –Expressions connected by && and || are evaluated left to right, and evaluation stops as soon as the truth or falsehood of the result is known –Example: for (i=0; i<lim-1 && (c=getchar()) != '\n' && c != EOF; ++i) s[i] = c; –By definition, the numeric value of a relational or logical expression is 1 if the relation is true, and 0 if the relational is false Unary negation operator: ! –Converts a non-zero operand into 0, and a zero operand into 1 –Example: if (!valid) is equivalent to if (valid == 0)

Type Conversions Explicit type conversions : cast unary operator – (type-name) expression – Example: (double) n Automatic Arithmetic Conversion Rules (a slight simplified vue) Vertical conversions are always performed Horizontal conversions are performed only when necessary int –> unsigned –> long –> double char, short intfloat

Increment and Decrement Operators The increment operator ++ adds 1 to its operand, while the decrement operator -- substracts 1 ++ and -- may be used either as prefix or postfix operators: –++n increments n before its value is used –n++ increments n after its value has been used Example if n is 5, then x = ++n; sets x to 6, but x = n++; sets x to 5. In both cases, n becomes 6.

Bitwise Operators &bitwise AND |bitwise OR ^bitwise XOR << left shift >> right shift ~ one’s complement ( converts each 1-bit into a 0-bit and vice versa) Examples n = n & 0177 (sets to zero all but the low-order 7 bits of n) n = n & ~077 (sets the last 6 bits of n to zero) Illustration /* getbits: get n bits from position p */ unsigned my_getbits(unsigned x, int p, int n) { return (x >> (p+1-n)) & ~(~0 << n); } p 0 p - n n p - n + 1 moves the desired n bits to the right places 0-bits in the rightmost n bits and 1-bits elsewhere

Assignment Operators and Expressions Most binary operators have a corresponding assignment operator op=, where op is one of : + – * / % > & ^ | If expr1 and expr2 are expressions, then expr1 op= expr2 is equivalent to expr1 = expr1 op (expr2) Assignment expression has the same value as the left expression of the assignment Example: while ((c = getchar()) != EOF) {…}

Conditional Expressions The conditional expression expr1 ? expr2 : expr3 provides an alternate way to write the if-else statement. Note that the operator ?: has three operands. Such an operator is commonly called a ternary operator. Example z = (a > b) ? a : b; /* z = max(a,b) */

Precedence and Order of Evaluation The rules for precedence and associativity of all operators are summarized in Table 2.1. C, like most languages, does not specify the order in which the operands of an operator are evaluated. The exceptions are (with a guaranted left to right evaluation) : && || ?:, ( comma operator) Examples –In a statement like x = f() + g(); f may be evaluated before g or vice versa. –Logical expressions are evaluated from left to right and the evaluation is stopped as soon as the result TRUE or FALSE is established.

17 Table 2.1: Precedence and Associativity of Operators Associativity Operators left to right () [] ->. right to left ! ~ * (type) sizeof left to right / % left to right + - left to right > left to right >= left to right == != left to right & left to right ^ left to right | left to right && left to right || left to right ?: right to left = += -= *= /= %= &= ^= |= >= left to right, Remark. Unary & +, -, and * have higher precedence than the binary forms.