Chapter 10 C Structures, Unions, Bit Manipulations

Size: px
Start display at page:

Download "Chapter 10 C Structures, Unions, Bit Manipulations"

Transcription

1 Chapter 10 C Structures, Unions, Bit Manipulations Skipped! Skipped! and Enumerations Skipped!

2 Page 416 In programming languages, Arrays (Chapter 6) allows programmers to group elements of the same type under one name. C programming language also provides another tool for grouping elements together called Structures. Structures sometimes referred to as aggregates are collections of related variables grouped under one name. Structures may contain variables of many different data types in contrast to arrays that contain only elements of the same data type. Structures are commonly used to define records to be stored in files (see Chapter 11, C File Processing). Structures are very powerful concept that programmers use in many C programs.

3 10.2 Structure Definitions 1/3 Page 416 A structure is a derived data types and structures are defined by using struct statement. In order to understand the concept of structure, let s consider the definition of any point in Cartesian coordinates. In Cartesian coordinates any point can be described with two numbers corresponding x- and y-coordinates.**** Name Tag kind of structure struct point { int x; int y; }; Members of Structure Members are defined within braces as regular variable definitions. struct statement ends with ; as other statements in C Note: This is just the structure definition. Structure definitions doesn t consume any space on the memory. We will learn how to use the structures later in this chapter.

4 10.2 Structure Definitions 2/3 Page 416 Let s suppose that we are dealing with playing cards; each card can be represented by a face and suit.** Suit of the card: one of the following Hearths, Spades, Clubs, Diamonds Face of the Card: one of the following Ace, Two,., Jack, Queen, King We can define a structure with two members corresponding the suit and face of the card. Both variables can be considered as strings as below.

5 10.2 Structure Definitions 3/3 Page 416 Let s suppose that we are dealing with the employee records; employee s record contains information such as employee s first and last names, employee s age, 'M' or 'F' for the employee s gender and the employee s hourly salary. We can define a structure type with tag, employee. Employee Information: First Name : Character array Last Name : Character array Age : Integer Gender : A character Hourly Salary : Double

6 Understanding Structures Page 416 Let s suppose that we are dealing with dates (calendar date). Any date can be expressed by three integer numbers corresponding to day, month, and year as in dd/mm/yyyy. We can then define the structure with three members of integer types. struct date { int month; int day; int year; }; It is more suitable to define a structure with three members if you are dealing with bunch of dates as like (date of birth, date of marriage, date of manufacture, expiration date and so on ). After defining a structure, it becomes easier to deal data types grouped in a structure. Many more examples can be given on structures (e.g. time (hh:mm:ss).) Remember: Members of a structure can be primitive data types and as well as another structure.

