Declarations and Expressions C and Data Structures Baojian Hua

Slides:



Advertisements
Similar presentations
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
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.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
C++ Basics Prof. Shermane Austin. Learning Programming Language Basics Data Types – simple Expressions Relational and Logical Operators Conditional Statements.
Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
Pointers and Arrays C and Data Structures Baojian Hua
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,
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 2 Data Types, Declarations, and Displays
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
Data Types.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Basic Elements of C++ Chapter 2.
Performing Simple Calculations with C# Svetlin Nakov Telerik Corporation
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.
Computer Science 210 Computer Organization Introduction to C.
Objectives You should be able to describe: Data Types
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.
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.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
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.
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.
CPS120: Introduction to Computer Science
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Basic Elements of C++ Chapter 1.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Primitive Variables.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 1.
CPS120: Introduction to Computer Science Variables and Constants.
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.
Characters and Strings
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.
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.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
1 C++ Programming C++ Basics ● C++ Program ● Variables, objects, types ● Functions ● Namespace ● Tests ● Loops ● Pointers, references.
Chapter Topics The Basics of a C++ Program Data Types
Chap. 2. Types, Operators, and Expressions
Java Primer 1: Types, Classes and Operators
Tokens in C Keywords Identifiers Constants
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Lectures on Numerical Methods
elementary programming
Chapter 3 Operators and Expressions
Data Types and Expressions
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Basics Data Types in Java.
In this class, we will cover:
Variables and Constants
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Declarations and Expressions C and Data Structures Baojian Hua

Overview Variable declaration the way to introduce variables into programs Operators do some useful operations on variables and constants Expressions form complex expressions with variables, operators, constants and other expressions

Variables Variable formation rules: consists of one or more letter or digit, ( “ _ ” counts as a letter) starts with a letter lower and upper cases are distinct different from key words It ’ s common practice not to start with “ _ ” variable names in C library Choose informative variable names Compare “ sldj_wdwp_ ” with “ name ”

Types and Constants type nameruleexample charnum or char ‘ a ’, 97 intnum or char97, ‘ a ’ longsuffixes: “ L ” or “ l ” 97L, 999l floatsuffixes: “ F ” or “ f ” 97.0f, 2.5e-2F double97.0, 2.5e-2

Types and Constants(2) octalstarts with 005, 013 hexadecimalstarts with 0x0x5, 0x13 escape sequence \ooo \xhh \013 \xb string “…” “ hello, world ” enumenumerationenum boolean { NO, YES };

Variable Declaration C obeys the variable use-after-declaration rule Variable declarations consists of these parts: Type name Variable list (may only one) initialization Ex: int x, y, z; double f; char c = ‘ c ’ ; Type qualifiers: signed, unsigned (default is signed), const

Sample Variable Declaration int low, high; char line[1000]; double pi = 3.14; const double e = 2.71; const char digits[] = “ abcdef”;

Variable Declaration A declaration makes clear a variable ’ s type, size and allocates memory for it int i, j; ?%*

Variable Initialization Programmers ’ duty to initialize that memory space int i, j; i = 9; 9 ?%*

Array Declaration int i, j; i = 9; int a[5]; 9 $? &^ a ?%*

Array Initialization int i, j; i = 9; int a[5]; for (int j=0; j<5; j++) a[j] = j*j; ?%* a

Arithmetic Operators Operator:Example: +x + y -x – y *x * y /x / y %x % y ++ x -- x

Relational and Logical Operators Operator:Example: >x > y >=x >= y <x < y <=x <= y ==x == y !=x != y &&x && y ||x || y !!x

Relational and Logical Operators Boolean operators are short-circuit Computations stop as soon as true or false is known x = 9; if ((3>2) || (x=8)); // what’s x’s value? 8 or 9?

Relational and Logical Operators // another example on && x = 9; if ((1<0) && (x=8)); // what’s x’s value? 8 or 9? // a more fancy example int i = 0; int a[10]; while (i<10 && a[i]!=0){ … }

“!”“!” Not “ ! ” converts 0 to 1, and non-zero values to 0 if (!a){ … } // reads “if a does not hold”, then … // be equivalent with if (0 == a) { … } // we’d see more examples later

Increment and Decrement int n = 9; int x; x = n++; // x == 9 // n == 10 int n = 9; int x; x = ++n; // x == 10 // n == 10

Bitwise Operators Name:Example: &x & y |x | y ^x ^ y <<x << y >>x >> y ~~ x

Bitwise Operators Example // count num of 1’s in an integer x int count (int x) { int num = 0; unsigned int y = (unsigned int)x; while (y) { if (y & 0x1) num++; y >>= 1; } return num; }

Assignment Operators and Expressions Assignment is a special kind of expression, so the following code fragment is legal int x; printf (“%d\n”, x=999); Other kind of assignment expressions are of the form: e1 op= e2 Equivalent to: e1 = e1 op e2 Example: x += 9; ====> x = x + 9; See the text for a complete list of op

Assignment Operators and Expressions // more examples on assignment operators: int a, b, c; a = b = 9; a = (b = 9) + (c = 8); a = (b = 9) / (c = 3); // even as the arguments of function calls: int a, b; x = sum (a = 1, b = 2);

Conditional Expressions if (a > b) max = a; else max = b; // C provides a more succinct way to do this: max = (a>b) ? a : b; // the general form is: e1 ? e2 : e3; // if e1 evaluates to true, then evaluates e2, // else evaluates e3.

Type Conversion (Cast) When the type of some expression is not compatible with the one expected, automatic type conversion occurs Example from our previous code: // all of c, ‘0’ and ‘9’ are automatic converted // into integer values by the compiler: if ((c>=‘0’) && (c<=‘9’)) a[c-’0’]++;

Type Conversion (Cast) Generally, there are two kinds of automatic cast: cast a “ big ” into “ smaller ” ones long ==> int cast a “ small ” into “ bigger ” ones char ==> int The former is not always safe

Type Conversion (Cast) The general scheme for “safe” cast is: doublefloat long unsigned intchar

Type Conversion (Cast) // Dirty simple? However, it’s more subtle than // it first looks. Consider: #include int main () { int i = -1; unsigned int j = 0; if (i < j) printf (“Never reach here, :-( \n”); else printf (“Shoot myself in the foot\n”); return 0; }

Type Conversion (Cast) Don ’ t expect the compiler will always behave as you desire Two general principals: Always use the C ’ s type system as strong as possible It ’ s your basic protection One reason for strong type system ’ s popularity Make use of C ’ s type conversion explicitly To make clear what we are doing

Explicit Type Cast General form of explicit type conversion: (type) expression // which converts the expression’s type to type // More examples: int i = (int) ; char c = (char)i; int a[10]; a[(int)2.71] = 99; As we see, data precision may be changed

Safety Issue Type conversion is an infamous source of C programs bugs especially with pointers (int *)999 will send your passwd to BillG … Any serious and well-designed C program should use type conversion really rarely General principle: do NOT use it, always use the explicit form