Data Types and Variables in C language

Similar documents
Operators and Expressions:

Department of Computer Science

Operators in C. Staff Incharge: S.Sasirekha

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

Constants and Variables

UNIT 3 OPERATORS. [Marks- 12]

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.

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

Operators in java Operator operands.

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

Basics of Programming

Unit-2 (Operators) ANAND KR.SRIVASTAVA

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

Unit 3. Operators. School of Science and Technology INTRODUCTION

JAVA OPERATORS GENERAL

Chapter 3: Operators, Expressions and Type Conversion

Operators & Expressions

UNIT- 3 Introduction to C++

Prepared by: Shraddha Modi

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

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

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

Declaration and Memory

Programming in C++ 5. Integral data types

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

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

Reserved Words and Identifiers

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

Expressions and Precedence. Last updated 12/10/18

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

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

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

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

The Arithmetic Operators

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

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

Operators And Expressions

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

SECTION II: LANGUAGE BASICS

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

Computers Programming Course 6. Iulian Năstac

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

Information Science 1

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

Prof. Navrati Saxena TA: Rochak Sachan

PESIT Bangalore South Campus Hosur Road (1km before Electronic City), Bengaluru Department of Basic Science and Humanities

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Chapter 2: Using Data

P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS 1 ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS)

Java Primer 1: Types, Classes and Operators

Operators. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a)

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 1 IDL Operators

Chapter 7. Additional Control Structures

Operators and Expressions

Programming for Engineers Iteration

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

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

Chapter 4: Basic C Operators

CSC 1214: Object-Oriented Programming

Information Science 1

Review of the C Programming Language for Principles of Operating Systems

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.

3. Java - Language Constructs I

Computer System and programming in C

LECTURE 3 C++ Basics Part 2

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Visual C# Instructor s Manual Table of Contents

Programming in C++ 6. Floating point data types

Arithmetic Operators. Portability: Printing Numbers

Advantages of writing algorithm

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Flow Control. CSC215 Lecture

Chapter 2: Using Data

Computer Programming CS F111

Chapter 7. Expressions and Assignment Statements ISBN

Variables and literals

ME 461 C review Session Fall 2009 S. Keres

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

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

Review of the C Programming Language

Assignment: 1. (Unit-1 Flowchart and Algorithm)

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

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen. Introduction

More Programming Constructs -- Introduction

Fundamentals of Programming

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

ECE 122 Engineering Problem Solving with Java

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

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

Lecture 3. More About C

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

CSC 1107: Structured Programming

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

Part I Part 1 Expressions

Fundamentals of Programming CS-110. Lecture 3

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Expressions & Assignment Statements

Transcription:

Data Types and Variables in C language

Disclaimer The slides are prepared from various sources. The purpose of the slides is for academic use only

Operators in C C supports a rich set of operators. Operators are used in programs to manipulate data and variables. They usually form a part of the mathematical or logical expressions. C operators are classified into a number of categories. They include: 1. Arithmetic operators 2. Relational operators 3. Logical operators 4. Assignment operators 5. Increment and Decrement operators 6. Conditional operators 7. Bitwise operators 8. Special operators

Arithmatic Operators Eg: 1) a-b 2) a+b 3) a*b 4) p%q

Integer Arithmatic When both the operands in a single arithmetic expression are integers, the expression is called an integer expression, and the operation is called integer arithmetic. During modulo division the sign of the result is always the sign of the first operand.that is 14 % 3 = 2-14 % 3 = -2-14 % -3 = -2 14 % -3 = 2

Real Arithmetic An arithmetic operation involving only real operands is called real arithmetic. If x and y are floats then we will have: 1) x = 6.0 / 7.0 = 0.857143 2) y = 1.0 / 3.0 = 0.333333 The operator % cannot be used with real operands.

Mixed-mode Arithmetic When one of the operands is real and the other is integer, the expression is called a mixed-mode arithmetic expression and its result is always a real number. Eg: 1) 15 / 10.0 = 1.5

Relational Operators Comparisons can be done with the help of relational operators. The expression containing a relational operator is termed as a relational expression. T h e v a l u e o f a r e l a t i o n a l expression is either one or zero.

Logical Operators C has the following three logical operators. Eg: if(age>55 && sal<1000) if(number<0 number>100)

Assignment Operators The usual assignment operator is =. In addition, C has a set of shorthand assignment operators of the form, v op = exp; Eg: x += 1; This is same as the statement x = x + 1;

Shorthand Assignment Operator

Increment and Decrement Operators C has two very useful operators that are not generally found in other languages. These are the increment and decrement operator: ++ and -- The operator ++ adds 1 to the operands while subtracts 1. It takes the following form: ++m; or m++ --m; or m

Rules for ++ & -- operators These require variables as their operands When postfix either ++ or - - is used with the variable in a given expression, the expression is evaluated first and then it is incremented or decremented by one When prefix either ++ or - - is used with the variable in a given expression, it is incremented or decremented by one first and then the expression is evaluated with the new value

