ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 1: Types, Operators and Expressions.

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Data types and variables
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
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.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
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.
Objectives You should be able to describe: Data Types
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 2 part #4 Operator
UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming1 Introduction to C – Part 2.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
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 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
C++ Programming: Basic Elements of C++.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
CPS120: Introduction to Computer Science Operations Lecture 9.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Copyright © – Curt Hill Types What they do.
COMP Primitive and Class Types Yi Hong May 14, 2015.
1 Expressions. 2 Variables and constants linked with operators  Arithmetic expressions Uses arithmetic operators Can evaluate to any value  Logical.
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.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Variables Symbol representing a place to store information
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
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.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
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.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Chap. 2. Types, Operators, and Expressions
Tokens in C Keywords Identifiers Constants
Relational Operations
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Lecture 3 Expressions Richard Gesick.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Chapter-3 Operators.
Lectures on Numerical Methods
Chapter 3 Operators and Expressions
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Primitive Types and Expressions
Operator King Saud University
DATA TYPES There are four basic data types associated with variables:
Presentation transcript:

ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 1: Types, Operators and Expressions

ספטמבר 04Copyright Meir Kalech2 The Binary World  Describes the world using only 0 and 1:  open – 1, close – 0  success – 1, failure – 0  true – 1, false – 0 This dual notation representation is called a “ bit ” (short for “ BInary digiT ” ). We can ’ t describe the door ’ s color or the course number with 0/1.  Do you have any idea how to do that?

ספטמבר 04Copyright Meir Kalech3 Great!!! Join two bits together. With two bits we can represent 4 states:  000  011  102  113 Now we can count 4 colors or 4 numbers. But the raised question still remains. The Binary World

ספטמבר 04Copyright Meir Kalech4 JOINING AGAIN?! Join eight bits together – call them a “ byte ”. In eight bits we can represent 256 (2 8 ) states:    … …   But for describing the number of (real ) bugs at my home, this byte is not large enough. The Binary World

ספטמבר 04Copyright Meir Kalech5 YES, JOIN THEM AGAIN! But now join two bytes (not bits) together. Our world contains now 2 16 states, and if it is still not sufficient, join in two more bytes, and then four, etc … This scheme is OK for positive numbers. But how can we represent negative numbers or floating point numbers? The Binary World

ספטמבר 04Copyright Meir Kalech6 Negative numbers: Assign the Most Significant Bit (MSB) as the sign of the number, and use the remaining bits to represent the number itself. Each number could be formed in one of 2 notations: 1.Normal binary notation. 2.2 ’ s complement notation. In 2 ’ s complement notation:  If the MSB is 0 then the number is positive.  If the MSB is 1 then the number is negative. Negative Numbers

ספטמבר 04Copyright Meir Kalech7 The following byte represents a certain number. Which number is it? There are 2 options here: 1.If the number is kept in the normal binary notation, it is If the number is kept in the 2's complement notation, it is  How do I know this? Negative Numbers

ספטמבר 04Copyright Meir Kalech8 1.Let sum the bits from the MSB to LSB in the following form: 1*2 7 +0*2 6 +0*2 5 +0*2 4 +0*2 3 +1*2 2 +0*2 1 +1*2 0 = If the number is kept in the 2's complement notation, we complement and add 1 to get its complement value: Negative Numbers not (123)

ספטמבר 04Copyright Meir Kalech9 What is the scope of each of the above notations? Normal binary notation: to = ((2 0 -1) – (2 8 -1)) 2's complement notation: There is no difference between positive 0 or negative 0. So the (asymmetric) scope is: to = ((2 0 -1) – (2 7 -1)) and to = (-(2 7 ) – -(2 0 )) Negative Numbers

ספטמבר 04Copyright Meir Kalech10 Floating-point representation involves composing the number from 4 parts: 1.The sign of the number. 2.The mantissa ( 0 < mantissa < 1 ). 3.The sign of the exponent. 4.The exponent. Examples: The number’s sign is + The number’s sign is - The mantissa is The mantissa is 0.78 The exponent’s sign is + The exponent’s sign is - The exponent is 2 The exponent is 3 Floating Point Numbers

ספטמבר 04Copyright Meir Kalech11 The figure below describes the general principle of floating-point representation (details might vary between different operating systems). One way to represent floating point is: sign mantissa sign exponent In this (single float) case, 32 bits (4 bytes) are used. Floating Point Numbers 1 bit 23 bits 1 bit 7 bits

