Engineering Computing I

Similar documents
Types, Operators and Expressions

Types, Operators and Expressions

CSC 1107: Structured Programming

File Handling in C. EECS 2031 Fall October 27, 2014

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Operators and Expressions:

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

Lecture 02 C FUNDAMENTALS

UNIT- 3 Introduction to C++

Operators in java Operator operands.

Operators. Java operators are classified into three categories:

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

Operators. Lecture 3 COP 3014 Spring January 16, 2018

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

Operators in C. Staff Incharge: S.Sasirekha

Arithmetic Operators. Portability: Printing Numbers

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

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

Java Notes. 10th ICSE. Saravanan Ganesh

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

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

More Programming Constructs -- Introduction

JAVA Programming Fundamentals

COMP327 Mobile Computing Session: Tutorial 2 - Operators and Control Flow

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:

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

C Programming Language (Chapter 2 of K&R) Variables and Constants

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C

Chapter 2: Using Data

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC.

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

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

BSM540 Basics of C Language

Unit 3. Operators. School of Science and Technology INTRODUCTION

Work relative to other classes

JAVA OPERATORS GENERAL

Fundamentals of Programming

Information Science 1

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

The Arithmetic Operators

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

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

Lecture 3. More About C

Operators & Expressions

Expression and Operator

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

LECTURE 3 C++ Basics Part 2

Computer System and programming in C

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

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

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

COMP2611: Computer Organization. Data Representation

Programming in C++ 5. Integral data types

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

SECTION II: LANGUAGE BASICS

Infix to Postfix Conversion

THE FUNDAMENTAL DATA TYPES

C Programming Language. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

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

Java Programming Fundamentals. Visit for more.

Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:

Fundamental of Programming (C)

Programming in C++ 6. Floating point data types

Reserved Words and Identifiers

Positional notation Ch Conversions between Decimal and Binary. /continued. Binary to Decimal

DEPARTMENT OF MATHS, MJ COLLEGE

CHAPTER 3 Expressions, Functions, Output

ME 461 C review Session Fall 2009 S. Keres

Number System. Introduction. Decimal Numbers

3. Java - Language Constructs I

Department of Computer Science

Visual C# Instructor s Manual Table of Contents

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

ARG! Language Reference Manual

Programming I Lecture 7

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

Chapter 3: Operators, Expressions and Type Conversion

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

UNIT 7A Data Representation: Numbers and Text. Digital Data

OBJECT ORIENTED PROGRAMMING

CSCI 2212: Intermediate Programming / C Chapter 15

Fundamental of C programming. - Ompal Singh

Data Types and Variables in C language

UNIT 3 OPERATORS. [Marks- 12]

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

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

Programming. Elementary Concepts

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

Applied Computer Programming

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

Declaration and Memory

Chapter 12 Variables and Operators

Signed Multiplication Multiply the positives Negate result if signs of operand are different

Chapter 7. Additional Control Structures

Transcription:

Engineering Computing I Types, Operators, and Expressions Types Operators Expressions 2 1

2.1 Variable Names Names are made up of letters and digits The first character must be a letter The underscore _ counts as a letter Don t begin variable names with _, reserved library routines Upper and lower case letters are distinct Traditional C practice is to use lower case for variable names, and all upper case for symbolic constants 3 2.1 Variable Names continued At least the first 31 characters of an internal name are significant ifi Keywords like if, else, int, float, etc., are reserved It s wise to choose variable names that are related to the purpose of the variable and that areunlikely to get mixed up typographically 4 2

Different Types Type char int float Description a single byte, capable of holding one character in the local character set an integer, typically reflecting the natural size of integers on the host machine single precision floating point double double precision floating point 5 Other Qualifiers short long short int long int 6 3

Other Qualifiers signed unsigned signed char unsigned int 7 Floating Point We need a way to represent numbers with fractions, e.g., 3.1416 very small numbers, e.g.,.000000001 In binary ±1.xxxxxxx 2 2 yyyy Types float and double in C very large numbers, e.g., 3.15576 10 9 Representation: sign, exponent, significand: ( 1) sign significand 2 exponent more bits for significand gives more accuracy more bits for exponent increases range IEEE 754 floating point standard: single precision: sign bit 8 bit exponent 23 bit significand double precision: sign bit 11 bit exponent 52 bit significand 8 4

Float, double Name binary32 Common name Single precision Base Digits E min E max 2 23+1 126 +127 binary64 Double 2 52+1 1022 +1023 precision 9 Testing Different Sizes Using sizeof() function, write a short program to determine the size of different types of variables on your machine. 10 5

Constants Suffix Prefix Example Description 1234 integer 1.234 double 1.23e 3 double l, L 12345678L long integer u, U 1234U unsigned int ul, UL 12345678UL unsigned long 0 037 octal 0x, 0X 0x10 hexadecimal l, L 0, 0x, 0X long octal/hexadecimal u, U 0, 0x, 0X unsigned octal/hexadecimal ul, UL 0, 0x, 0X unsigned long octal/hexadecimal x character 11 2.3 Constants continued Suffix Prefix Example Description \ \n Special characters \0 \7 Special characters octal \00 \17 Special characters octal \000 \117 Special characters octal \xh \xd Special characters hexadecimal \xhh \x10 Special characters hexadecimal 0 037 octal 0x, 0X 0x10 hexadecimal l, L 0, 0x, 0X long octal/hexadecimal u, U 0, 0x, 0X unsigned octal/hexadecimal ul, UL 0, 0x, 0X unsigned long octal/hexadecimal x character 12 6

