Basic Assignment and Arithmetic Operators

Similar documents
Programming & Data Structure: CS Section - 1/A DO NOT POWER ON THE MACHINE

Computer System and programming in C

IEEE 754 Floating-Point Format

Fundamentals of Programming

Fundamental of Programming (C)

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

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

Introduction to C. Systems Programming Concepts

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

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

Arithmetic Expressions in C

Lecture 3. More About C

Chapter 4: Basic C Operators

Operators and Expressions:

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

Programming and Data Structures

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

1.3b Type Conversion

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

IEEE 754 Floating-Point Format

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

Operators. Lecture 3 COP 3014 Spring January 16, 2018

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen. Introduction

LECTURE 3 C++ Basics Part 2

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

COP 3275: Chapter 04. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

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

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Data Types and Variables in C language

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

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

ISA 563 : Fundamentals of Systems Programming

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-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

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

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

The C language. Introductory course #1

C Programming

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

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

Data types, variables, constants

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

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

Unit 3. Operators. School of Science and Technology INTRODUCTION

The Arithmetic Operators

2. Numbers In, Numbers Out

Programming in C++ 5. Integral data types

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Basics of Programming

Data Type Fall 2014 Jinkyu Jeong

Structured programming

2. Numbers In, Numbers Out

Chapter 3. Section 3.10 Type of Expressions and Automatic Conversion. CS 50 Hathairat Rattanasook

Chapter 2: Using Data

Structured programming. Exercises 3

Arithmetic Operators. Portability: Printing Numbers

More Programming Constructs -- Introduction

Unit-2 (Operators) ANAND KR.SRIVASTAVA

JAVA OPERATORS GENERAL

Add Subtract Multiply Divide

Programming for Engineers Iteration

Operators and expressions. (precedence and associability of operators, type conversions).

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

Operators in C. Staff Incharge: S.Sasirekha

3. Java - Language Constructs I

Arithmetic type issues

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

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

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

Work relative to other classes

Declaration and Memory

COMP2611: Computer Organization. Data Representation

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

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Chapter 3 Structure of a C Program

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Chapter 3 Structured Program Development in C Part II

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

Lecture 3 Tao Wang 1

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

CIS133J. Working with Numbers in Java

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

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

Fundamentals of Programming Session 7

Part I Part 1 Expressions

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Engineering Computing I

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

C: How to Program. Week /Mar/05

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Operators and Expressions

Chapter 2: Using Data

CHAPTER 3 Expressions, Functions, Output

Operators & Expressions

DEPARTMENT OF MATHS, MJ COLLEGE

Programming Language A

Transcription:

C Programming 1 Basic Assignment and Arithmetic Operators

C Programming 2 Assignment Operator = int count ; count = 10 ; The first line declares the variable count. In the second line an assignment operator (=) is used to store 10 in the location of count.

C Programming 3 In C language count = 10 is called an expression (with side effect) where = is called the assignment operator (not equality). Value of the expression is 10. The semicolon ; converts the expression to a statement.

C Programming 4 Next statement - count = 2*count + 5; In this statement the variable count is used on two sides of the assignment operator. There are two constants 2 and 5, and three operators = (assignment), (multiplication) and + (addition). count = 2*count + 5 is an expression (excluding the semicolon).

C Programming 5 The informal semantics (action, meaning) of the previous expression is the following: The content (r-value) of count is multiplied by 2, then 5 is added to the result (10 2+5 is 25). The final value 25 is stored in the location (l-value) of count. The value of the expression is 25.

C Programming 6 int count; garbage count int count = 10 ; 10 count count = 2*count + 5; count 25

C Programming 7 Assignment from One Type to Another A float data can be assigned to an int variable, but there may be loss of precession. Similarly an int data can also be assigned to a variable of type float; but again there may be loss information. These processes are called type casting.

C Programming 8 7 int count 7.5 2.147484e+09 float cgpa 2147483647

