Variable Declarations, Data types, Expressions

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
String Escape Sequences
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Primitive Variables.
Data Types Declarations Expressions Data storage C++ Basics.
Doing math In java.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
Fundamental Data Types and Storage Classes: A C language programmer has to tell the system before-hand, the type of numbers or characters he is using in.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
CompSci 230 S Programming Techniques
CSE 220 – C Programming Expressions.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Operators And Expressions
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chap. 2. Types, Operators, and Expressions
University of Central Florida COP 3330 Object Oriented Programming
Visual Basic Variables
Chapter 2 - Introduction to C Programming
University of Central Florida COP 3330 Object Oriented Programming
Relational Operations
Data Types, Identifiers, and Expressions
Chapter 7: Expressions and Assignment Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Variable Declaration, Data types, Expressions
Intro to C Tutorial 4: Arithmetic and Logical expressions
Chapter 2 - Introduction to C Programming
Data Structures Mohammed Thajeel To the second year students
Introduction to C Programming
Operators and Expressions
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Introduction to C Programming
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Chapter 2 - Introduction to C Programming
Expressions.
Chapter 2 Programming Basics.
Chapter 2 - Introduction to C Programming
Module 2 Variables, Data Types and Arithmetic
OPERATORS in C Programming
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
OPERATORS in C Programming
Presentation transcript:

Variable Declarations, Data types, Expressions Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Variable Declarations, Data types, Expressions - Variables and Operators

Memory Concepts Variables A visual representation (memory map) Variable names (identifiers) correspond to locations in the computer's memory (von Neuman model) Every variable has a name, a type, a size and a value Whenever a new value is placed into a variable (through scanf, for example), it replaces (and destroys) the previous value. (Destructive write) Reading variables from memory does not change them A visual representation (memory map) address value variable datatype size 36443 int 45 i 4 bytes

Keywords

Expression Expressions are computations that return a value. Like variables, a value is an instance of a data type. Examples of expressions are: 45 (int) 2+3 (int) 2.0/5.0 (double) “Hello” (string) x (the current value of variable int x)

Arithmetic Operators Binary Operator (two operands) + (addition) - (subtraction) * (multiplication) / (division) % (modulus, remainder) (no ** ) Unary Operator (single operands) - (no + ) Example: int i=1, j=2, k=3, x; x=i+2*j-22/k; x=-1+j; x=1+-j; x=+i+j; x=22%k; float f=1.5, g=0.5, y; y=2.5*f+4.0*g; Exercise: Try -5%3 -5%-3 5%-3 (hint: -5/3=-1 -5/-3=1 5/-3=-1 and R=x-y*i) Mixed data types will be discussed later Operators that have more than two operands use functional notation a = f(x,y,x). (x=1 + 4 -7 = -2) (x= 1) (x= -1) (wrong expression) (x= 1, remainder) (y=5.75) Ans: -2 -2 2

Arithmetic Operators Arithmetic operators: C operation Arithmetic Algebraic expression C expression Addition + f + 7 Subtraction - p – c Multiplication * bm b * m Division / x / y Modulus % r mod s r % s

Precedence

Relational Operators Binary Operators == != < > <= >= == != < > <= >= Result is a integer: 1 means TRUE 0 means FALSE No logical type variable and constants No space between the operators Example: Meaning C Expression Result equal not equal greater less greater equal less equal == != > < >= <= 5 == 3 5 != 3 5 > 3 5 < 3 5 >= 3 5 <= 3 1 1 1 0==0 1 int i=10, j=10, k=1; i + j <= 3 + k

Relational Operators

Logical (Boolean) Operators Binary Operators && (and) || (OR) Unary Operator ! (not) Operand must be int Use float or double, the result may not predictable nonzero (TRUE) zero (FALSE) Result is int 1 (TRUE) 0 (FALSE) Express connected by && or || are evaluated from left to right, and evaluation stops as soon as the truth or falsehood of the result is known. i.e. ‘expr1 && expr2’ is not equal to ‘expr2 && expr1’. This is called short-circuit evaluation. ‘inward == 0’ normally be written as ‘!inward’ Example: 3 < 7 < 5 3 < 7 && 7 < 5 Example: Expression Result 5||3 5||0 5&&3 5&&0 i&&j (if i=0, j=0) i&&j+1 (if i=5, j=0) !5 !0 !i (if i=5) 1 1 1 1 1 (3 < 7) < 5 1 < 5 1 1 && 0 0

Conditional (ternary) Operator Syntax expr1 ? expr2 : expr3 If expr1  0, then execute expr2 and ignore expr3 If expr1 = 0, then execute expr3 and ignore expr2 Example: x = i+j ? i+1 : j+1 Example: x = 5 ? 4 : 2; /* x = 4 */ j = 4; i = 2 x = i+j ? i+1 : j-1 /* x = 3 */ l = a > b ? a : b; /* the larger of a and b */ max =(a > b)?((a>c)?a:c):(b>c)?b:c); /* the maximum number among a, b, and c */ x = a > 0 ? a: -a; /* the absolute value of a */

sizeof Operator Syntax sizeof(expr) The number of bytes occupied by expr For most computers sizeof(3) 2 or 4 (bytes) (depending on16 bit CPU or 32 bit CPU), where 3 is an integer sizeof(3L) 4 (long int) sizeof(3.0) 8 (double float) Example: double i; printf(“%d”,sizeof(i)); 8 Usually, this operator is used to get the size of an organized variable (like struct, union, …) This is one of a few functions that are built-in. No #include is required.

Address Operator & means the address of var Syntax &var Get the address of the variable & means the address of var Type of var may be (a) fundamental data type (b) organized data type Example: int i=100; printf(“%d %d”, &i, i); Address Content RAM 100 1002 1001 1000 1003 1004 1005