 All C programs are made up of functions that perform operations on variables.  In this lecture we examine variables  Variables are the basic building.

Slides:



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

IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Assembly Language and Computer Architecture Using C++ and Java
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.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Chapter 2 Data Types, Declarations, and Displays
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.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
String Escape Sequences
Basic Elements of C++ Chapter 2.
CPS120: Introduction to Computer Science Lecture 8.
1 The Design of C: A Rational Reconstruction Jennifer Rexford.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Computers Organization & Assembly Language
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Number Systems Spring Semester 2013Programming and Data Structure1.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Chapter 2: Using Data.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
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.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Bit Operations Horton pp Why we need to work with bits Sometimes one bit is enough to store your data: say the gender of the student (e.g. 0.
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.
Primitive Variables.
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 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
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.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
INTRODUCTION TO ‘C’ PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.
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 Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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.
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.
Number Systems. The position of each digit in a weighted number system is assigned a weight based on the base or radix of the system. The radix of decimal.
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.
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.  Algorithm: 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x PI 3. Display the area 2.
CSE 220 – C Programming Bitwise Operators.
Chapter 12 Variables and Operators
Chap. 2. Types, Operators, and Expressions
Tokens in C Keywords Identifiers Constants
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
By: Syed Shahrukh Haider
Operators and Expressions
Chapter 12 Variables and Operators
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Basics of ‘C’.
Lectures on Numerical Methods
Expressions and Assignment
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Chapter 12 Variables and Operators
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

 All C programs are made up of functions that perform operations on variables.  In this lecture we examine variables  Variables are the basic building blocks of a program  Variables have: types, names, and constants  Operations performed on variables using operators  Each operation forms an expression  A statement is a collection of expressions terminated by ; 2

 C is a “typed” language.  We must explicitly define variables with a specific type.  Different types have different characteristics.  Size: how many bytes of memory a variable occupies.  Representation: what does the bit-pattern mean.  Usage: what operations may be performed a variable.  By specifying types, we enable the compiler to perform a number of tasks for us:  Compiler allocates fixed amount of memory for a given type.  A variable’s type determines what value it can represent and what operations may be performed on it.  Compiler can detect type-mismatch errors. 3

 Names are composed of alphanumeric characters (letters and numbers), and _ (underscore)  Names may not start with a number  Names are case sensitive; eg, step and Step are different variables  Good Style : By convention, variable names begin with a lower-case letter. Short names are given to local variables, while longer more descriptive names to distant variables. Local: step, top, i, j, x, fahr, flag Distant: lookup_table_index, nameListHead 4

 C has three basic data types 1. Integer types: char, int 2. Floating point types: float, double 3. No type (an empty set): void  Different types have different properties:  Values they can represent  Operations that can be performed on them.  These types have finite precision and range.  There is a limit to the size of a number (min, max)  Floating point values have a limit to the number of significant figures.  Size of int may be qualified by long or short 5

 Basic building blocks of digital systems.  1 Bit = 1 B inary Dig it  Stores 1/0, True/False  1 Byte = 8 Bits 6

 The standard does not specify exact sizes. The amount of memory storage allocated for a particular type will be different on different systems.  ISO C standard requires only that  char is at least 8 bits  short is at least 16 bits  long is at least 32 bits  short <= int <= long  Range of permissible values of integers is defined in limits.h 7

 ISO C does not specify the size of floating point types, or even that their sizes are different.  Simply says: float <= double <= long double  Range of permissible values of integers is defined in float.h 8

signed  explicitly tells the compiler that the quantity is a signed integer (ie, the type may represent a negative number). unsigned  explicitly tells the compiler that the quantity is an unsigned integer (ie, cannot represent a negative number). This doubles the size of the max representable number. 9

 An integer value is represented by a sequence of bits: base 2 numbers.  For a signed integer, the most-significant-bit (MSB) is the sign bit.  If MSB is 0, the number is positive. If 1, the number is negative (using a 2’s complement binary representation).  An unsigned integer does not have a sign bit, and the MSB is part of the actual number – extending the maximum possible value by a factor of 2. Examples for 8-bit integers: > 7 (signed or unsigned) > -121 (signed, 2’s complement) > 135 (unsigned) 10

 signed char, -128 to 127, (-2 7 to 2 7 – 1)  unsigned char, 0 to 255, (2 8 – 1)  signed short, to 32767, (-2 15 to 2 15 – 1)  unsigned short, 0 to 65535, (2 16 – 1)  int, -2,147,483,648 to 2,147,483,647, (-2 31 to )  unsigned, 0 to 4,294,967,296, ( )  float, 1.2e-38 to 3.4e+38, (6 decimal digits precision)  double, 2.2e-308 to 1.8e+308, (15 decimal digits precision)  All are available in limit.h and float.h 11

const  indicates that a variable is intended to remain constant and should not be changed. const int DoesNotChange = 5; DoesNotChange = 6; /* will not compile */ 12

 There are many ways to represent the same integer value  Decimal, 12  Octal, 014  Hexadecimal, 0x0C  All represent the same binary value,  Consider the following 8-bit binary number binary: decimal: = 106 hex: 6 and 10 = 0x6A  a long constant is written like L or l  for an unsigned integer, add the suffix U or u  for an unsigned long integer, add the suffix UL or ul 13

 Specify by a decimal point and/or e or E e-3  Are type double by default  Can specify type float by appending an f 12.5f 7.F 14

 All data is stored as sets of bits.  2 main questions:  How do we represent letters?  Convert to a number: e.g. ‘A’ = 1; ‘B’ = 2 etc.  How do we communicate between different systems?  Use a standard conversion table. 15

 Standard method for representing characters as integers. 16

 Character constants are simply integers  To represent characters, C uses a lookup table  Each character has a unique integer code  ASCII table is most common code  Eg., Letter “R” we can represent by  Its ASCII integer code: 82 or 0122 or 0x52  Or, use character constant: ‘R’  The type of ‘R’ is an int not a char  Some ASCII constants are defined as escape sequences (for characters that are difficult or impossible to type) \n new line \t tab \b backspace \0 NUL character 17

 A string is a sequence of char s terminated by a NUL character, ‘\0’.  A string constant is written as e.g. “this is a string”  A string constant is automatically terminated by a NUL e.g. |t|h|i|s| |i|s| |a| |s|t|r|i|n|g|\0| WARNING!  Don’t confuse a character constant ‘X’ which is an int with a (null-terminated) string constant “X” which is an array of two char s: |X|\0|  Thus, sizeof(‘X’) is 4 (for Win32) and sizeof(“X”) is 2 18

 In printf(), use conversion specifiers, prefixed by % %c character - char %d decimal int %x hexadecimal int %o octal int %f %e %gfloat %f %e %gdouble  Conversion specifiers MUST match the type of the variable it corresponds to, else the program will be incorrect. 19

 See Text Section 2.5 and 13.1 for more information.  Refer to any C textbook for more complete discussion on printf() and its conversion specifiers. 20

 Extremely bad practice to have “magic numbers” in code. It may be difficult to see what the number stands for, and code changes become error-prone.  Use #define to define named constants, all in the one place #define ARRAY_LENGTH2500 #define BLOCK_SIZE4096 #define TRACK_SIZE 16*BLOCK_SIZE #define STRING“Hello World!\n”  Symbolic constants mean making changes of constants is easy and safe.  Example: tempf2.c 21

 Arithmetic operators are + plus - minus * multiply / divide = assignment % modulus (remainder after division)  The first 5 are valid for integer and floating-point types.  The % is valid only for integer types (including char ). 22

3.0 / 5.0 – equals / 5 – integer division truncates, equals 0 17 / 6 – equals 2 18 % 7 - equals 4 2*7 + 5*9 – equals : 59  Show prime.c 23

 Precedence and order of evaluation. eg, a + b * c  Order of evaluation from left to right.  *, / and % take precedence over + and -, so that a + b * c is the same as a + (b * c)  Precedence table exists, but use brackets () instead for safety!! 24

 Overflow (integers have finite range) y = x + 1; z = x * y;  Overflow and signed/unsigned  unsigned: modulo wrap around  signed: undefined behaviour  Divide by zero (integers or floats) z = x / y; 25

 Increment ++ and decrement -- ++a is equivalent to a = a + 1  Valid operators on integer or floating-point numbers.  Two forms: preincrement and postincrement int a=2, b, c; b = ++a; /* a=3 and b=3 */ c = a++; /* a=4 and c=3 */ 26

 Relational operators are > greater-than < less-than >= greater-than-or-equal-to <= less-than-or-equal-to == equal-to != not-equal-to  These operators are valid for integer and floating-point types.  Evaluate to 1 if TRUE, and 0 if FALSE 3.2 < 7 equals 1, and x != x equals 0 27

 Logical operators are && AND || OR ! NOT  && and || connect multiple conditional expressions.  ! negates a conditional expression (non-zero becomes 0, zero becomes 1). 28

int a=1, b=2, c=3, d=3; a < b && b < c && c < d /* FALSE */ a < b && b < c && c <= d /* TRUE */ (a < b && b < c) || c < d /* TRUE */ a && !b /* FALSE */  Show logical.c and leapyear.c  && and || are evaluated left-to-right and, once the result of TRUE or FALSE is known, evaluation stops – leaving the remaining expressions unevaluated. This is a useful feature, and leads to several common C idioms. i = 0; while(i < SIZE && array[i] != val) ++i; 29

 Bitwise operators are & bitwise AND | bitwise OR ^ bitwise XOR << left shift >> right shift ~ one’s complement (bitwise NOT)  Used to manipulate individual bits.  Details in later lecture, mention here to avoid confusion with logical operators. They cannot be used interchangeably.  & is not &&, | is not ||, >> is not “much-greater-than”. 30

 Assignment operators - for example, a += b; is equivalent to a = a + b; x *= y+1; is equivalent to x = x * (y+1);  Assignment also with other arithmetic operators : +,-,*,/,%  Show factorial.c 31

 One can write expressions with variables and constants of different types  The compiler performs implicit conversions on terms so each binary expression has same (larger) type.  (see Section 2.11 of lecture notes for more details) int x = 2, y = 1; float z = 5.4; double a; a = y + x*z;  Type promotion occurs stepwise for each binary expression 1. (tmp1) = x*z, the variable x is promoted to float; and result stored as a float 2. (tmp2) = y+(tmp1), the variable y is promoted to float 3. a = (tmp2), the implicit temporary is promoted to double and result stored in a 32