C Programming 9 #include <stdio.h> int main() // temp1.c { int count = (int) 7.5 ; float cgpa = (float) 2147483647; printf("count: %d\n", count); printf("cgpa: %e\n", cgpa); return 0 ; }

C Programming 10 $ cc -Wall temp.c $./a.out count: 7 cgpa: 2.147484e+09 Output

C Programming 11 Note Type casting is not a simple operation due to the difference in internal representations of int and float. In case of (int)7.5, the fractional part is removed and the value is 7 in 32-bit integer representation (2 s complement form). The value of (int)0.75 is zero (0).

C Programming 12 Note In the second case (float) 2147483647 converts the integer to IEEE 754 floating-point representation. In this format the number of bits available for storing the significant digits is 23, and all the digits of 2147483647 cannot be stored (2 23 = 8388608). So there will be a loss of precision.

C Programming 13 Five Basic Arithmetic Operators +,,, /, % The first four operators have their usual meaning - addition, subtraction, multiplication and division. The last operator % extracts the remainder a. It may be called the mod operator. a a%b - the first operand a should be non-negative integer and the second operand b should be a positive integer.

C Programming 14 #include <stdio.h> int main() // temp2.c { printf("0%%10 = %d\n", 0%10); printf("4%%10 = %d\n", 4%10); printf("10%%4 = %d\n", 10%4); printf("-10%%4 = %d\n", -10%4); return 0 ; }

C Programming 15 $ cc -Wall temp2.c $./a.out 0%10 = 0 4%10 = 4 10%4 = 2-10%4 = -2 Output

C Programming 16 Operator Overloading The first four operators +,,,/ can be used for int, float and char data a. But the fifth operator cannot be used on float data. a The actual operations of addition, subtraction etc. on int and float data are very different due to the difference in their representations.

C Programming 17 Mixed Mode Operations Mixed mode operations among int, float and char data are permitted. If one operand is of type float and the other one is of type int, the int data will be converted to the closest float representation before performing the operation.

C Programming 18 #include <stdio.h> int main() // temp3.c { int n = 4 ; float a = 2.5 ; char c = a ; // ASCII value 97 printf("%d*%f = %f\n", n, a, n*a); printf("%d*%f+%c = %f\n",n,a,c,n*a+c); return 0 ; }

C Programming 19 Output $ cc -Wall temp3.c $./a.out 4*2.500000 = 10.000000 4*2.500000+a = 107.000000

C Programming 20 Computer Arithmetic! 1 3 30.0 = 0.0 One should be careful about the division operation on int data.

C Programming 21 #include <stdio.h> int main() // temp4.c { printf("1/3*30.0=%f\n", 1/3*30.0); return 0 ; }

C Programming 22 $ cc -Wall temp4.c $./a.out 1/3*10.0=0.000000 Output

C Programming 23 #include <stdio.h> int main() // temp4a.c { printf("10.0*1/3=%f\n", 10.0*1/3); printf("10.0*(1/3)=%f\n", 10.0*(1/3)); return 0 ; }

C Programming 24 $ cc -Wall temp4a.c $ a.out 10.0*1/3=3.333333 10.0*(1/3)=0.000000 Output

C Programming 25 Computer Arithmetic! 2147483647+1 = 2147483648 Addition may give wrong result due to range overflow.

C Programming 26 #include <stdio.h> int main() // temp14.c { int n = 2147483647; printf("n+1: %d\n", n+1) ; return 0 ; }

C Programming 27 $ cc -Wall temp14.c $./a.out n+1: -2147483648 Output

C Programming 28 Computer Arithmetic! 10 5 +10 41 = 10 5 There will be loss of precession in floating point arithmetic.

C Programming 29 #include <stdio.h> int main() // temp8.c { float a = 1.0e-40, b = 1.0e+5, c; c = a+b ; printf("%e + %e = %e\n", a, b, c); if(b == a+b) printf("equal\n"); else printf("not Equal\n"); return 0 ; }