ספטמבר 04Copyright Meir Kalech12 It ’ s the programmer ’ s responsibility to know: how many bytes are needed for the correct representation of the number. which data type is needed for this representation. Examples: If the program gets a student ’ s grade as input from the user, the programmer needs to represent a positive number between 0 to 100. One byte is enough for this. If the program gets a customer account status from the user, say of values between to 250, the programmer can use a floating point number. 4 bytes should be enough. Programmer ’ s Responsibility

ספטמבר 04Copyright Meir Kalech13 How could we define to the program the requested number of bytes and data type? The program uses keywords. Each keyword has a role in the program environment. The program has keywords for the pre-defined types. How to request this?

ספטמבר 04Copyright Meir Kalech14 unsigned char -1 bytefor characters char -1 bytefor characters unsigned short int - 2 bytes for short positive integers short -2 bytesfor short integers unsigned int -2/4 bytesfor positive integers int -2/4 bytesfor integers unsigned long -4 bytesfor large positive integers long -4 bytesfor large integers float -4 bytesfor floating point double -8 bytes for long floating point long double - 10 bytes for longer floating point Common (C) Types

ספטמבר 04Copyright Meir Kalech15 char and unsigned char are set to represent characters. Each character has two representations: 1.Binary 2.ASCII The binary representation is a 8-bit number. The ASCII representation is the “ figure ” of the character. An ASCII table can map between these two representations. Characters

ספטמבר 04Copyright Meir Kalech16 For instance: For the lower-case character ‘ a ’ : Binary: (97) ASCII: ‘ a ’ What is the difference between the number 3 and the character ‘ 3 ’ ? The number 3 is: Binary: (3) ASCII: end of text (ETX) (also the ^C character) The character ‘ 3 ’ is: Binary: (51) ASCII: ‘ 3 ’ Characters

ספטמבר 04Copyright Meir Kalech17 A variable defines an area for storing data. Each has:  name  type  address  value  Examples: char colorint catalog_number Variable definition: type variable_name;  For instance:int grade; Name, type and address are not allowed to change. If value hasn ’ t been defined/assigned, the variable value is garbage. Variables ‘a’‘a’

ספטמבר 04Copyright Meir Kalech18 Memory is an area in the computer that is used to store variables. The compiler is responsible for preparing an execution file and to inform how many bytes in memory are necessary for the execution of the file. The number of bytes needed is also derived from the variables that are defined in the program. Conclusion: variable definition = needed memory area Memory

ספטמבר 04Copyright Meir Kalech19 The C language has portability and reusability characteristics. Portability: the language is not tied down to a certain platform/environment. Reusability: the language enables reuse of code. The pre-processor directive “ #include ” enables including (standard) utility files and user defined files into the program during compilation. This way, the programmer can use utilities that were defined already by other programmers. #include - File Inclusion

ספטמבר 04Copyright Meir Kalech20 C supplies functions that are responsible for input/output. scanf is usually used for input and printf for output. Syntax: scanf( “ % format string ”, &variable name); printf( “ % format string ”, variable name); #include library to use the I/O functions. Example: #include int x; float y; char z; scanf(“%d %f %c”, &x, &y, &z); printf(“x=%d y=%f z=%c\n”, x, y, z); Input/Output (I/O)

ספטמבר 04Copyright Meir Kalech21 In addition to types, C also defines operators. There are various classes of operators: Assignment operators Arithmetic operators Increment/Decrement operators Relational operators Logical operators Operators

ספטמבר 04Copyright Meir Kalech22 Do you like mathematics? If not, CONGRATULATIONS!!! The meaning of the assignment operator in C is definitely different from its meaning in mathematics!!! The assignment operator ‘ = ‘ assigns a given value to a variable. For instance: int x = 5; // assigns 5 into the variable x. x = x+2; /* assigns the current value of x (5) plus 2 into x. Its new value is 7. */ Be careful of:  overflow (rvalue is above the range of lvalue).  underflow (rvalue is under the range of lvalue). Assignment Operators

ספטמבר 04Copyright Meir Kalech23 Arithmetic Operators  +plus (unary and binary)  -minus (unary and binary)  *multiplication  /division  %remainder of division (modulus) The precedence of the *, / and % operators, is higher than that of the binary + and - operators. The statement: x = x + 2; can be written in the form x += 2; The syntax of this assignment operation is: operand1 operator= operand2 Which is identical to: operand1 = operand1 operator operand2

