These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.

Slides:



Advertisements
Similar presentations
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
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.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
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,
Chapter 2 Data Types, Declarations, and Displays
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
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.
String Escape Sequences
Basic Elements of C++ Chapter 2.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
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)
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
C Tokens Identifiers Keywords Constants Operators Special symbols.
CPS120: Introduction to Computer Science
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
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.
Copyright © – Curt Hill Types What they do.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
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.
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.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
1 Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
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.
Rational Expressions relational operators logical operators order of precedence.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
1 Chapter 1: Basic Concepts Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine 9/6/2003.
Chapter 2 Variables.
The Machine Model Memory
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 12 Variables and Operators
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 6: Data Types Lectures # 10.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
ITEC113 Algorithms and Programming Techniques
Basic Elements of C++.
EPSII 59:006 Spring 2004.
Multiple variables can be created in one declaration
Basic Elements of C++ Chapter 2.
Chapter 12 Variables and Operators
Introduction to Abstract Data Types
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Comp Org & Assembly Lang
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Chapter 2 Variables.
Variables and Constants
Review of Java Fundamentals
Programming Fundamental-1
Chapter 12 Variables and Operators
Presentation transcript:

These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson University. The notes have been modified for CpSc These notes introduce the C programming language, outline the development of a program called a ray tracer that produces a photo-realistic image of a virtual world, demonstrates standard program development methodology for large programs, and introduces basic tools for program development in a Unix environment. This version was first modified by Dr. Wayne Madison for the Spring 2007 offering of CpSc 210, later modified by Dr. Rose Lowe for the Spring 2013 offering of CpSc 210, and most recently modified by Dr. Lowe for CPSC Course Notes For CPSC 2100

Memory A one dimensional array of individually addressable storage elements. Each element, called a byte, holds 8 binary digits (bits) of information. It is extremely important to understand and be able to distinguish o the address of a storage element o the contents of a storage element The Machine Model

Memory the address of a storage element o Addresses begin at 0 and increase in unit steps to N-1 where N is the total number of bytes in the address space. A pointer variable holds an address. the content of a storage element o Since each byte consists of only 8 bits, there are only 256 different values that can be contained in a storage element. These range from binary to binary , which corresponds to decimal numbers 0 to 255. The Machine Model

Aggregation of basic memory elements More than 8 bits are needed for useful application to numerical problems. Thus it is common to group adjacent bytes into units commonly called words. Multiple word lengths are common, and common word lengths include 2 bytes, 4 bytes, and 8 bytes. In some architectures (Sun Sparc) it is required that the address of each word be a multiple of word length. That is, the only valid addresses of 4-byte words are 0, 4, 8, 12, 16,...). The Machine Model

The Basic Block - { declaration of variables executable code } Historically, unlike in Java and C++, all variable declarations must precede the first line of executable code within the block. With newer compilers this restriction may not be true, but in any case, scattering variable declarations throughout a program has adverse effects on the readability and maintainability of a program. Nesting of blocks is legal and common. Each interior block may include variable declarations. C Program Structure

Declaration of variables Two generic types of basic (unstructured) variables exist: integer floating point Integer variables may be declared as follows: char a; /* 8 bits */ short b; /* (usually) 16 bits */ int c; /* (usually) 32 bits */ long d; /* 32 or 64 bits */ long long e; /* (usually) 64 bits */ These declarations implicitly create signed integers. An 8 bit signed integer can represent values in the range [-2 7,...0, ] Signed integers are represented internally using 2's complement representation.

Declaration of variables Unsigned integers Each of the preceeding declarations may also be preceded by the qualifier unsigned. unsigned char a; /* 8 bits */ unsigned short b; /* (usually) 16 bits */ An 8 bit unsigned integer can represent values in the range [0, ] In all modern computer systems different hardware instructions are used for signed and unsigned arithmetic.

Encoding of integer constants: Integer constants may be expressed in several ways decimal number 65 hexadecimal number 0x41 octal number 0101 ASCII encoded character 'A' ALL of the above values are equivalent ways to represent the 8 bit byte whose value is:

Encoding of integer constants: Constants of different representation may be freely intermixed in expressions. x = 11 + 'b' '\t'; x = 0xb + 0x62 - 0x2d + 0x9; x = ; x = 73;

Floating point data Two variations on floating point variables float 32 bits double 64 bits Example float a, b; double c; Floating point constants can be expressed in two ways Decimal number Scientific notation e+3 avogadro = e+23;

Executable code Expressions: consist of (legal combinations of): constants variables operators function calls Operators Arithmetic: +, -, *, /, % Comparative: ==, !=,, >= Logical: !, &&, || Bitwise: &, |, ~, ^ Shift: >

Executable code Special types of expression A statement consists of an expression followed by a semicolon An assignment expression consists of lvalue = expression; lvalue is short for "left-value", which in turn represents any entity that may legitimately be assigned a value: The two most common examples are: A simple variable A pointer dereference

Executable code Warnings: Non-assignment statements (with the exception of function calls) are syntactically legal but (generally) semantically useless: x + y - 3; x <= 2; Use parentheses to avoid problems with: operator precedence (the order in which the operators are evaluated) y = x + 5 & z - 5; and operator associativity (the direction in which operations of equal precedence are evaluated) y = 10 / 5.0 * 4.0

Executable code Control flow: if and while if (expression) statement | basic-block <--- Executed if expression value is true else statement | basic-block <--- Executed if expression value is false while (expression) statement | basic-block <--- Executed while expression remains true do <--- Executed until expression becomes false statement | basic block while (expression);

Executable code Control flow: if and while There is no boolean type in C. An expression is false its value is 0 If the expression is an assignment, the value of the expression is the value that is assigned. Be particularly careful not to confuse the following: if (x = (a + b)) if (x == (a + b)) Be careful not to accidentally use: while (x < y);

Executable code Function definitions A C function is comprised of 4 components 1.the type of value returned by the function 2.the name of the function 3.parenthesized declaration of function parameters 4.at least one basic block containing local variable declarations and executable code int main( int argc, /* Number of cmd line parms */ char *argv[]) /* Array of ptrs to cmd line parms */ { --- basic block --- }