7 Page 418 Structures can be initialized using initializer list similar to initialization of elements of an array. Let s now consider again a structure for date (dd/mm/yyyy) struct date /*Defining a Structure with date tag after include statements*/ { int day; int month; int year; }date1,date2; /* Defining Two Variables with this Structure Type*/ OR initialization can be done while defining the variables as below }date1, date2 = {{1,1,2013},{22,5,2013}; In the above initialization List: date1: (dd/mm/yyyy) members corresponds 1/1/2013 date2: (dd/mm/yyyy) members corresponds 22/5/2013

8 Page 418 Another way to define to initialize a structures is first the structure is defined then the object with this type is defined and initialized later in the main program. Let s now consider again a structure for date (dd/mm/yyyy) struct date /*Defining a Structure with date tag after include statements*/ { int day; int month; int year; }; /* No Variable IS DEFINED YET. */ LATER UNDER MAIN PROGRAM defining two structure Variable struct date date1,date2; OR UNDER MAIN PROGRAM defining two structure Variable and initializing struct date date1,date2 = {{1,1,2013},{22,5,2013}; In the above initialization List: date1: (dd/mm/yyyy) members corresponds 1/1/2013 date2: (dd/mm/yyyy) members corresponds 22/5/2013 Note: Because structure variables can be defined later in the program. It is highly recommended to using the structure tag although it is optional.

9 Page 418 So far, we have learned how to define a structure, but didn t talk about how to use it. In order to use structures, we should be able to access the members of the structure. Two operators are used to access members of structures: 1. The structure member operator (. ) also called dot operator. VariableName.MemberName 2. The structure pointer operator (->) also called arrow operator. It is possible define a pointer to a structure. -> is used to access the member of the structure variable via pointers. PointerName->MemberName

10 Structure Member Operator. Page 418 Let s now consider the date structure (mm/dd/yyyy) struct date{ /*Defining Structure Type*/ int month; int day; int year; }; struct date today; Members of the today structure variables can be accessed via. operator. We can print, read, or assign any value to members by accessing them using. operator. today.month = 9; today.day = 25; today.year = 2004; Below printf statement prints the date in (mm/dd/yyyy) format. printf( %d/%d/%d, today.month, today.day, today.year);

11 Structure Pointer Operator -> Page 418 Let s now consider the segment of program struct date{ /*Defining Structure Type*/ int month; int day; int year; }; struct date today={9,25,2004}; /* Initialized */ struct date *dateptr=today; /* Defining a Pointer to date Structure */ Members of Date Structure can be accessed in Different Ways The members of the today structure variable can be accessed by using. today.month, today.day, today.month They can be also accessible by using the dateptr pointer. dateptr->month dateptr->day dateptr->year OR also can be accessed via pointer but using. operator. Two are the Same (*dateptr).month (*dateptr). day (*dateptr). year

12 Accessing Structure Members Out of Book Below program demonstrates accessing members of a structure directly and via a pointer. * Output of the Program * Note: instead of dtptr >day (*dtptr).day could be also used.

13 Accessing Structure Members Example- Page 419 * Output of the Program *

14 Page 420 In the programs using structures, "struct tag variable;" statement is used every time to define a new variable with this type. In order to avoid typing struct tag variable every time, C programming language allows programmers to define a synonym (or alias) to the "struct tag", so programmer don't need to type it every time. The keyword typedef creates synonyms (or aliases) for data types. Names for structure types are often defined with typedef to create shorter type names.

15 Page 420 Let s revisit the card structure example struct card {/* card structure definition */ char *face; /* define pointer face */ char *suit; /* define pointer suit */ }; /* end structure card */ // Now define structure Synonym usually the capital letter of the struct. tag typedef struct card Card; // Card is now Synonym struct card // Now on, we can use the structure Synonym to define a variable Card acard; // same as struct card acard; Card *cardptr; // same as struct card *cardptr; C programmers often use typedef to define a structure type, so a structure tag is not required. Synonym is then used to declare a variable with this type. As an example, the above structure definition can be done without tag. typedef struct { //Defining Structure without tag char *face; char *suit; } Card; /* Card is the Synonym to this structure */ Card acard; // Defines the type via the synonym

16 Swapping Structure Variables Out of Book Below program demonstrates copying a structure variable to another and usage of typedef command. * Output of the Program * Note: Copying a structure variable to another structure variable of the same type is very simple.

17 Page 420 It is possible to pass a structure or structures to functions in two ways. Structures may be passed to functions by passing individual structure members one by one (Call-by-Value). In this method, structure members are passed by value, therefore, the members of a caller s structure cannot be modified by the called function. It is also possible to pass the entire structure or by passing a pointer to the structure (Call-by-Reference). In this method, structure members are passed by reference (like memory address) and the caller's structure can be modified by the function (as like passing an array with reference). Passing structure by reference is a lot more efficient than passing structures by value. Passing a structure variable to a function is very similar to passing an array to a function.

18 More on Using Structures with Functions Page 420 Using structures/passing to a function and getting a feedback/return from a function can be used for may purposes. Because, call-by-reference is more efficient, it will under consideration in this slide. Segment of program given below demonstrates the usability of functions for different ways. struct card {/* card structure definition */ char *face; /* define pointer face */ char *suit; /* define pointer suit */ }; /* end structure card */ typedef struct card Card; // Card is now Synonym struct card // Function prototypes This are also the first lines of the functions - void function1( struct card a, int size); int function2( Card a, Card b, int size); char *function3( Card a ); struct card function 4 ( struct card a, int x, int y); Under main function, below lines shows how we can pass the structures. Let s assume person_a, person_b, and person_c are defined as structure variable of type card and x, y, xx, and yy are integer type of variables. function1(person_a); x = function2(person_a, person_b, y); printf( %s, function3(person_b); person_c = function4(person_a, xx, yy);

19 Structures with Functions Example- Out of Book Below program prints a given date in EU standard (dd/mm/yyyy) and US standard (mm/dd/yyyy). Each task is accomplished by separate functions. * Output of the Program *

20 Structures with Functions Example- Out of Book Below program demonstrates the usage of function to compare two dates. Function receives two date structure variables and returns the earlier date. * Output of the Program *

21 Arrays of Structures Page 421 In the previous example, we have used a structure variables to keep track of the employee card information for only two workers. We have defined the structure variables as; struct employee person_a; struct employee person_b; If we have too many employees then we can define a structure array variable by which we can deal with as many as employee records/information. It is very similar to array declaration; struct employee person[100]; This will create person[0],, person[99] and each one of them has members defined as person[i].name person[i].last person[i].age person[i].salary where 0 i < 100. (100 variables are defined).

22 Initialization of Arrays of Structures Page 423 Let s now consider the example d[3] array of structures to keep track of three dates employees information. struct date d[3]; //members are dd mm yyyy Members of the d[0], d[1],and d[2] can be read from keyboard or assigned within the program using the following. operator d[i].day d[i].month d[i].year where 0 i < 3. Also the members can be initialized at the time of declaration typedef struct date Date; /* Emp is synonym*/ struct date d[3]={{11,5,2004}}; Date d[3]={{11,5,2004}, {21,6,2004},{11,6,2012}}; /* inner braces are optional below line does the same */ Date d[3]={11,5,2004,21,6,2004,11,6,2012}; Date d[3]={{11,5,2004}, {21,6,2004}, {11,6,2012}}; Date d[3]={[2]= {11,6,2012}};

23 Structures with Functions Example- Out of Book Below program demonstrates the usage of array of structures to keep track of the five student exam results. Calculating avg. then printing them * Output of the Program *

24 Pointers and Arrays Example- Out of Book Array of Structures and pointers are strongly related. An array of structure can be pointed by a pointer and can be processed through this pointer. * Output of the Program *

25 Last Comment on Pointers and Arrays Out of Book In order to understand how actually pointers and arrays are related, we should look at how an array of structure is stored in the memory. Below an array of structure named months with type of month is defined. It holds the 3 Letters and Number of days in this month.** Right figure shows the array in the memory. struct month { //Defining Structure int numberofdays; char name[3]; }; struct month months[12] ={ {31,"Jan"}, {28,"Feb"}, {31,"Mar"},{30,"Apr"},{31,"May"},{30,"Jun"}, {31,"Jul"},{30,"Aug"},{30,"Sep"},{31,"Oct"}, {30,"Nov"},{31,"Dec"}}; //Pointing the forth element of the array of structure struct month *mptr = months[3]; mptr We can make mptr point next element by mptr++

26 Page 423 A union is a derived data type like a structure with members that share the same storage space. For different situations in a program, some variables may not be relevant, but other variables are so a union shares the space instead of wasting storage on variables that are not being used. Definition and usage of unions are similar to structure typedef union shape{ //Defining Union double radius; //Needs a size of 6 bytes double length; //Needs a size of 6 bytes double width; //Needs a size of 6 bytes }; // Total space is 6 bytes Note: Advantage of using union instead of structure. Size of the union unlike structure is adjusted according to the structure variable that requires the maximum usage from the members of the union. This helps on saving storage on the memory when dealing with large amount of data.

27 10.11 Enumeration Constants Page 438 As we have learned before, we can define constants in our program by using #define SIZE 10. User-defined data can be also defined by using enumeration. Each enumeration starts with enum statement. By default, enumeration starts with 0, unless specified otherwise. this creates enumeration constants with type of months with values from 0 to 11. (same like #define JAN 0.) enum statement allows user to define the starting value as below; Note: Enumeration constants doesn t take any storage on the memory and they are constant throughout the program.

28 Enumeration Example- Page 439 * Output of the Program *

29 Page 439 NOTE: Add a slide on nested structures See C programlama Dili Page 268 Unions are presented as separate chapter in C Programming Language 3 rd Edition See the referenced text and add some clear examples on the usage of unions.

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations Fundamentals of Programming Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations Instructor: Fatemeh Zamani f_zamani@ce.sharif.edu Sharif University of Technology Computer Engineering Department

More information

Programming for Engineers Structures, Unions

Programming for Engineers Structures, Unions Programming for Engineers Structures, Unions ICEN 200 Spring 2017 Prof. Dola Saha 1 Structure Ø Collections of related variables under one name. Ø Variables of may be of different data types. Ø struct

More information

OBJECT ORIENTED PROGRAMMING USING C++

OBJECT ORIENTED PROGRAMMING USING C++ OBJECT ORIENTED PROGRAMMING USING C++ Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1 Introduction 10.2 Structure Definitions 10.3 Initializing Structures 10.4 Accessing

More information

typedef int Array[10]; String name; Array ages;

typedef int Array[10]; String name; Array ages; Morteza Noferesti The C language provides a facility called typedef for creating synonyms for previously defined data type names. For example, the declaration: typedef int Length; Length a, b, len ; Length

More information

Fundamentals of Programming Session 24

Fundamentals of Programming Session 24 Fundamentals of Programming Session 24 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2014 These slides have been created using Deitel s slides Sharif University of Technology Outlines

More information

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5 1 What is structure? How to declare a Structure? Explain with Example Structure is a collection of logically related data items of different data types grouped together under a single name. Structure is

More information

Introduction. Structures, Unions, Bit Manipulations, and Enumerations. Structure. Structure Definitions

Introduction. Structures, Unions, Bit Manipulations, and Enumerations. Structure. Structure Definitions Introduction Structures, Unions, Bit Manipulations, and Enumerations In C, we can create our own data types If programmers do a good job of this, the end user does not even have to know what is in the

More information

IV Unit Second Part STRUCTURES

IV Unit Second Part STRUCTURES STRUCTURES IV Unit Second Part Structure is a very useful derived data type supported in c that allows grouping one or more variables of different data types with a single name. The general syntax of structure

More information

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -STRUCTURE- SPRING 2015 SEON-JU AHN, CNU EE

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -STRUCTURE- SPRING 2015 SEON-JU AHN, CNU EE 공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -STRUCTURE- SPRING 2015 SEON-JU AHN, CNU EE STRUCTURE Structures are collections of related variables under one name. Structures may contain variables of

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 10 Structures, Unions, Bit Manipulations and Enumerations Department of Computer Engineering

More information

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Pointers and Structure Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island 1 Pointer Variables Each variable in a C program occupies space in

More information

CS132 Algorithm. Instructor: Jialiang Lu Office: Information Center 703

CS132 Algorithm. Instructor: Jialiang Lu   Office: Information Center 703 CS132 Algorithm Instructor: Jialiang Lu Email: jialiang.lu@sjtu.edu.cn Office: Information Center 703 Chapter 3 STRUCTURES IN C 2 Structures Introduction Collections of related variables (aggregates) under

More information

CS561 Manju Muralidharan Priya Structures in C CS200 STRUCTURES. Manju Muralidharan Priya

CS561 Manju Muralidharan Priya Structures in C CS200 STRUCTURES. Manju Muralidharan Priya OBJECTIVES: CS200 STRUCTURES Manju Muralidharan Priya By the end of this class you will have understood: 1. Definition of a structure 2. Nested Structures 3. Arrays of structure 4. User defined data types

More information

UNIT-V. Structures. The general syntax of structure is given below: Struct <tagname> { datatype membername1; datatype membername2; };

UNIT-V. Structures. The general syntax of structure is given below: Struct <tagname> { datatype membername1; datatype membername2; }; UNIT-V Structures Structure is a very useful derived data type supported in c that allows grouping one or more variables of different data types with a single name. The general syntax of structure is given

More information

C PROGRAMMING Lecture 4. 1st semester

C PROGRAMMING Lecture 4. 1st semester C PROGRAMMING Lecture 4 1st semester 2017-2018 Structures Structure: Collection of one or more variables a tool for grouping heterogeneous elements together (different types) Array: a tool for grouping

More information

Chapter 7: Structuring the Data. Lecture7 1

Chapter 7: Structuring the Data. Lecture7 1 Chapter 7: Structuring the Data Lecture7 1 Topics Introduction to Structures Operations on Structures Using Structures with Arrays Using Structures with Pointers Passing Structures to and from Functions

More information

Chapter 8 STRUCTURES IN C

Chapter 8 STRUCTURES IN C Chapter 8 STRUCTURES IN C 1 Structures Introduction Collections of related variables (aggregates) under one name Can contain variables of different data types Commonly used to define records to be stored

More information

Structures. Today s Goals. Structure Operations. Structure Type Declaration. Struct Instance. typedef. CS246 Lec12. Structures

Structures. Today s Goals. Structure Operations. Structure Type Declaration. Struct Instance. typedef. CS246 Lec12. Structures Today s Goals Section 1 Structures Structures Types and variables typedef structs and pointers Unions Enumerations To group multiple (heterogeneous) variables Similar to Java classes, but not as powerful

More information

Structures and Unions in C. Alan L. Cox

Structures and Unions in C. Alan L. Cox Structures and Unions in C Alan L. Cox alc@rice.edu Administrivia Assignment 1 is due tonight Textbook Lectures begin covering material that is also covered by the textbook on 1/30 Assignment 3 (assigned

More information

Data Representation and Storage

Data Representation and Storage Data Representation and Storage Learning Objectives Define the following terms (with respect to C): Object Declaration Definition Alias Fundamental type Derived type Use size_t, ssize_t appropriately Use

More information

Dept. of Computer and Information Science (IDA) Linköpings universitet Sweden

Dept. of Computer and Information Science (IDA) Linköpings universitet Sweden Dept. of Computer and Information Science (IDA) Linköpings universitet Sweden Structures Unions Endianness Bit field Bit manipulation Collections of related variables (aggregates) under one name Can contain

More information

BLM2031 Structured Programming. Zeyneb KURT

BLM2031 Structured Programming. Zeyneb KURT BLM2031 Structured Programming Zeyneb KURT 1 Contact Contact info office : D-219 e-mail zeynebkurt@gmail.com, zeyneb@ce.yildiz.edu.tr When to contact e-mail first, take an appointment What to expect help

More information

CHAPTER 4 Structures

CHAPTER 4 Structures CHAPTER 4 Structures Page 1 Structures: Arrays are one of the most widely used data structures in programming languages. The limitation of arrays, however, is that all the elements must be of the same

More information

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information Laboratory 2: Programming Basics and Variables Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information 3. Comment: a. name your program with extension.c b. use o option to specify

More information

A Fast Review of C Essentials Part I

A Fast Review of C Essentials Part I A Fast Review of C Essentials Part I Structural Programming by Z. Cihan TAYSI Outline Program development C Essentials Functions Variables & constants Names Formatting Comments Preprocessor Data types

More information

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple

More information

Tin học cơ sở 4$ Structures!

Tin học cơ sở 4$ Structures! Tin học cơ sở 4$ Structures! Outline$ Structured Data types! Type definitions! Self-referential types: linked list! Structure$ Many structured collections of data are:! Heterogeneous (components are different

More information

Student Name: (in Capital Letters) CSE Introduction to Programming for Engineers and Scientists. Final Exam

Student Name: (in Capital Letters) CSE Introduction to Programming for Engineers and Scientists. Final Exam Student Name: (in Capital Letters) CSE 1311 Introduction to Programming for Engineers and Scientists Final Exam Fall 2013 1 1. If count is a properly defined integer variable, the following piece of code:

More information

by: Lizawati, Norhidayah & Muhammad Noorazlan Shah Computer Engineering, FKEKK, UTeM

by: Lizawati, Norhidayah & Muhammad Noorazlan Shah Computer Engineering, FKEKK, UTeM by: Lizawati, Norhidayah & Muhammad Noorazlan Shah Computer Engineering, FKEKK, UTeM At the end of this chapter, the students should be able to: understand and apply typedef understand and apply structure

More information

Programming in C. What is C?... What is C?

Programming in C. What is C?... What is C? Programming in C UVic SEng 265 C Developed by Brian Kernighan and Dennis Ritchie of Bell Labs Earlier, in 1969, Ritchie and Thompson developed the Unix operating system We will be focusing on a version

More information

Downloaded S. from Kiran, PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from

Downloaded S. from Kiran,  PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from Downloaded S. from Kiran, www.studiestoday.com PGT (CS) KV, STRUCTURES WHAT IS A STRUCTURE? Structure is a collection of logically related data. It is also a collection of dissimilar datatype. Downloaded

More information

CS Programming In C

CS Programming In C CS 24000 - Programming In C Week Two: Basic C Program Organization and Data Types Zhiyuan Li Department of Computer Science Purdue University, USA 2 int main() { } return 0; The Simplest C Program C programs

More information

Structures and Union. Fall Jinkyu Jeong GEBD029: Basis and Practice in Programming Fall 2014 Jinkyu Jeong

Structures and Union. Fall Jinkyu Jeong GEBD029: Basis and Practice in Programming Fall 2014 Jinkyu Jeong Structures and Union Fall 2014 Jinkyu Jeong (jinkyu@skku.edu) 1 Review Bitwise operations You need them for performance in terms of space and time Shifts are equivalent to arithmetics Enumeration you can

More information

User Defined data Types

User Defined data Types User Defined data Types typedef datatype user_defined_data_type typedef int salary; salary emp1,emp2; salary x; typedef char sentence[50]; sentence header,footer; char header[50],footer[50]; 5/1/2006 Computer

More information

Input And Output of C++

Input And Output of C++ Input And Output of C++ Input And Output of C++ Seperating Lines of Output New lines in output Recall: "\n" "newline" A second method: object endl Examples: cout

More information

ALQUDS University Department of Computer Engineering

ALQUDS University Department of Computer Engineering 2013/2014 Programming Fundamentals for Engineers Lab Lab Session # 8 Structures, and Enumeration ALQUDS University Department of Computer Engineering Objective: After completing this lab, the students

More information

Compiling and Running a C Program in Unix

Compiling and Running a C Program in Unix CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 95 ] Compiling and Running a C Program in Unix Simple scenario in which your program is in a single file: Suppose you want to name

More information

Relationship between Pointers and Arrays

Relationship between Pointers and Arrays Relationship between Pointers and Arrays Arrays and pointers are intimately related in C and often may be used interchangeably. An array name can be thought of as a constant pointer. Pointers can be used

More information

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University CS558 Programming Languages Winter 2018 Lecture 4a Andrew Tolmach Portland State University 1994-2018 Pragmatics of Large Values Real machines are very efficient at handling word-size chunks of data (e.g.

More information

C Structures Self-referencing Card Shuffling Unions. C Structures CS Prof. Jonathan Ventura. Prof. Jonathan Ventura C Structures

C Structures Self-referencing Card Shuffling Unions. C Structures CS Prof. Jonathan Ventura. Prof. Jonathan Ventura C Structures Self-referencing Card Shuffling Unions CS 2060 Self-referencing Card Shuffling Unions : struct C enables us to aggregate diverse data types into a structure: struct Employee { char first_name[20]; char

More information

Programming in C. What is C?... What is C?

Programming in C. What is C?... What is C? C Programming in C UVic SEng 265 Developed by Brian Kernighan and Dennis Ritchie of Bell Labs Earlier, in 1969, Ritchie and Thompson developed the Unix operating system We will be focusing on a version

More information

CSE 230 Intermediate Programming in C and C++

CSE 230 Intermediate Programming in C and C++ CSE 230 Intermediate Programming in C and C++ Unions and Bit Fields Fall 2017 Stony Brook University Instructor: Shebuti Rayana http://www3.cs.stonybrook.edu/~cse230/ Union Like structures, unions are

More information

Programming in C UVic SEng 265

Programming in C UVic SEng 265 Programming in C UVic SEng 265 Daniel M. German Department of Computer Science University of Victoria 1 SEng 265 dmgerman@uvic.ca C Developed by Brian Kernighan and Dennis Ritchie of Bell Labs Earlier,

More information

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017 C Concepts - I/O Lecture 19 COP 3014 Fall 2017 November 29, 2017 C vs. C++: Some important differences C has been around since around 1970 (or before) C++ was based on the C language While C is not actually

More information

Example: Structure, Union. Syntax. of Structure: struct book { char title[100]; char author[50] ]; float price; }; void main( )

Example: Structure, Union. Syntax. of Structure: struct book { char title[100]; char author[50] ]; float price; }; void main( ) Computer Programming and Utilization ( CPU) 110003 Structure, Union 1 What is structure? How to declare a Structure? Explain with Example Structure is a collection of logically related data items of different

More information

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language 1 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC

More information

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 12: Structures Readings: Chapter 11 Structures (1/2) A structure is a collection of one

More information

Programming. Structures, enums and unions

Programming. Structures, enums and unions Programming Structures, enums and unions Summary } Structures } Declaration } Member access } Function arguments } Memory layout } Array of structures } Typedef } Enums } Unions 2 Idea! } I want to describe

