CSC141- Introduction to Computer Programming

Slides:



Advertisements
Similar presentations
2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Structures –Collections of related.
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Structures Functions and Arrays Dale Roberts, Lecturer Computer.
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
C Language.
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Arrays and Strings.
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.
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 25 Thanks for Lecture Slides: Dr. Sadaf Tanveer Dr. Sadaf Tanveer,
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
Introduction to C Language
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
C Tokens Identifiers Keywords Constants Operators Special symbols.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
Week 1 Algorithmization and Programming Languages.
Data Types. Data types Data type tells the type of data, that you are going to store in memory. It gives the information to compiler that how much memory.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Pointers *, &, array similarities, functions, sizeof.
Dale Roberts Introduction to Java - Input, Program Control and Instantiation Dale Roberts, Lecturer Computer Science, IUPUI
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
ME2008– W05 MID1- Reference 2016Q1- Source: Deitel /C- How To.
Structure, Unions, typedef and enumeration
C Structures, Unions, Bit Manipulations and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
CSCI206 - Computer Organization & Programming
Basic notes on pointers in C
DATA HANDLING.
Visit for more Learning Resources
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
C Structures, Unions, Bit Manipulations and Enumerations
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
C Programming Pointers
C Data Types and Variable
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Course Outcomes of Programming In C (PIC) (17212, C203):
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Getting Started With Coding
Presentation transcript:

CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 26 Thanks for Lecture Slides: http://www.deitel.com/Books/C/CHowtoProgram7e/tabid/3635/Default.aspx

Unions : Introduction Memory that contains a variety of objects over timeBut use one data member at a time Members of a union share space and hence conserves storage Members of a union can be of any data type. Number of bytes used to store a union must be at least enough to hold the largest member of the union. Only the last data member defined can be accessed Important to ensure that that the data in a union is referenced with the proper data type

Union Declarations Declared with keyword union in the same format as a structure union Number { int x; double y; }; The union definition is normally placed in a header and included in all source files that use the union type.

Valid Operations on Unions Assignment to union of same type: = Taking address (&) of a union variable Accessing union members using (.) Accessing members using pointers (->) Invalid Operations on Unions Unions may NOT be compared using (==) and (!=) for the same reasons that structures cannot be compared. Comparing unions is a ‘syntax error’.

Initializing Unions in Declarations Union may be initialized with a value of the same type as the first union member. For example, union Number value = { 10 }; Above initialization of union variable is valid because the union is initialized with an int which is the same type as the first union member. But, the following declaration would truncate the floating-point part of the value. union Number value = { 10.4 }; It will also produce a warning from the compiler.

Demonstrating Unions // Fig. 10.5: fig10_05.c // Displaying the value of a union in both member data types #include <stdio.h> // number union definition union number { int x; double y; }; // end union number int main( void ) { union number value; // define union variable value.x = 100; // put an integer into the union printf( "%s\n%s\n%s\n %d\n\n%s\n %f\n\n\n", "Put 100 in the integer member", "and print both members.", "int:", value.x, "double:", value.y );

value.y = 100.0; // put a double into the same union Demonstrating Unions value.y = 100.0; // put a double into the same union printf( "%s\n%s\n%s\n %d\n\n%s\n %f\n", "Put 100.0 in the floating member", "and print both members.", "int:", value.x, "double:", value.y ); } // end main  

Output Put a value in the integer member and print both members. int: 100 double: 92559592117433136000000000000000000000000000000000000000000000.000000 Put a value in the floating member int: 0 100.000000

Bitwise Operators All data represented internally as sequences of bits Each bit can be either 0 or 1 Sequence of 8 bits forms a byte

Example: Bit Manipulation // Fig. 10.7: fig10_07.c // Displaying an unsigned int in bits #include <stdio.h> void displayBits( unsigned int value ); // prototype int main( void ) { unsigned int x; // variable to hold user input printf( "%s", "Enter a nonnegative int: " ); scanf( "%u", &x ); displayBits( x ); } // end main // display bits of an unsigned int value

