Computer System and programming in C

Similar documents
Programming. Structures, enums and unions

CS240: Programming in C

by Pearson Education, Inc. All Rights Reserved.

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

Review of the C Programming Language for Principles of Operating Systems

Short Notes of CS201

Review of the C Programming Language

CS201 - Introduction to Programming Glossary By

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

CS240: Programming in C

Type Checking. Prof. James L. Frankel Harvard University

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

CSE 230 Intermediate Programming in C and C++

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

12 CREATING NEW TYPES

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

INTRODUCTION 1 AND REVIEW

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC.

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

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Goals of this Lecture

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

Variables. Data Types.

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

[0569] p 0318 garbage

High Performance Computing in C and C++

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

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

C Programming Review CSC 4320/6320

C Language Advanced Concepts. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

Input And Output of C++

Operating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan

Fundamental of Programming (C)

QUIZ. What is wrong with this code that uses default arguments?

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

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

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

>B<82. 2Soft ware. C Language manual. Copyright COSMIC Software 1999, 2001 All rights reserved.

a data type is Types

Low-Level Programming

Pointers, Dynamic Data, and Reference Types

EL6483: Brief Overview of C Programming Language

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

QUIZ. What are 3 differences between C and C++ const variables?

Unit 7. Functions. Need of User Defined Functions

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Data Structures Unit 02

C-LANGUAGE CURRICULAM

A flow chart is a graphical or symbolic representation of a process.

Tokens, Expressions and Control Structures

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Variation of Pointers

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Ch. 12: Operator Overloading

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

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

A Fast Review of C Essentials Part I

Chapter 7: Structuring the Data. Lecture7 1

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

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

Single-pass Static Semantic Check for Efficient Translation in YAPL

Contents of Lecture 3

S-1. Concepts this lecture. CSE 142 Computer Programming I. Chapter 11. Review: Data Structures. Problem: Account Records.

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

DECLARAING AND INITIALIZING POINTERS

C Language, Token, Keywords, Constant, variable

Pointer Declarations

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

APPENDIX A : Example Standard <--Prev page Next page -->

Prof. Carl Schultheiss MS, PE. CLASS NOTES Lecture 12

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

3/7/2018. Sometimes, Knowing Which Thing is Enough. ECE 220: Computer Systems & Programming. Often Want to Group Data Together Conceptually

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

UNIT- 3 Introduction to C++

OBJECT ORIENTED PROGRAMMING USING C++

The New C Standard (Excerpted material)

C Praktikum. Advanced Pointers. Eugen Betke, Nathanael Hübbe, Michael Kuhn, Jakob Lüttgau, Jannek Squar

Extending SystemVerilog Data Types to Nets

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

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

Structures, Unions, and Typedefs

Computer Systems Principles. C Pointers

Pointers as Arguments

Structures, Operators

C Programming Language (Chapter 2 of K&R) Variables and Constants

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

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

The New C Standard (Excerpted material)

CMPE-013/L. Introduction to C Programming

Lecture 02 C FUNDAMENTALS

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

Chapter 1 Getting Started

Motivation was to facilitate development of systems software, especially OS development.

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Introduction to C++ with content from

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

C++ Programming Chapter 7 Pointers

Programming for Engineers Structures, Unions

Transcription:

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 class/field in C or C++ but without the data hiding features scalar in that C treats each structure as a unit as opposed to the array approach: a pointer to a collection of members in memory entire structures (not just pointers to structures) may be passed as function arguments, assigned to variables, etc. Interestingly, they cannot be compared using == (rationale: too inefficient) Computer System and programming in C 2

Structure declarations Combined variable and type declaration struct tag {member-list} variable-list; Any one of the three portions can be omitted struct {int a, b; char *p;} x, y; /* omit tag */ variables x, y declared with members as described: int members a, b and char pointer p. x and y have same type, but differ from all others even if there is another declaration: struct {int a, b; char *p;} z; /* z has different type from x, y */ Computer System and programming in C 3