ספטמבר 04Copyright Meir Kalech24 An expression is a combination of operators and operands. An operand may be a variable or constant. The simplest expression is an operand by itself. For instance: > 1 a a = a * (4 > 5) Expressions

ספטמבר 04Copyright Meir Kalech25 Each expression has: Type Value Address Type and value of the expression are derived from the operands ’ type and value. For instance:  5 type is int, because the default of constant integers is int; value is 5.  3.2 type is double, because the default of constant floating point numbers is double; value is 3.2.  3+2 type is int because 3 is int and 2 is int; value is 5. Expressions

ספטמבר 04Copyright Meir Kalech26 And what about 3+2.4? 3 is int and 2.4 is double. What is the type of an expression of int+double? RULE: the operands in an expression should be of the same types. WHAT??? My grade in “introduction to computing” is 60 and my exercise grade is Should I attend the course again? First, we should understand what is conversion. If you understand, you will not have to take the course again. But if not … Expressions

ספטמבר 04Copyright Meir Kalech27 Two types of conversions: 1.Implicit Conversion: Invoked automatically by the compiler when an expression mixes different types. 2.Explicit Conversion (Type Casting): Invoked by the programmer code. Conversion can result in:  Promotion: The type is converted to a “ higher ” type.  Demotion: The type is converted to a “ lower ” type, which may lead to trouble (information loss). A temporal variable is created during the conversion process, that is identical to the original variable to be converted, except its type. Conversions

ספטמבר 04Copyright Meir Kalech28 Comments on the conversion process : The original variable does not change. The temporal variable disappears at the statement end. Example of implicit conversion: double d = 12.5; int a = 3, b = 4; a = b + d; Promotion to make this addition possible:  b int  double Addition of the 2 double types:  b + d Demotion upon this assignment:  b + d double  int (CAUTION!) Conversions

ספטמבר 04Copyright Meir Kalech29 Example of explicit conversion: int a = 7, b = 4; int result; result = (float) a / b; Promotion to make division possible (explicit)  a int  float Promotion to make division possible (implicit)  b int  float Demotion upon assignment  a / b float  int (CAUTION!) Conversions

ספטמבר 04Copyright Meir Kalech30 C provides two (unusual) unary operators for incrementing and decrementing variables by one (1). Prefix Increment/Decrement operators: ++, -- unary-expression: ++ unary-expression -- unary-expression Postfix Increment/Decrement operators: ++, -- postfix-expression: postfix-expression ++ postfix-expression -- Increment/Decrement

ספטמבר 04Copyright Meir Kalech31 Prefix operator: the operand is incremented or decremented by 1 and its new value is the value of the expression. Postfix operator: expression value is the value of the postfix-expression before the increment or decrement operator is applied. Examples: int x = 5, y;  y = ++x;// y and x are 6  y = x++;// y is 5, x is 6 Increment/Decrement

ספטמבר 04Copyright Meir Kalech32 == Equal != Different (not equal) > Greater than < Less than >= Greater or equal than <= Less or equal than == and != have a lower precedence than the others, and all of them have a lower precedence than the arithmetic operators. If the relation is:  true: the relation returns 1.  false: the relation returns 0. Relational Operators

ספטמבר 04Copyright Meir Kalech33 Two examples: int x=2; (x>5)+1; The value of the expression x>5 is false (0). The value of entire expression is 1 (0+1). int a=2, b; ((b = 2) == a); Expression value is true. Relational Operators

ספטמבר 04Copyright Meir Kalech34 Operators that can form more complex expressions. Let a and b be expressions: If the first (left) operand of a logical AND operation is equal to 0, the second (right) operand is not evaluated. If the first (left) operand of a logical OR operation is equal to 1, the second (right) operand is not evaluated. Logical Operators a || ba or b a && ba and b !anot a aba || ba && b!a TTTTF TFTFF FTTFT FFFFT

ספטמבר 04Copyright Meir Kalech35 Example: int a=5, b=3, c=0; Which of the following expressions are TRUE (T)? Which are FALSE (F)? Logical Operators ExpressionEvaluated asIntermediateValue (T or F) a>b || c>b5>3 || 0>3T || FT a>b && c>b a!=2 || !c b<a || b<c && a<c (b<a || b<c) && a<c a || b && c b<=a && (c=1)