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

Similar documents
12 CREATING NEW TYPES

IV Unit Second Part STRUCTURES

Programming. Structures, enums and unions

Lecture10: Structures, Unions and Enumerations 11/26/2012

BLM2031 Structured Programming. Zeyneb KURT

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

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

Programming in C UVic SEng 265

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

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

For each of the following variables named x, specify whether they are static, stack-dynamic, or heapdynamic:

Lecture 3. The syntax for accessing a struct member is

In this session we will cover the following sub-topics: 1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.

Fundamental of Programming (C)

Basic Elements of C. Staff Incharge: S.Sasirekha

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

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

Unit IV & V Previous Papers 1 mark Answers

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Computer Systems Principles. C Pointers

Chapter 7: Structuring the Data. Lecture7 1

C Programming Review CSC 4320/6320

ECET 264 C Programming Language with Applications. Lecture 13. Structures and Unions

Unit 8. Structures and Unions. School of Science and Technology INTRODUCTION

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

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

A Fast Review of C Essentials Part I

C PROGRAMMING Lecture 4. 1st semester

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Syntax and Variables

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

UNIT - V STRUCTURES AND UNIONS

PROGRAMMAZIONE I A.A. 2017/2018

Arrays. Systems Programming Concepts

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

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

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

The syntax of structure declaration is. struct structure_name { type element 1; type element 2; type element n;

3.3 Structures. Department of CSE

PROGRAMMAZIONE I A.A. 2018/2019

Chapter-8 DATA TYPES. Introduction. Variable:

Language comparison. C has pointers. Java has references. C++ has pointers and references

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

COMP 2355 Introduction to Systems Programming

ALQUDS University Department of Computer Engineering

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

Department of Computer Science and Engineering. Programming for Problem Solving. I Year B.Tech. (I - Sem) Assistant Professor (M.

Data Type. structure. C Programming Lecture 11 : Structuret. struct union. typedef. A collection of one or more variables

Francesco Nidito. Programmazione Avanzata AA 2007/08

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

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

CS 61c: Great Ideas in Computer Architecture

Structures Unions and Enumerated Datatypes 224

Programming for Engineers Structures, Unions

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

ARRAYS(II Unit Part II)

Programming in C - Part 2

Pointers and Arrays 1

Variation of Pointers

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

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

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

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

Tokens, Expressions and Control Structures

COMP322 - Introduction to C++

M1-R4: Programing and Problem Solving using C (JAN 2019)

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

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

First of all, it is a variable, just like other variables you studied

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Chapter 10 C Structures, Unions, Bit Manipulations

EM108 Software Development for Engineers

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

Chapter 21: Introduction to C Programming Language

Character Strings. String-copy Example

Midterm Examination # 2 Wednesday, March 19, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Subject: Fundamental of Computer Programming 2068

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

CS3157: Advanced Programming. Announcement

CMPE-013/L. Introduction to C Programming

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

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

Introduction to C++ with content from

M.EC201 Programming language

Object-Oriented Principles and Practice / C++

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

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

Bit Manipulation in C

Enumerated Types. Mr. Dave Clausen La Cañada High School

PROGRAMMAZIONE I A.A. 2017/2018

CSE 230 Intermediate Programming in C and C++

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

Structures. Dr. Donald Davendra Ph.D. (Department of Computing Science, Structures FEI VSB-TU Ostrava)

Computer Programming Unit 3

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

Technical Questions. Q 1) What are the key features in C programming language?

Review of the C Programming Language for Principles of Operating Systems

Transcription:

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 (struct) understand and apply Enumeration (enum) understand and apply union

User Define Data Type (typedef) Structured Type definition, initialization, assignment pointer to structures nested structures arrays of structures structures and functions Enumerated Type Union Type

Derived Types Arrays Pointers Structure Enumerated Union Chapter 5 Chapter 7

creating synonyms for previously defined data type names is defined using the keyword typedef and the syntax is: typedef daya_type variable_name;

Example: typedef int LENGTH; /*LENGTH is a synonym for the data type int*/ LENGTH a, b, len; LENGTH numbers[10];

Structure is a collection of variables that are functionally related to each other group of related values the group is treated as a single unit each member of structure is a field each field of the structure has a specific type different fields may have either the same or different types

Group Field1 Field2 Field3 Field4 Field 1 Field 2 id name CGPA Field 3 student Structure

Declaration & definition: keyword struct Most common declaration method describe as follows: 1. Declaring a tag name and then using the tag name to declare actual variables This form is more commonly used, especially for a large program struct tagname type 1 fieldname 1 ; type 2 fieldname 2 ; type n fieldname n ; }; struct tagname varname; struct STUDENT int id; char name[30]; float CGPA; }; struct STUDENT benc, bene; benc & bene is declared as variable of type struct STUDENT with 3 members, id, name and CGPA