Structure declarations struct S {int a, b; char *p;}; /* omit variables */ No variables are declared, but there is now a type struct S that can be referred to later struct S z; /* omit members */ Given an earlier declaration of struct S, this declares a variable of that type typedef struct {int a, b; char *p;} S; /* omit both tag and variables */ This creates a simple type name S (more convenient than struct S) Computer System and programming in C 4

Recursively defined structures Obviously, you can t have a structure that contains an instance of itself as a member such a data item would be infinitely large But within a structure you can refer to structures of the same type, via pointers struct TREENODE { } char *label; struct TREENODE *leftchild, *rightchild; Computer System and programming in C 5

Recursively defined structures When two structures refer to each other, one must be declared in incomplete (prototype) fashion struct HUMAN; struct PET { char name[name_limit]; char species[name_limit]; struct HUMAN *owner; } fido = { Fido, Canis lupus familiaris }; struct HUMAN { char name[name_limit]; struct PET pets[pet_limit]; } sam = { Sam, {fido}}; We can t initialize the owner member at this point, since it hasn t been declared yet Computer System and programming in C 6

Member access Direct access operator s.m subscript and dot operators have same precedence and associate left-to-right, so we don t need parentheses for sam.pets[0].species Indirect access s->m: equivalent to (*s).m Dereference a pointer to a structure, then return a member of that structure Dot operator has higher precedence than indirection operator, so parentheses are needed in (*s).m. evaluated first: access owner member (*fido.owner).name or. and fido.owner->name have equal precedence * evaluated next: dereference pointer to HUMAN and associate left-to-right Computer System and programming in C 7

Memory layout struct COST { int amount; char currency_type[2]; } struct PART { char id[2]; struct COST cost; int num_avail; } currency_type layout of struct PART: id amount num_avail cost Here, the system uses 4-byte alignment of integers, so amount and num_avail must be aligned Four bytes wasted for each structure! Computer System and programming in C 8

Memory layout A better alternative (from a space perspective): struct COST { int amount; char currency_type; } struct PART { struct COST cost; char id[2]; int num_avail; currency_type } amount id num_avail cost Computer System and programming in C 9

Bit field members must be ints Bit fields If space is a serious concern, you can select the number of bits used for each member struct CHAR { unsigned ch: 7; unsigned font: 6; unsigned size: 19; }; Layout possibilities (machine-dependent): Note: This won t work on machines with 16-bit ints ch font size size font ch Computer System and programming in C 10

Bit fields Portability is an issue: Do any bit field sizes exceed the machine s int size? Is there any pointer manipulation in your code that assumes a particular layout? Bit fields are syntactic sugar for more complex shifting/masking e.g. to get font value, mask off the ch and size bits, then shift right by 19 This is what actually happens in the object code bit fields just make it look simpler at the source level Computer System and programming in C 11

Structures as function arguments Structures are scalars, so they can be returned and passed as arguments just like ints, chars struct BIG changestruct(struct BIG s); Call by value: temporary copy of structure is created Caution: passing large structures is inefficient involves a lot of copying avoid by passing a pointer to the structure instead: void changestruct(struct BIG *s); What if the struct argument is read-only? Safe approach: use const void changestruct(struct BIG const *s); Computer System and programming in C 12

Unions Like structures, but every member occupies the same region of memory! Structures: members are and ed together: name and species and owner Unions: members are xor ed together union VALUE { float f; int i; char *s; }; /* either a float xor an int xor a string */ Computer System and programming in C 13

Unions Up to programmer to determine how to interpret a union (i.e. which member to access) Often used in conjunction with a type variable that indicates how to interpret the union value enum TYPE { INT, FLOAT, STRING }; Access type to determine struct VARIABLE { how to interpret value enum TYPE type; }; union VALUE value; Computer System and programming in C 14

Unions Storage size of union is the size of its largest member avoid unions with widely varying member sizes; for the larger data types, consider using pointers instead Initialization Union may only be initialized to a value appropriate for the type of its first member Computer System and programming in C 15