ISA 563 : Fundamentals of Systems Programming

Similar documents
Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Fundamental of Programming (C)

LECTURE 3 C++ Basics Part 2

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Expressions and Precedence. Last updated 12/10/18

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Fundamentals of Programming

Operators. Java operators are classified into three categories:

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Work relative to other classes

More Programming Constructs -- Introduction

JAVA OPERATORS GENERAL

Operators in C. Staff Incharge: S.Sasirekha

Chapter 3. Fundamental Data Types

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

UNIT- 3 Introduction to C++

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Informatics Ingeniería en Electrónica y Automática Industrial

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Operators & Expressions

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

Chapter 3: Operators, Expressions and Type Conversion

A flow chart is a graphical or symbolic representation of a process.

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Unit-2 (Operators) ANAND KR.SRIVASTAVA

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

CT 229. Java Syntax 26/09/2006 CT229

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Information Science 1

DEPARTMENT OF MATHS, MJ COLLEGE

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

SECTION II: LANGUAGE BASICS

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Prof. Navrati Saxena TA: Rochak Sachan

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Operators and Expressions:

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Expression and Operator

C: How to Program. Week /Mar/05

OBJECT ORIENTED PROGRAMMING

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

First of all, it is a variable, just like other variables you studied

UNIT 3 OPERATORS. [Marks- 12]

More about Binary 9/6/2016

Computer System and programming in C

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Chapter 2 - Introduction to C Programming

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

COMP Primitive and Class Types. Yi Hong May 14, 2015

Computers Programming Course 6. Iulian Năstac

Chapter 2: Using Data

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

These are reserved words of the C language. For example int, float, if, else, for, while etc.

INTRODUCTION TO COMPUTER SCIENCE - LAB

Chapter 4: Basic C Operators

Language Fundamentals Summary

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

C Language Summary. Chris J Michael 28 August CSC 4103 Operating Systems Fall 2008 Lecture 2 C Summary

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

ME 461 C review Session Fall 2009 S. Keres

Data Types and Variables in C language

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

Programming in C++ 5. Integral data types

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

CS313D: ADVANCED PROGRAMMING LANGUAGE

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 2

3. Types of Algorithmic and Program Instructions

Arithmetic and Bitwise Operations on Binary Data

THE FUNDAMENTAL DATA TYPES

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Introduction to C. Systems Programming Concepts

Quick Reference Guide

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Programming for Engineers: Operators, Expressions, and Statem

CSC 1214: Object-Oriented Programming

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Basic Assignment and Arithmetic Operators

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Lecture 3 Tao Wang 1

Unit 3. Operators. School of Science and Technology INTRODUCTION

Chapter 7. Expressions and Assignment Statements ISBN

Engineering Computing I

Basics of Java Programming

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Arithmetic Operators. Portability: Printing Numbers

Basic Types and Formatted I/O

Arithmetic Expressions in C

Transcription:

ISA 563 : Fundamentals of Systems Programming Variables, Primitive Types, Operators, and Expressions September 4 th 2008

Outline Define Expressions Discuss how to represent data in a program variable name variable type List and discuss the properties of primitives types List and discuss basic operations on variables

Readings TCPL Section 1.2 Variables and Arithmetic Expressions TCPL Chapter 2 Types, Operators, and Expressions

Expressions A C program is a sequence of statements A C program is a collection of functions and the data those functions operate on The building blocks of statements are expressions: combinations of language key words, function calls, operators, and operands that evaluate to a value

Review: What is a Program? A program is a sequence of instructions that operates on data A C program is a collection of variables and functions that process the data held in those variables Computers process long strings of 1's and 0's need a way to refer to portions of those strings as higher level data objects

Variables

A variable is a container for data 00101001101001 01000100101010... dsalkjdlkajdlk ajdjfjf028f.f. 2q4fqd09024CVv qwe#@fq#(v(gkl jsoive00130100 1290avvvvv8vea eeee0xrrtlkjaa vvvv..eaaf0000 Unstructured Data 345 Michael Patient SSN: 999550000 3.141592658 Structured Data

What exactly is a Variable? A variable is the concept of a piece of structured data that can be accessed (read or modified) via well known, standard rules A variable is NOT JUST the data it contains! A variable also has: a name or identifier that provides a way to refer to it a type that defines its size (how much memory it uses) a location or memory address specifying where the data is stored

Example:: Simple Integer Values Suppose we want to write a program for processing a student's grades Need a variable to hold the total score Variable name: value total_score: 345

Example:: Variable Declaration Declaring a variable is a standard action to let the rest of the program know about a piece of data that will be used. The following program statement declares (that is, tells the computer to set aside a memory location) an integer variable called 'total_score' int total_score; type variable name