C Programming 30 Output $ cc -Wall temp8.c $ a.out 9.999946e-41 + 1.000000e+05 = 1.000000e+05 Equal $

C Programming 31 Precedence and Associativity All these five operators +,,,/,% are left-to-right associative.,/,% have same precedence and it is higher than +, which also have the same precedence.

C Programming 32 = is Right Associative int count = 10, n ; n = count = 2*count + 5 ; The variable n gets the updated value of count i.e. 25.

C Programming 33 Precedence of = The precedence of assignment operator(s) is lower than every other operator except the comma, operator.

C Programming 34 Computer Arithmetic! 1.3 float a = 1.3;

C Programming 35 #include <stdio.h> int main() // temp5.c { float a = 1.3 ; } if(a == 1.3) printf("equal\n") ; else printf("not equal\n") ; return 0 ;

C Programming 36 $ cc -Wall temp5.c $./a.out Not equal Output

C Programming 37 Error Division of int data by zero gives error at run time a. But the division of float or double data by zero does not generate any run time error. The result is inf b. a GCC error message is funny. b This value can be used.

C Programming 38 #include <stdio.h> int main() // temp9.c { int n = 10, m ; printf("enter an integer: "); scanf("%d", &m); printf("n/m: %d\n", n/m); return 0 ; }

C Programming 39 Output $ cc -Wall temp9.c $./a.out Enter an integer: 0 Floating point exception

C Programming 40 #include <stdio.h> #include <math.h> int main() // temp10.c { float n = 10.0, m, r ; printf("enter a number: "); scanf("%f", &m); printf("n/m: %f\n", r = n/m); printf("atan(%f) = %f\n", r, atan(r)); return 0 ; }

C Programming 41 Output $ cc -Wall temp10.c -lm $ a.out Enter a number: 0 n/m: inf atan(inf) = 1.570796

C Programming 42 Integer Character If a char data is assigned to an int type variable, the ASCII value of the data is stored in the location. But if an int data (32-bit size) is assigned to a char type variable (8-bit size), the least significant 8-bits of the data are stored in the location.

C Programming 43 int count ; char grade ; 67 C count A grade 1345

C Programming 44 #include <stdio.h> int main() // temp6.c { int count = C ; char grade = 1345 ; printf("count: %d, grade: %c\n", count, grade) ; return 0 ; }

C Programming 45 Output $ cc -Wall temp6.c temp6.c: In function main : temp6.c:5: warning: overflow in implicit constant conversion $./a.out count: 67, grade: A

C Programming 46 Note The ASCII code for C is 67 and that is stored in the location for count. The internal representation of 1345 is 0000 0000 0000 0000 0000 0101 0100 0001. The decimal value of the least significant byte (8-bit) 0100 0001 is 65, the ASCII code for A.

C Programming 47 Pre and Post Increments int count = 10, total = 10 ; ++count ; total++ ;

C Programming 48 Both ++count and total++ are expressions with increment operators. The first one is pre-increment and the second one is post-increment. At the end of execution of the corresponding statements, the value of each location is 11. But the value of the expression ++count is 11 and that of the total++ is 10.

C Programming 49 Pre and Post Decrements int count = 10, total = 10 ; --count ; total-- ; Similarly we have pre and post decrement operators.

C Programming 50 More Assignment Operators int count = 10, total = 6 ; count += 5*total ; The new value of count is 10+5 6 = 40. count += 5*total; is equivalent to count=count+5*total;.

C Programming 51 More Assignment Operators But count *= 5+total; is not same as count = count*5+total; In the first case the value is 10 (5+6) = 110. But in the second case it is 10 5+6 = 56.

C Programming 52 #include <stdio.h> int main(){ // temp7a.c int count = 10, total = 6; } printf("first val: %d\n", count *= 5+total); count = 10; total = 6; printf("sec. val: %d\n", count = count*5+total); return 0;

C Programming 53 Unary + and The unary and + have their usual meaning with higher precedence than,/,%.