The Cast Operator The cast operator converts explicitly from one data type of an expression to another. For example, if x is of type int, the value of.

Slides:



Advertisements
Similar presentations
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Advertisements

Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h.
1 Review of Class on Oct Outline of Chapter 4  How to write a function?  Function Prototypes  Function Invocation  Function Definition  The.
1 Fundamental Data Types. 2 Declaration All variables must be declared before being used. –Tells the compiler to set aside an appropriate amount of space.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05.
Guidelines for working with Microsoft Visual Studio.Net.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Guidelines for working with Microsoft Visual Studio 6.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
1 Review of Chapter 6: The Fundamental Data Types.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Week 3 - Monday.  What did we talk about last time?  Math library  Preprocessor directives  Lab 2.
Real World Applications: Statistical Measures Problem (page 95-98) Read a series of floating-point numbers from the standard input, and print a statistical.
Outline Symbolic Constants Character Input and Output if … else switch…case.
Real World Applications: Generating Prime Numbers  Problem Write a program that prints all positive prime integers less than or equal to n. A positive.
Opening and Closing Files First define a file pointer, which can "pointing" to one of the opened file. #include... FILE *fp; Use fopen() to open a file,
Lecture12. Outline Binary representation of integer numbers Operations on bits –The Bitwise AND Operator –The Bitwise Inclusive-OR Operator –The Bitwise.
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through.
Functions #include int sum(int i, int j); main() { int a, b, res; a = 1; b = 2; res = sum(a, b); printf("%d + %d = %d\n", a, b, res); } int sum(int i,
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Number Systems and Bitwise Operation.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Administrative things
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
Road map char data type Reading –Liang 5: Chapter 2: 2.7.4; 2.9; –Liang 6: Chapter 2: 2.7.4; 2.9 –Liang 7: Chapter 2: 2.7.4; 2.9.
Introduction to Computer Organization & Systems Topics: Command Line Bitwise operators COMP Spring 2014 C Part V.
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.
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
DATA TYPE, MEMORY, AND FUNCTION Dong-Chul Kim BioMeCIS UTA 2/18/
2/23/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 9 (C-3)
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
7. BASIC TYPES. Systems of numeration Numeric Types C’s basic types include integer types and floating types. Integer types can be either signed or unsigned.
Modulus Operator #include main() { int a, b, q, r; printf(" a b a/b a%b\n"); while (scanf("%d%d", &a, &b) != EOF) { q = a / b; /* quotient */ r = a % b;
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Basic Data Types & Memory & Representation
C Programming Types & Dynamic memory & bits & others
Stack and Heap Memory Stack resident variables include:
While and Do-while action action while ( ) { } do { } while ( );
CSE 220 – C Programming Bitwise Operators.
Input/output.
7. BASIC TYPES.
Command-Line Arguments
Number Systems and Bitwise Operation
Fundamental Data Types
Lexical Elements, Operators, and the C Cystem
Introduction to the C Language
C Program Design Data Types
More about Numerical Computation
Lexical Elements, Operators, and the C Cystem
Bits and Bytes Topics Representing information as bits
Homework Homework Continue Reading K&R Chapter 2 Questions?
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Fundamental Data Types
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Module 10 Operations on Bits
Bitwise Operators.
Week 2 - Friday CS222.
INTRODUCTION TO C.
Presentation transcript:

The Cast Operator The cast operator converts explicitly from one data type of an expression to another. For example, if x is of type int, the value of the expression ( float ) x is the original value of x converted to float. The type and value of x are unchanged. If x = 3, then (float) x is

Cast, example # include main() { int i, j; float x, y, z; i = 1; j = 3; x = i/j; y = (float) i / j; z = (float) i / (float) j; printf("x=%f,\ny=%f,\n" "z=%f.\n", x, y, z); } prints out: x= , y= , z=

Real World Applications: Summing a Series  Problem Write a program to sum the first n terms of the infinite series 1/ / / /4 2 + …  Solution We first prompt the user for the value n. We add the terms starting from the smallest. This improves the accuracy of the sum by ensuring that the values to be added are within the same range, so that we minimize the loss of significant digits due to rounding.

Sum up a series #include main() { int i, n, resp; float sum; do { printf( "\nAgain (1 = yes, 0 = no)? "); scanf("%d", &resp); if (resp) { printf("n = ? "); scanf("%d", &n); sum = 0.0; for(i = n; i > 0; i--) sum += 1/((float) i * (float) i); printf("Sum = %f\n", sum); } } while (resp); }