More information

Structure, Union. Ashishprajapati29.wordpress.com. 1 What is structure? How to declare a Structure? Explain with Example

Structure, Union. Ashishprajapati29.wordpress.com. 1 What is structure? How to declare a Structure? Explain with Example Structure, Union 1 What is structure? How to declare a Structure? Explain with Example Structure s a collection of logically related data items of different data types grouped together under a single name.

More information

Programming 2. Object Oriented Programming. Daniel POP

Programming 2. Object Oriented Programming. Daniel POP Programming 2 Object Oriented Programming Daniel POP Week 2 Agenda 1. Basic OOP concepts 1. Class 2. Object 3. Message 2. Scopes 3. Classes 1. Declaration 2. Implementation 3. Accessing class members 4.

More information

Low-Level Programming in C

Low-Level Programming in C Low-Level Programming in C Menu C Programming Language Pointers in C Pointer Arithmetic Type checking in C 2 No support for: C Language Array bounds checking Null dereferences checking Data abstraction,

More information

Structures. Lecture 15 COP 3014 Spring April 16, 2018

Structures. Lecture 15 COP 3014 Spring April 16, 2018 Structures Lecture 15 COP 3014 Spring 2018 April 16, 2018 Motivation We have plenty of simple types for storing single items like numbers, characters. But is this really enough for storing more complex