2. Declaring a structure without a tag name This form is useful if the structure is used only in one place struct type 1 fieldname 1 ; type 2 fieldname 2 ; type n fieldname n ; } varname; struct int id; char name[30]; float CGPA; }benc, bene;

3. Declaring a structure with a tag name and variables struct tagname type 1 fieldname 1 ; type 2 fieldname 2 ; type n fieldname n ; } varname; struct STUDENT int id; char name[30]; float CGPA; }benc, bene; benc & bene is declared as variable of type struct STUDENT

typedef of structure declaration & definition: typedef struct tagname type 1 fieldname 1 ; type 2 fieldname 2 ; type n fieldname n ; } typename; typename varname; typedef struct STUDENT int id; char name[30]; float CGPA; } stud; stud benc, bene; Create a data type call stud. benc & bene is declared as variable of typedef stud. Keyword struct can be ignore since stud is not normal structure

Initialization: struct tagname varname = fieldval 1, fieldval 2,..., fieldval n }; struct STUDENT char id[8]; char name[30]; float CGPA; }; struct STUDENT stud1= 0123, Anne, 3.98}; 0123 Anne 3.98 id name CGPA struct STUDENT stud2= 0123 }; 0123 id \0 name 0.0 CGPA

Accessing Structures: dot (.) form to refer to field of a structure each field treated as an individual variable varname.fieldname typedef struct DATE int month, day, year; } date; month d1 day year void main() date d1; 12 2 1970 d1.month = 12; d1.day = 2; d1.year = 1970; }

Structure Assignment: variables must be of same type (same name) values are copied one at a time from field to corresponding field typedef struct DATE int month, day, year; } date; month 12 d1 day 2 year 1970 date d1 = 12, 2, 1970 }; date d2; d2 = d1; /* Assignment */ d2.day = d1.day; month 12 d2 day 2 year 1970

used to reference structures can only stored the address of a structure struct STUDENT char id[8]; char name[30]; float CGPA; }; struct STUDENT stud1 = 0123, Anne, 3.98}; struct STUDENT *ptr; //declare a pointer ptr = &stud1;//assign address of stud1 ptr 0123 Anne name 3.98 id name CGPA

Accessing Pointer Structures: (*pointername). fieldname Or pointername -> fieldname printf ( Student CGPA : %.2f, stud1.cgpa); or printf ( Student CGPA : %.2f, (*ptr).cgpa); or printf ( Student CGPA : %.2f, ptr->cgpa);

Example: a group of students and the data stored in a structure Create array of structure: struct STUDENT stuary[30]; Initialize for two students: stuary = 1001, Ahmad, 2.98}, 1002, Siti, 3.01}};

Access data for a student: refer to the structure name with an index or a pointer stuary[i] or *pstu

Example: compute CGPA of all students float totcgpa; float avrcgpa; int i; struct STUDENT stuary[30]; accessing using index for (i=0; i<30; i++) totcgpa += stuary[i].cgpa; avrcgpa = totcgpa/30; accessing using pointer float totcgpa; float avrcgpa; struct STUDENT *pstu, *plaststu; plaststu = stuary + 29; for (pstu=stuary; pstu<=plaststu; pstu++) totcgpa += (*pstu).cgpa; avrcgpa = totcgpa/30;

a structure includes another structure stamp.date.month stamp.time.min month day year hour min sec date time stamp.date stamp stamp.time struct DATE int day; int month; int year; }; struct TIME int hour; int min; int sec; }; struct STAMP struct DATE date; struct TIME time; }; struct STAMP stamp;

Accessing nested structures include each level from the highest to the component being referenced stamp stamp.date stamp.date.month stamp.time stamp.time.sec

