Lecture 8.

Slides:



Advertisements
Similar presentations
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Advertisements

Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.
Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)
1 Chapter 8 Functions, Pointers, and Storage Classes  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
Computer Science 210 Computer Organization Introduction to C.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
CS2311 Computer Programming Dr. Yang, Qingxiong (with slides borrowed from Dr. Xu, Henry) Lecture 2: Basic Syntax – Part I: Variables and Constants.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
CPS120: Introduction to Computer Science
Tutorial ICS431 – Introduction to C.
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
Lecture #5 Introduction to C++
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Introduction to Programming
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Gator Engineering 1 Chapter 2 C Fundamentals (Continued) Copyright © 2008 W. W. Norton & Company. All rights reserved.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
 constant represented by a name, just like a variable, but whose value cannot be changed  The const keyword precedes the type, name, and initialization.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Basic Data Types & Memory & Representation
C++ Lesson 1.
Chapter # 2 Part 2 Programs And data
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Computer Science 210 Computer Organization
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Data types Data types Basic types
Computing and Statistical Data Analysis Lecture 2
Programming Fundamentals
Tokens in C Keywords Identifiers Constants
Fundamental of Programming (C)
Week 4 – Repetition Structures / Loops
For loops Increment ,decrement operations
Chapter 3: Understanding C# Language Fundamentals
Introduction to C Programming
Chapter 2.
Computer Science 210 Computer Organization
Introduction to the C Language
Arrays, For loop While loop Do while loop
Conversions of the type of the value of an expression
Lecture 8.
פרטים נוספים בסילבוס של הקורס
Introduction to C Programming
C++ Data Types Data Type
Lectures on Numerical Methods
UMBC CMSC 104 – Section 01, Fall 2016
Introduction to Programming
Chapter # 2 Part 2 Programs And data
C++ Programming Lecture 3 C++ Basics – Part I
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Java’s Central Casting
Type Conversion It is a procedure of converting one data type values into another data type In C programming language we are having two types of type conversion.
Module 2 Variables, Data Types and Arithmetic
Session 1 – Introduction to Computer and Algorithm (Part 2)‏
Chapter 2 Primitive Data Types and Operations
Variables in C Topics Naming Variables Declaring Variables
Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token.
Variables and Constants
Programming Fundamental-1
Presentation transcript:

Lecture 8

Outline Sizeof Type casting Constants Examples

sizeof The sizeof keyword returns the number of bytes of the given expression or type returns an unsigned integer result sizeof variable_Identifier; sizeof (variable_Identifier); sizeof (Data_Taype); Example: int x; printf("size of x = %d", sizeof x);//4 printf("size of long long = %d", sizeof(long long));//8 printf("size of x = %d", sizeof (x));//4

Type Casting What is the casting? When the type of variable and value are not the same Example: Assigning double value to integer variable

Implicit casting Implicit We don’t say it But we do it carried out by compiler automatically char f2= 50e6; /*cast from double to char */ int i= 98.01;/*cast from double to int */ float f = 65.6; int i = f; //f = (int)f; char c = i; // c = (char)i;

Explicit casting Explicit And we do it We say it And we do it carried out by programmer using casting int i=(int)98.1;/*cast from double to int */ char c = (char) 90;/* cast from int to char */ int k, i = 7; float f = 10.14; char c = 'B'; k = (i + f) % 3; // error k = (int)(i + f) % 3;

Type Casting char c = 'A'; short s = 1; int i; float f; double d; i = (c + s); (int) (char) (short) (short) (int) d = (c / i) + (f * d) + (f + i); (char) (int) (float) (double) (float) (int) int double float double

Casting effects Casting from small types to large types There is not any problem No loss of data int i; short s; float f; double d; s = 'A'; // s = 65 i = 'B'; // i = 66 f = 4566; // f = 4566.0 d = 5666; // d = 5666.0