Declaring Variables Variables are usually declared at the beginning of the program or function they are used in Variable names can be any combination of letters, numbers, or underscores, but must start with a letter or underscore Variable names should be descriptive; avoid names like 'ab', 'x', 'tmp', etc. Make sure you don't try to name a variable after a reserved word (if, for, while, switch...)

A Simple Statement: Initialization You can initialize a variable when you declare it This gives the variable a definite starting value To initialize a variable, use an assignment statement. An assignment statement computes the value of the expression on the right hand side and stores it in the variable named on the left hand side. = should be read stores the result of int total_score = 0;

Initializing a Variable int total_score = 0; //... //... int main() { //... } C Program total_score: address 1022 1023 1024 1025 1026 1027 value 00000000 Computer Memory

Subtle Point About Variable Names When you program, you see the variable name When the computer executes your program, it actually sees the variable memory address In both cases, the data is used behind the scenes

Types

Variable Types A type is a hint to the computer on how to handle the data contained in or referred to by the variable usually this involves size of the storage allocated There are 4 primitive (basic) types in C: int (regular integer) char (1 character) float (single precision floating point number) double (double precision floating point number)

Type Modifiers Types can be augmented by additional information Some simple type qualifiers are listed below: short (applied to int) long (applied to int) unsigned (only positive values and 0) signed const (specifies that the value can't be changed) We usually drop the 'int' when specifying short or long

showsize Demo

Output (sizes are in # of bytes) [michael@xoren code]$ make showsize gcc -Wall -g -o showsize showsize.c [michael@xoren code]$./showsize sizeof(int) = 4 sizeof(char) = 1 sizeof(long) = 4 sizeof(short) = 2 sizeof(float) = 4 sizeof(double) = 8 sizeof(long double) = 12 [michael@xoren code]$

consttest Demo

Language Operators

Operators Overview You are familiar with many operators from basic math and logic: addition (+), subtraction ( ), multiplication (*), division (/) AND (&&), OR ( ), NOT (!) Operators are basically common functions that take their input and produce some output Common enough to have their own symbols in a programming language (see above)

Operators (cont.) C has many operators Some you are familiar with (see previous page) Some not: mod, bitwise AND, OR, XOR, relational Operators are: unary (take one argument, e.g.,! ) binary (take two arguments, e.g., + */<>==) ternary (take three arguments) Classifications: arithmetic, logic, relational, assignment

Operator Context Warning! Operators are represented by symbols. Sometimes, the symbols may mean something completely different based on context. For example: int x = -1; // the '-' operator is negation int x = 4-3; //the '-' operator is subtraction

Arithmetic Operators Addition is represented by + e.g., sum = x+y; Subtraction is rep. by e.g., diff = x y; Multiplication is rep. by * e.g., scale = x * y; Division is rep. by / e.g., quotient = x/y; Modulus is rep. by % e.g., remainder = x % y;

Relational Operators Assignment operator is =, e.g., int sum = x; Equality operator is ==, e.g., is_equal = (x==y); Not equal is expressed by!=, not = (x!=y); Less than: < Greater than: > Less than or equal to <= Greater than or equal to >=

Logical Operators AND: (x&&y) OR: (x y) NOT: (!x)

Bitwise Operators Like logical operators, but operate on the individual bits of a variable, not the whole logical value int x = 2; int y = 1; int r = x y; printf( r is: %d,r); int x = 2; int y = 1; int r = x y; printf( r is: %d,r); Output is: r is: 1 Output is: r is: 3

Bitwise Operators bitwise OR: bitwise AND: & bitwise XOR: ^ one's complement: ~ left shift: << right shift: >>

Order of Operations PEMDAS (power, exponent, mul, div, add, sub) For everything else, use parenthesis () to say what you mean There are other rules. Learn them at your leisure while using the above two. See table 2.1 in TCPL (page 53)

Type Conversions (TCPL, 2.7) Read it. Key question is of the form: when I {add, sub, mul, div, mod...} an {int, float, long,...} {with, from, by,...} a {float, double, long, int...} what happens? Intermediate results are converted according to a set of rules. Basic rule is that results are automatically graduated to the type of the larger operand.

Casting casting is the process of forcing a type conversion Below, the integer value in sum is changed into a double type before being used, as is the result of the average score calculation. int n = 100; int sum = getsum(); double d = ((double)sum); double average = (double)sum/n;

Things We Haven't Covered Here Increment and decrement operators Assignment operators The ternary condition operator Short circuit boolean evaluation The nuances of type conversion Collections of data types and variables (arrays, next session)