More information

Structures Unions and Enumerated Datatypes 224

Structures Unions and Enumerated Datatypes 224 STRUCTURES, UNIONS AND ENUMERATED DATA TYPES LEARNING OBJECTIVES After reading this chapter, the readers will be able to understand the purpose of the user defined data types Structures, Unions and Enumerated

More information

Data Structures Unit 02

Data Structures Unit 02 Data Structures Unit 02 Bucharest University of Economic Studies Memory classes, Bit structures and operators, User data types Memory classes Define specific types of variables in order to differentiate

More information

Lab Session # 3 Conditional Statements. ALQUDS University Department of Computer Engineering

Lab Session # 3 Conditional Statements. ALQUDS University Department of Computer Engineering 2013/2014 Programming Fundamentals for Engineers Lab Lab Session # 3 Conditional Statements ALQUDS University Department of Computer Engineering Objective: Our objective for today s lab session is to introduce

More information

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS Manish Dronacharya College Of Engineering, Maharishi Dayanand University, Gurgaon, Haryana, India III. Abstract- C Language History: The C programming language

More information

M.EC201 Programming language

M.EC201 Programming language Power Engineering School M.EC201 Programming language Lecture 13 Lecturer: Prof. Dr. T.Uranchimeg Agenda The union Keyword typedef and Structures What Is Scope? External Variables 2 The union Keyword The