Conditional Operator A ternary operator pair?: is available in C to construct conditional expression of the form: exp1? exp2 : exp3; Here exp1 is evaluated first. If it is true then the expression exp2 is evaluated and becomes the value of the expression. If exp1 is false then exp3 is evaluated and its value becomes the value of the expression. Eg: if(a>b) x = a; else x = b; Equivalent statement: x = (a>b)? a : b;

Examples for ++ & -- operators Let the value of a =5 and b=++a then a = b =6 Let the value of a = 5 and b=a++ then a =6 but b=5 i.e.: 1. a prefix operator first adds 1 to the operand and then the result is assigned to the variable on the left 2. a postfix operator first assigns the value to the variable on left and then increments the operand.

Bitwise Operators

Special Operators C supports some special operators such as Comma operator Size of operator Pointer operators(& and *) and Member selection operators(. and ->)

The Comma Operator The comma operator can be used to link the related expressions together. A comma linked list of expressions are evaluated left to right and the value of right-most expression is the value of the combined expression. Eg: value = (x = 10, y = 5, x + y); This statement first assigns the value 10 to x, then assigns 5 to y, and finally assigns15(i.e, 10+5) to value. The Size of Operator The size of is a compile time operator and, when used with an operand, it returns the number of bytes the operand occupies. Eg: 1) m = sizeof(sum); 2) n = sizeof(long int) 3) k = sizeof(235l)

Precedence of operators BODMAS RULE- Brackets of Division Multiplication Addition Subtraction Brackets will have the highest precedence and have to be evaluated first, then comes of, then comes division, multiplication, addition and finally subtraction. C language uses some rules in evaluating the expressions and they are called as precedence rules or sometimes also referred to as hierarchy of operations, with some operators with highest precedence and some with least. The 2 distinct priority levels of arithmetic operators in c are- Highest priority : * / % Lowest priority : + -

EXPRESSIONS The combination of operators and operands is said to be an expression. An arithmetic expression is a combination of variables, constants, and operators arranged as per the syntax of the language. Eg 1) a = x + y;

Arithmetic Expressions An arithmetic expression is a combination of variables, constants, and operators arranged as per the syntax of the language. Eg 1) a = x + y;

EVALUATION OF EXPRESSIONS Expressions are evaluated using an assignment statement of the form variable = expression; Eg: 1) x = a * b c; 2) y = b / c * a; An arithmetic expression without parenthesis will be evaluated from left to right using the rule of precedence of operators. There are two distinct priority levels of arithmetic operators in C. High priority * / % Low priority + -

Operators in C

PRECEDENCE OF ARITHMETIC OPERATORS /*Evaluation of expressions*/ main() { float a, b, c, y, x, z; a = 9; b = 12; c = 3; x = a b / 3 + c * 2 1; y = a b / (3 + c) * (2 1); z = a (b / (3 + c) * 2) 1; printf( x = %f \n,x); printf( y = %f \n,y); printf( z = %f \n,z); } OUTPUT x = 10.000000 y = 7.000000 z = 4.000000

Rules for evaluation of expression 1. First parenthesized sub expression from left to right are evaluated. 2. If parentheses are nested, the evaluation begins with the innermost sub expression 3. The precedence rule is applied in determining the order of application of operators in evaluating sub expressions 4. The associativity rule is applied when 2 or more operators of the same precedence level appear in a sub expression. 5. Arithmetic expressions are evaluated from left to right using the rules of precedence 6. When parentheses are used, the expressions within parentheses assume highest priority

TYPE CONVERSION IN EXPRESSIONS C permits the mixing of constants and variables of different types in an expression. If the operands are of different types, the lower type is automatically converted to higher type before the operation proceeds. The result is of the higher type. All short and char are converted to int If one of the operand is long double, the other is converted to long double and result will be long double Else If one of the operand is double, the other is converted to double and result will be double If one of the operand is float, the other is converted to float and result will be float If one of the operand is unsigned long int, the other is converted to unsigned long int and result will be unsigned long int If one of the operand is long int, the other is unsigned int, then If unsigned int can be converted to long int, the unsigned int operand will be converted and result will be long int Else both operands will be converted to unsigned long int and the result will be unsigned long int

TYPE CONVERSION IN EXPRESSIONS

Explicit Conversion C performs type conversion automatically. However, there are instances when we want to force a type conversion in a way that is different from the automatic conversion. Eg: ratio = female_number / male_number Since female_number and male_number are declared as integers the ratio would resent a wrong figure. Hence it should be converted to float. ratio = (float) female_number / male_number The general form of a cast is: (type-name)expression

MATHEMATICAL FUNCTIONS Mathematical functions such as sqrt, cos, log etc., are the most frequently used ones. To use the mathematical functions in a C program, we should include the line #include<math.h> in the beginning of the program