Escape Characters 13 Constant Expressions 14 7

String Constants The standard library function strlen(s) returns the length of its character string argument s, excluding the terminal \0. Here is our version: /* strlen: return length of s */ int strlen(char s[]) { int i; while (s[i]!= \0 ) \0) ++i; return i; } 15 enumeration 16 8

Exercise Enumerate Days Of the Week, Sunday being 7 17 Arithmetic Operators unary operations: +, Binary operators: +,, *, / % (modulus) 18 9

Arithmetic Operators continued Precedence Unary Operation +, Binary Operation +,, *, /, % Left Right 19 Relational and Logical Operators Category Operation Relational Operation > >= < <= Relational Operation ==!= Logical Operators && Precedence > >= < <= ==!= &&¹ ¹ Left Right 20 1 evaluation stops as soon as the truth or falsehood of the result is known 10

Relational and Logical Operators Precedence Unary Operation +, Binary Operation +,,, *, /, % Left Right Parentheses are NOT Needed! Parentheses ARE Needed! 21 Type Conversions General Rules C automatically converts a narrower operand operand into a wider one without losing information Expressions that don t make sense, like using a float as a subscript, are disallowed Expressions that t might htlose information, like assigning a longer integer type to a shorter, or a floating point type to an integer, may draw a warning, but they Aare not illegal 22 11

Type Conversions A char is just a small integer! /* atoi: convert s to integer */ int atoi(char s[]) { int i, n; n = 0; for (i = 0; s[i] >= 0 && s[i] <= 9 ; ++i) n = 10 * n + (s[i] 0 ); return n; } S = 235U Iteration 1 s[0]=50 (0)*10 + (50 48) Iteration 2 s[1]=51 (2)*10 + (51 48) Iteration 3 s[2]=53 (2)*10*10 + (3)*10+(53 48) 2*100 + 3*10 +5 = 235 Iteration 4 s[3]= 87 Terminate the Chapter for 2loop 23 24 12

Type Conversions Upper case to Lower Case conversion /* lower: convert c to lower case; ASCII only */ intl lower(int c) { if (c >= A && c <= Z ) return c + a A ; else return c; } c >= 0 && c <= 9 can be replaced by isdigit(c) along with <ctype.h> 25 Type Conversions Exercise Write a program to convert a two digit numerical characters to integer using the isdigit() function 26 13

Type Conversions Implicit arithmetic conversions In general, if an operator like + or * that takes two operands has operands of different types, the lower type is promotedto to the higher typebefore the operation proceeds. If either operand is long double, convert the other to long double. Otherwise, if either operand is double, convert the other to double. Otherwise, if either operand is float, convert the other to float. Otherwise, convert char and short to int. Then, if either operand is long, convert the other to long. 27 Increment and Decrement Operators The increment operator ++ adds 1 to its operand, while the decrement operator subtracts 1 The unusual aspect is that ++ and may be used either as prefix operators (before the variable, as in ++n), or postfix operators (after the variable: n++) If n is 5, then x = n++; sets x to 5, but x = ++n; sets x to 6 28 14

Most Significant bit Bits and Bytes! b7 b6 b5 b4 b3 b2 b1 b0 Least Significant bit Bit = 0 or 1 Nibble = (b3) 3 +(b2) 2 + (b1) 1 + (b0) 0 = 0, 1, 2,.. 15 Byte= (b7) 7 +(b6) 6 + +(b1) 1 + (b0) 0 = 0, 1, 2,.. 255 29 Octal and Hexadecimal Arithmetic Octal Groups of 3 bits, i.e. 0 through 7 Hexadecimal Groups of 4 bits, i.e. 0 through 15 Bytes can be written as 3 decimal, 3 Octal or 2 Hexadecimal digits. 30 15

Bitwise Operators & bitwise AND bitwise inclusive OR ^ bitwise exclusive OR << left shift >> right shift ~ one s complement (unary) 31 Octal and Hexadecimal Arithmetic \034 + \567 =? Convert 213 (decimal) to Binary, Octal and Hexadecimal 0x1AB + 0x67 =? 0x123 >> 5 =? (0xC2 0x 123) & 0x11 =? 32 16

Bitwise Operators n = n & 0177; x = x SET_ON; x = x & ~077 sets to zero all but the low order 7 bits of n sets to one in x the bits that are set to one in SET_ON sets the last six bits of x to zero 33 Assignment Operators and Expressions i= i+ 2 i+= 2 The operator += is called an assignment operator. + * / % << >> & ^ expr1 op= expr2 is equivalent to expr1 = (expr1) op (expr2) 34 17

Conditional Expressions expr1? expr2 : expr3 if (a > b) z = a; else z = b; z = (a > b)? a : b 35 Precedence and Order of Evaluation Unary & +,, and * have higher precedence than the binary forms 36 18