More information

12 CREATING NEW TYPES

12 CREATING NEW TYPES Lecture 12 CREATING NEW TYPES of DATA Typedef declaration Enumeration Structure Bit fields Uninon Creating New Types Is difficult to solve complex problems by using programs written with only fundamental

More information

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14 C introduction Variables Variables 1 / 14 Contents Variables Data types Variable I/O Variables 2 / 14 Usage Declaration: t y p e i d e n t i f i e r ; Assignment: i d e n t i f i e r = v a l u e ; Definition

More information

Variables and Constants

Variables and Constants HOUR 3 Variables and Constants Programs need a way to store the data they use. Variables and constants offer various ways to work with numbers and other values. In this hour you learn: How to declare and

More information

Chapter-8 DATA TYPES. Introduction. Variable:

Chapter-8 DATA TYPES. Introduction. Variable: Chapter-8 DATA TYPES Introduction To understand any programming languages we need to first understand the elementary concepts which form the building block of that program. The basic building blocks include

More information

Type Definition. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

Type Definition. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18 Enum and Struct Type Definition C Types Derived Function Array Pointer Structure Union Enumerated 2 tj Type Definition Typedef Define a new Type Inherits members and operations from a standard or previously

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Fall 2016 Lecture 4a Andrew Tolmach Portland State University 1994-2016 Pragmatics of Large Values Real machines are very efficient at handling word-size chunks of data (e.g.

More information

3.3 Structures. Department of CSE

3.3 Structures. Department of CSE 3.3 Structures 1 Department of CSE Objectives To give an introduction to Structures To clearly distinguish between Structures from Arrays To explain the scenarios which require Structures To illustrate

More information

Lexical and Syntax Analysis

Lexical and Syntax Analysis Lexical and Syntax Analysis (of Programming Languages) Abstract Syntax Lexical and Syntax Analysis (of Programming Languages) Abstract Syntax What is Parsing? Parser String of characters Data structure

More information

8. Structures, File I/O, Recursion. 18 th October IIT Kanpur

8. Structures, File I/O, Recursion. 18 th October IIT Kanpur 8. Structures, File I/O, Recursion 18 th October IIT Kanpur C Course, Programming club, Fall 2008 1 Basic of Structures Definition: A collection of one or more different variables with the same handle

More information

Chapter 11: Structured Data

Chapter 11: Structured Data Chapter 11: Structured Data 11.1 Abstract Data Types (ADT's) Abstract Data Types A data type that specifies values that can be stored attributes operations that can be done on the values behaviors User

More information

Computer System and programming in C

Computer System and programming in C Computer System and programming in C 1 C structures: aggregate, yet scalar aggregate in that they hold multiple data items at one time named members hold data items of various types like the notion of

More information

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02 Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02 Hello, in this lecture we will learn about some fundamentals concepts of java.

More information

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are

More information

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No. Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No. # 5 Structured Query Language Hello and greetings. In the ongoing

More information

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng Slide Set 6 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2017 ENCM 339 Fall 2017 Section 01 Slide

More information

.:: UNIT 1 ::. INTRODUCTION TO ALGORITHM & DATA STRUCTURES

.:: UNIT 1 ::. INTRODUCTION TO ALGORITHM & DATA STRUCTURES .:: UNIT 1 ::. INTRODUCTION TO ALGORITHM & DATA STRUCTURES 1.1 Algorithm: A clearly specified finite set of instructions a computer follows to solve a problem. An algorithm is a well-ordered collection

More information

Ch. 10: Name Control

Ch. 10: Name Control Ch. 10: Name Control Static elements from C The static keyword was overloaded in C before people knew what the term overload meant, and C++ has added yet another meaning. The underlying concept with all

More information

F4104 ALGORITHM & DATA STRUCTURE

F4104 ALGORITHM & DATA STRUCTURE F4104 ALGORITHM & DATA STRUCTURE.:: UNIT 1::. INTRODUCTION TO ALGORITHM & DATA STRUCTURES 1.1 ALGORITHM CONCEPTS ALGORITHM Algorithm: A clearly specified finite set of instructions a computer follows to

More information

Programming in C. Pointers and Arrays

Programming in C. Pointers and Arrays Programming in C Pointers and Arrays NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313 http://www.csee.umbc.edu/courses/undergraduate/313/fall11/" Pointers and Arrays In C, there is a strong relationship

More information

COMP322 - Introduction to C++

COMP322 - Introduction to C++ COMP322 - Introduction to C++ Winter 2011 Lecture 2 - Language Basics Milena Scaccia School of Computer Science McGill University January 11, 2011 Course Web Tools Announcements, Lecture Notes, Assignments

More information

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types Chapter 6 Topics WEEK E FOUR Data Types Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative Arrays Record Types Union Types Pointer and Reference

More information

UNIT-IV. Structure is a user-defined data type in C language which allows us to combine data of different types together.

UNIT-IV. Structure is a user-defined data type in C language which allows us to combine data of different types together. UNIT-IV Unit 4 Command Argument line They are parameters/arguments supplied to the program when it is invoked. They are used to control program from outside instead of hard coding those values inside the

More information

Syntax and Variables

Syntax and Variables Syntax and Variables What the Compiler needs to understand your program, and managing data 1 Pre-Processing Any line that starts with # is a pre-processor directive Pre-processor consumes that entire line

More information

C Programming for Engineers Functions

C Programming for Engineers Functions C Programming for Engineers Functions ICEN 360 Spring 2017 Prof. Dola Saha 1 Introduction Real world problems are larger, more complex Top down approach Modularize divide and control Easier to track smaller

More information

Structured Data. CIS 15 : Spring 2007

Structured Data. CIS 15 : Spring 2007 Structured Data CIS 15 : Spring 2007 Functionalia HW4 Part A due this SUNDAY April 1st: 11:59pm Reminder: I do NOT accept LATE HOMEWORK. Today: Dynamic Memory Allocation Allocating Arrays Returning Pointers

More information

C Language, Token, Keywords, Constant, variable

C Language, Token, Keywords, Constant, variable C Language, Token, Keywords, Constant, variable A language written by Brian Kernighan and Dennis Ritchie. This was to be the language that UNIX was written in to become the first "portable" language. C

More information

Chapter-14 STRUCTURES

Chapter-14 STRUCTURES Chapter-14 STRUCTURES Introduction: We have seen variables of simple data types, such as float, char, and int. Variables of such types represent one item of information: a height, an amount, a count, and

More information

CS 31 Discussion 1A, Week 8. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

CS 31 Discussion 1A, Week 8. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m. CS 31 Discussion 1A, Week 8 Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m. Today s focus Pointer Structure Clarifications Pointer A pointer is the memory address of a variable.

More information

Lexical and Syntax Analysis. Abstract Syntax

Lexical and Syntax Analysis. Abstract Syntax Lexical and Syntax Analysis Abstract Syntax What is Parsing? Parser String of characters Data structure Easy for humans to write Easy for programs to process A parser also checks that the input string

More information

Chapter 7 C Pointers

Chapter 7 C Pointers Chapter 7 C Pointers Objectives of This Chapter Definition and Operations with Pointers Using Pointers to pass arguments as call by reference call. Using Pointers to deal with arrays and strings. Character

More information

The New C Standard (Excerpted material)

The New C Standard (Excerpted material) The New C Standard (Excerpted material) An Economic and Cultural Derek M. Jones derek@knosof.co.uk Copyright 2002-2008 Derek M. Jones. All rights reserved. 1456 6.7.2.3 Tags 6.7.2.3 Tags type contents

More information

MYcsvtu Notes LECTURE 34. POINTERS

MYcsvtu Notes LECTURE 34.  POINTERS LECTURE 34 POINTERS Pointer Variable Declarations and Initialization Pointer variables Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointers contain

More information

Binghamton University. CS-211 Fall Variable Scope

Binghamton University. CS-211 Fall Variable Scope Variable Scope 1 Scope The places in your code that can read and/or write a variable. Scope starts at the location where you declare the variable There may be holes in the scope! Scope ends at the end

More information

What is a Structure? related data items. Examples: n Used for handling a group of logically

What is a Structure? related data items. Examples: n Used for handling a group of logically Structures 1 What is a Structure? n Used for handling a group of logically related data items Examples: n Student name, roll number, and marks n Real part and complex part of a complex number n Helps in

More information

Binghamton University. CS-211 Fall Variable Scope

Binghamton University. CS-211 Fall Variable Scope Variable Scope 1 Scope The places in your code that can read and/or write a variable. Scope starts at the location where you declare the variable There may be holes in the scope! Scope ends at the end

More information

High Performance Computing

High Performance Computing High Performance Computing MPI and C-Language Seminars 2009 Photo Credit: NOAA (IBM Hardware) High Performance Computing - Seminar Plan Seminar Plan for Weeks 1-5 Week 1 - Introduction, Data Types, Control

More information

Procedures, Parameters, Values and Variables. Steven R. Bagley

Procedures, Parameters, Values and Variables. Steven R. Bagley Procedures, Parameters, Values and Variables Steven R. Bagley Recap A Program is a sequence of statements (instructions) Statements executed one-by-one in order Unless it is changed by the programmer e.g.

More information

Data Representation and Storage. Some definitions (in C)

Data Representation and Storage. Some definitions (in C) Data Representation and Storage Learning Objectives Define the following terms (with respect to C): Object Declaration Definition Alias Fundamental type Derived type Use pointer arithmetic correctly Explain

More information

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

COMP322 - Introduction to C++ Lecture 02 - Basics of C++ COMP322 - Introduction to C++ Lecture 02 - Basics of C++ School of Computer Science 16 January 2012 C++ basics - Arithmetic operators Where possible, C++ will automatically convert among the basic types.

More information

C: How to Program. Week /Mar/05

C: How to Program. Week /Mar/05 1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013 CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013 TOPICS TODAY Reminder: MIDTERM EXAM on THURSDAY Pointer Basics Pointers & Arrays Pointers & Strings Pointers & Structs

More information

Computer Systems Principles. C Pointers

Computer Systems Principles. C Pointers Computer Systems Principles C Pointers 1 Learning Objectives Learn about floating point number Learn about typedef, enum, and union Learn and understand pointers 2 FLOATING POINT NUMBER 3 IEEE Floating

More information