Run the sum program Compile with cc pg136.c a.out Again (1 = yes, 0 = no)? 1 n = ? 10 Sum = Again (1 = yes, 0 = no)? 1 n = ? 1000 Sum = Again (1 = yes, 0 = no)? 1 n = ? Sum = Again (1 = yes, 0 = no)? 1 n = ? Sum = Again (1 = yes, 0 = no)? 0 Expected answer for the infinite series is  2 /6=

The sizeof Operator The sizeof operator gives the amount of storage required by an object. Sizeof is not a function. Example: sizeof(char) sizeof('A')

Sizeof example #include main() { printf("size of char is %d\n", sizeof(char)); printf("size of int is %d\n", sizeof(int)); printf("size of float is %d\n", sizeof(float)); printf("size of double is %d\n", sizeof(double)); } You get the following print out : size of char is 1 size of int is 4 size of float is 4 size of double is 8

getchar/putchar The function getchar reads one character from the standard input, and the function putchar writes one charater to the standard output. The function getchar returns the character code or the value EOF when end of file is reached. Examples: c = getchar(); putchar(d);

getchar and putchar, example #include main() { int c; while( (c=getchar()) != EOF) putchar(c); } How to use this program? –Use it to type a file like UNIX cat command by a.out < file –Use it to copy files by a.out file2

Bitwise Operator ~ Bitwise complement & Bitwise AND | Bitwise OR ^ Bitwise exclusive OR << Bitwise left shift >> Bitwise right shift Bitwise operators work on the binary bits. In particular, bitwise complement, AND, OR, exclusive OR operate on all the bits of a word independently first number 1010 second number Bitwise AND 0010 result of bitwise AND That is 6 & 10 gets 2.

Bitwise Logical Operators Let b 1 and b 2 be two bits at the same bit position in two words. b2b2 b1b1 Word 1 Word 2 The operations are defined by this table:

Bitwise Operator, examples #include main() { short int w1, w2; w1 = 12; w2 = -35; printf("w1 = %d\n", w1); printf("w2 = %d\n", w2); printf("~w1 = %d\n", ~w1); printf("~w2 = %d\n", ~w2); printf("w1 & w2 = %d\n", w1 & w2); printf("w1 & ~w2 = %d\n", w1 & ~w2); printf("~w1 & ~w2 = %d\n", ~w1 & ~w2); printf("w1 | w2 = %d\n", w1 | w2); printf("~(w1 | w2) = %d\n", ~(w1 | w2)); printf("w1 ^ w2 = %d\n", w1 ^ w2); } This output is w1 = 12 w2 = -35 ~w1 = -13 ~w2 = 34 w1 & w2 = 12 w1 & ~w2 = 0 ~w1 & ~w2 = 34 w1 | w2 = -35 ~(w1 | w2) = 34 w1 ^ w2 = -47

Bitwise Operation w1 = w2 = ~w2 = & | ^ AND OR Exclusive OR

Shift Operators w << n shift the bit pattern as a whole to the left n bits. Fill the shift-in bits with 0. w >> n shift the bit pattern to the right n bits. Fill with zero for positive number. (0 or 1 for negative number). if w = w << 2 becomes

Bit Operation, Examples #include main() { int x = 'A'; int y = 2; short i, j; printf("%d\n", x >> y); i = 0x7A4E; j = ((i>>8) & 0x00FF) | (i<<8); printf("%X\n", j); } x = 'A' = = 0… x >>2 = 0… =16 10 i>>8 = 0x007A (i>>8) & 0x00FF = 0x007A i<<8 = 0x4E00 j = 0x007A | 0x4E00 = 0x4E7A

What is printed? #include main() { int i, j, k; i = 1; j = 0; k = 3; 1 printf("%d, ", k<<i); 2 printf("%d, ", !j); 3 printf("%d, ", ~j); 4 printf("%d, ", i & k); 5 printf("%d, ", i && k); 6 printf("%d, ", i ^ j); 7 printf("%d, ", i | k); 8 printf("%d, ", i || k); printf("\n"); } 6, 1, -1, 1, 1, 1, 3, 1,

Reading/Home Working Read Chapter 4, page 134 to 148. Work on Problems –Section 4.9, page 138, exercise 1, 3, 5. –Section 4.10, page 142, exercise 1. –Section 4.1, page 148, exercise 1, 3. Check your answers in the back of the textbook. Do not hand in.