Casting effects (cont’d) Casting from large types to small types Data loss is possible Depends on the values float double short f = 65536; d = 65536; s = 720; // 65536.0 // 720 char short c = (char) 65536; // c = 0 // s = 0 s = (short) 65536; int i = int i = 1.22; 1e23; // i = 1 // i = ???

Casting effects (cont’d) Casting to Boolean If value is zero  false If values is not zero  true bool bool b2 = 'a', b3 = -9, b5 = b4 = 4.5; //true //false 0, b6 = false; b7 = '\0';

Constant Variables Constants Do not want to change the value Example: pi = 3.14 We can only initialize a constant variable We MUST initialize the constant variables (why?!)

Constant const [Data_Type] Identifier = constant_value; const p = 3; // const int p = 3; const p; p = 3.14; // compile error const p = 3.14; // p = 3 because default is int const float p = 3.14;

Constant const int STUDENTS = 38; const long int MAX_GRADE =20; int i; i = MAX_GRADE; STUDENTS = 39;//ERROR

Definitions Another tool to define constants Definition is not variable We define definition, don’t declare them Pre-processor replaces them by their values before compiling #define STUDENTS 38 int main(void){ int i; i = STUDENTS STUDENTS = 90; //ERROR! What compiler sees: 38 = 90

Constant #define Identifier constant_value #define PI 3.14 #define ERROR "Disk error " #define ONE 1 #define TWO ONE + ONE

#include <stdio.h> // (preprocessor ) #define PI 3.14 // PI constant (preprocessor ) // calculating area of circle int main() { /* variable definition */ float Radius; float Area = 0; // get radius of circle form user printf("Enter Radius :\n"); scanf("%f", &float ); // calculating area of circle Area = PI * Radius * Radius; printf(“Area = %f", Area ); system("Pause"); return 0; }

Examples Precedence Rules

int num = 0, i = 0; for (i = 0; i < 5; i++) { printf("%d", ++num); } printf("%d", num++); 12345 01234 _ 18

b=(a++>0) ? printf("%d\n",a):printf("%d\n",a); int b,a = 0; b=(a++>0) ? printf("%d\n",a):printf("%d\n",a); b = (a>0)?printf("%d\n",++a):printf("%d\n",a); printf("%d\n" , a) ; b=++a+(a>0 )?printf("%d\n",a++):printf("%d\n",a); printf("%d\n" ,a) ; 1 _ 1 2 19

int a = 0; int b = 5; b = ( a++ > 0 ) ? ++a : ++b; printf("%d %d\n" , a, b); int a = 5; int b = 13; a = a * b % b / a; b = (++a > 0 ) ? a : b++ ; 1 6 1 1 _ 20

int j,k,t=1,i=2; for(k=0;k<5;k++){ j = i++ + t; printf("%d",j); } 34567 1 1 0 0 0 _ 21

int a = 40, b = 30, c; c = !a <= !(c = b); printf("%d", c); int i =1; int j = 3*(4+i++); printf("%d %d\n", i,j); 1 2 15 _ 22

int n = 5;int i = 0; while(i++ <= n) printf("%d " , i); 1 2 3 4 5 6 1 2 3 _ 1 2 3 23

printf("%d %d %d %d", i, j, k, m); printf("%d",1 + 5&4 == 3); int i=-3, j=2, m, k=0; m=++i && ++j || ++k; printf("%d %d %d %d", i, j, k, m); printf("%d",1 + 5&4 == 3); m=++i && ++j && k++; -2 3 0 1 -2 3 1 0 _ 24

printf("%d %d %d", i, ++i, i++); int i = 0, j = 0; j = i++ + ++i; printf("%d %d",i,j); 3 3 1 2 2 _ 25

int i=1; printf("%d", i++ + ++i); int i=3; printf("%d",++i + ++i); printf("%d\n", i); 4 10 1 C has the concept of undefined behavior, i.e. some language constructs are syntactically valid but you can't predict the behavior when the code is run. _ 26