Example: Bit Manipulation void displayBits( unsigned int value ) { unsigned int c; // counter // define displayMask and left shift 31 bits unsigned int displayMask = 1 << 31; printf( "%10u = ", value ); // loop through bits for ( c = 1; c <= 32; ++c ) { putchar( value & displayMask ? '1' : '0' ); value <<= 1; // shift value left by 1 if ( c % 8 == 0 ) { // output space after 8 bits putchar( ' ' ); } // end if } // end for putchar( '\n' ); } // end function displayBits

Bitwise Operators

Bitwise Operators

Bitwise Operators

Bitwise AND (&) OR ( | ) exclusive or ( ^ ) // Fig. 10.9: fig10_09.c // Using the bitwise AND, bitwise inclusive OR, bitwise // exclusive OR and bitwise complement operators #include <stdio.h> void displayBits( unsigned int value ); // prototype int main( void ) { unsigned int number1; unsigned int number2; unsigned int mask; unsigned int setBits; // demonstrate bitwise AND (&) number1 = 65535; mask = 1; puts( "The result of combining the following" ); displayBits( number1 ); displayBits( mask ); puts( "using the bitwise AND operator & is" ); displayBits( number1 & mask );

Bitwise AND (&) OR ( | ) exclusive or ( ^ ) // demonstrate bitwise inclusive OR (|) number1 = 15; setBits = 241; puts( "\nThe result of combining the following" ); displayBits( number1 ); displayBits( setBits ); puts( "using the bitwise inclusive OR operator | is" ); displayBits( number1 | setBits ); // demonstrate bitwise exclusive OR (^) number1 = 139; number2 = 199;

Bitwise AND (&) OR ( | ) exclusive or ( ^ ) puts( "\nThe result of combining the following" ); displayBits( number1 ); displayBits( number2 ); puts( "using the bitwise exclusive OR operator ^ is" ); displayBits( number1 ^ number2 ); // demonstrate bitwise complement (~) number1 = 21845; puts( "\nThe one's complement of" ); puts( "is" ); displayBits( ~number1 ); } // end main

Bitwise AND (&) OR ( | ) exclusive or ( ^ ) // display bits of an unsigned int value void displayBits( unsigned int value ) { unsigned int c; // counter // declare displayMask and left shift 31 bits unsigned int displayMask = 1 << 31; printf( "%10u = ", value ); // loop through bits for ( c = 1; c <= 32; ++c ) { putchar( value & displayMask ? '1' : '0' ); value <<= 1; // shift value left by 1 if ( c % 8 == 0 ) { // output a space after 8 bits putchar( ' ' ); } // end if } // end for putchar( '\n' ); } // end function displayBits

Output The result of combining the following 65535 = 00000000 00000000 11111111 11111111 1 = 00000000 00000000 00000000 00000001 using the bitwise AND operator & is   15 = 00000000 00000000 00000000 00001111 241 = 00000000 00000000 00000000 11110001 using the bitwise inclusive OR operator | is 255 = 00000000 00000000 00000000 11111111 139 = 00000000 00000000 00000000 10001011 199 = 00000000 00000000 00000000 11000111 using the bitwise exclusive OR operator ^ is 76 = 00000000 00000000 00000000 01001100 The one's complement of 21845 = 00000000 00000000 01010101 01010101 is 4294945450 = 11111111 11111111 10101010 10101010

Bitwise shift left ( << ) and shift right ( >> ) // Fig. 10.13: fig10_13.c // Using the bitwise shift operators #include <stdio.h> void displayBits( unsigned int value ); // prototype int main( void ) { unsigned int number1 = 960; // initialize number1 // demonstrate bitwise left shift puts( "\nThe result of left shifting" ); displayBits( number1 ); puts( "8 bit positions using the left shift operator << is" ); displayBits( number1 << 8 ); // demonstrate bitwise right shift puts( "\nThe result of right shifting" ); puts( "8 bit positions using the right shift operator >> is" ); displayBits( number1 >> 8 ); } // end main

Bitwise shift left ( << ) and shift right ( >> ) // display bits of an unsigned int value void displayBits( unsigned int value ) { unsigned int c; // counter // declare displayMask and left shift 31 bits unsigned int displayMask = 1 << 31; printf( "%7u = ", value ); // loop through bits for ( c = 1; c <= 32; ++c ) { putchar( value & displayMask ? '1' : '0' ); value <<= 1; // shift value left by 1 if ( c % 8 == 0 ) { // output a space after 8 bits putchar( ' ' ); } // end if } // end for putchar( '\n' ); } // end function displayBits

Output The result of left shifting 960 = 00000000 00000000 00000011 11000000 8 bit positions using the left shift operator << is 245760 = 00000000 00000011 11000000 00000000   The result of right shifting 8 bit positions using the right shift operator >> is 3 = 00000000 00000000 00000000 00000011

Bitwise Operators

Bitwise Operators

Bit Fields Bit field Member of a structure whose size (in bits) has been specified Enable better memory utilization Must be defined as int or unsigned Cannot access individual bits Defining bit fields Follow unsigned or int member with a colon (:) and an integer constant representing the width of the field Example: struct BitCard { unsigned face : 4; unsigned suit : 2; unsigned color : 1; };

Bit Fields Unnamed bit field Field used as padding in the structure Nothing may be stored in the bits struct Example { unsigned a : 13; unsigned : 3; unsigned b : 4; } Unnamed bit field with zero width aligns next bit field to a new storage unit boundary

Card shuffling Example using Bit-field // Fig. 10.16: fig10_16.c // Representing cards with bit fields in a struct #include <stdio.h> #define CARDS 52 // bitCard structure definition with bit fields struct bitCard { unsigned int face : 4; // 4 bits; 0-15 unsigned int suit : 2; // 2 bits; 0-3 unsigned int color : 1; // 1 bit; 0-1 }; // end struct bitCard typedef struct bitCard Card; // new type name for struct bitCard void fillDeck( Card * const wDeck ); // prototype void deal( const Card * const wDeck ); // prototype

Card shuffling Example using Bit-field int main( void ) { Card deck[ CARDS ]; // create array of Cards fillDeck( deck ); deal( deck ); } // end main // initialize Cards void fillDeck( Card * const wDeck ) size_t i; // counter // loop through wDeck for ( i = 0; i < CARDS; ++i ) { wDeck[ i ].face = i % (CARDS / 4); wDeck[ i ].suit = i / (CARDS / 4); wDeck[ i ].color = i / (CARDS / 2); } // end for } // end function fillDeck

Card shuffling Example using Bit-field // output cards in two-column format; cards 0-25 subscripted with // k1 (column 1); cards 26-51 subscripted with k2 (column 2) void deal( const Card * const wDeck ) { size_t k1; // subscripts 0-25 size_t k2; // subscripts 26-51 // loop through wDeck for ( k1 = 0, k2 = k1 + 26; k1 < CARDS / 2; ++k1, ++k2 ) { printf( "Card:%3d Suit:%2d Color:%2d ", wDeck[ k1 ].face, wDeck[ k1 ].suit, wDeck[ k1 ].color ); printf( "Card:%3d Suit:%2d Color:%2d\n", wDeck[ k2 ].face, wDeck[ k2 ].suit, wDeck[ k2 ].color ); } // end for } // end function deal

Output Card: 0 Suit: 0 Color: 0 Card: 0 Suit: 2 Color: 1

Enumeration Constants Set of integer constants represented by identifiers Enumeration constants are like symbolic constants whose values are automatically set Values start at 0 and are incremented by 1 Values can be set explicitly with = Need unique constant names Example: enum Months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; Creates a new type enum Months in which the identifiers are set to the integers 1 to 12 Enumeration variables can only assume their enumeration constant values (not the integer representations)

Example: using Enumeration Type // Fig. 10.18: fig10_18.c // Using an enumeration #include <stdio.h> // enumeration constants represent months of the year enum months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }; // end enum months int main( void ) { enum months month; // can contain any of the 12 months // initialize array of pointers const char *monthName[] = { "", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; // loop through months for ( month = JAN; month <= DEC; ++month ) { printf( "%2d%11s\n", month, monthName[ month ] ); } // end for } // end main

Output 1 January 2 February 3 March 4 April 5 May 6 June 7 July 8 August 9 September 10 October 11 November 12 December