Nested structure initialization: each structure must be initialized completely before proceeding to the next member each structure is enclosed in a set of braces ( }) struct STAMP stamp = 12, 06, 1983}, 23, 35, 05}};

Two methods to pass structures as parameters of a function: Passing by value passing a copy of the entire structure suitable if the structure is very small to make sure the called function does not change the structure being passed Passing by reference passing the address of the structure faster and more convenient method

#include <stdio.h> struct STUDENT char id[8]; char name[30]; float CGPA; }stud1 = "0123", "Anne", 3.98}; void func(struct STUDENT s, struct STUDENT *sp) printf("s.id = %s\n", s.id); printf("s.name = %s\n", s.name); printf("s.cgpa = %f\n", s.cgpa); } printf("sp->id = %s\n", sp->id); printf("sp->name = %s\n", sp->name); printf("sp->cgpa = %f\n", sp->cgpa);

int main() struct STUDENT stud2="0124", "Joe", 3.25}; } //passed structure stud1 and address of stud2 to func() func(stud1, &stud2); return 0;

The return type of the function must match the actual returned value One advantage of returning a structure is the ability to return more than one value However, function passing as reference is more commonly used than functions returning structures

#include <stdio.h> #include <string.h> struct STUDENT int id; char name[30]; float CGPA; }; /* function returns a structure */ struct STUDENT func(int id) struct STUDENT s1; s1.id = id + 1; strcpy(s1.name, "Anne"); //copy string Anne to s1.name s1.cgpa = 2.98; } return s1;

int main() struct STUDENT stud1=1000, "Joe", 3.25}, stud2; stud2 = func(stud1.id); //assign the returned structure to stud2 printf("stud2.id = %d\n", stud1.id); printf("stud2.name = %s\n", stud2.name); printf("stud2.cgpa = %f\n", stud2.cgpa); } return 0;

Enumerated types: is derived from the integer type is defined using the keyword enum and the syntax is: enum tag_name name_0, name_1,, name_n }; the names in an enumerated type are replaced by integer values by the compiler

the values in an enum start with 0, unless specified otherwise, and are incremented by 1. enum days Mon, Tue, Wed, Thu, Fri, Sat, Sun }; Mon is 0, Tue is 1, Wed is 2, etc give different initial value enum days Mon = 1, Tue, Wed, Thu, Fri, Sat, Sun }; Mon is 1, Tue is 2, Wed is 3, etc

give a value for each enum numbers First = 1, Third = 3, Fifth = 5}; Three thoughts about enumerated types: You can assign the same integer to multiple enumeration constants in the same definition enum colors green, blue, purple, yellow = 1}; assign same number to blue and yellow

You can assign an enumerated type to an integer enum colors green=2, blue, purple, yellow = 1}; enum colors shirt; int x; shirt = purple; x = purple; //OK. x is 4

Example 1: enum days Sun, Mon, Tue, Wed, Thu, Fri, Sat }; int day; for (day = Sun; day <= Sat; day++) if ((day == Sun) (day == Sat)) printf( Weekend\n ); else printf( Weekday\n ); } printf( %d\n, Wed); //Would print 3

Example 2: #include<stdio.h> int main() enum sport soccer=3, bowling=5, tenis, badminton}; enum sport game; game = badminton; } switch(game) case (3): printf("soccer"); break; case (5): printf("bowling"); break; case (6): printf("tenis"); break; case (7): printf("badminton"); break; } return 0;

Union: like a structure, a union is also a derived data type the members of a union share a single storage space only ONE member of each union can be referenced at a time amount of space allocated for storage is the amount needed for the largest member of the union

Union vs. Structure union NUMBER int x; float y; } union NUMBER val; struct NUMBER int x; float y; } struct NUMBER val; 4 byte storage (float) val x or y 6 byte storage (float + int) val x y

Example: #include<stdio.h> union number int x; float y; float z; }; void main() union number val; printf ("size of union is %d\n", sizeof(val)); //size of union is 4 val.x = 5; printf("x is %d\n", val.x); //x is 5

val.y = 100.25; printf("y is %.2f\n", val.y); //y is 100.25 val.z = 200.50; printf("z is %.2f\n", val.z); //z is 200.50 } printf("y is %.2f\n", val.y); //y is 200.50

Can you differentiate between enum, struct and union? apply enum, struct and union?