Fundamental of Programming (C)
|
|
- Sophie Wright
- 5 years ago
- Views:
Transcription
1 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 1/38
2 Outline Structures Be able to use compound data structures in programs Unions Be able to share storage space of their members Bit fields Structures Be able to do simple bit-vector manipulations Enumerations Be able to use compound symbolic constants Department of Computer Engineering 2
3 User Defined Data Types (typedef) The C language provides a facility called typedef for creating synonyms for previously defined data type names. For example, the declaration: typedef int Length; makes the name Length a synonym (or alias) for the data type int. The data type name Length can now be used in declarations in exactly the same way that the data type int can be used: Length a, b, len ; Length numbers[10] ; typedef char String[50]; typedef int Array[10]; String name; Array ages; Department of Computer Engineering 3
4 Structures (struct) Structures sometimes referred to as aggregates are collections of related variables 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 Pointers and structures facilitate the formation of more complex data structures such as linked lists, queues, stacks and trees Structures are derived data types they are constructed using objects of other types Department of Computer Engineering 4
5 Declaring Structures (struct) The name "employee" is called a structure tag Variables declared within the braces of the structure definition are the structure s members struct employee }; char firstname[ 20 ]; char lastname[ 20 ]; int age; char gender; double hourlysalary; struct employee Ali, emp[10]; struct employee char firstname[ 20 ]; char lastname[ 20 ]; int age; char gender; double hourlysalary; } Ali, Sara, empdts[20]; struct employee Reza, *emp; struct char firstname[ 20 ]; char lastname[ 20 ]; int age; char gender; double hourlysalary; } Ali; Department of Computer Engineering 5
6 Declaring Structures (struct) Often, typedef is used in combination with struct to declare a synonym (or an alias) for a structure: typedef struct char firstname[ 20 ]; char lastname[ 20 ]; int age; char gender; double hourlysalary; } employee; /* The "alias" employee Ali; /* Create a struct variable */ struct employee char firstname[ 20 ]; char lastname[ 20 ]; int age; char gender; double hourlysalary; } Ali, Sara, empdts[20]; struct employee Reza, *emp; Department of Computer Engineering 6
7 Declaring Structures (struct) Members of the same structure type must have unique names, but two different structure types may contain members of the same name without conflict struct employee char Name[ 20 ]; char Name[ 20 ]; // Error!!! int age; char gender; double hourlysalary; } Ali, Sara, empdts[20]; struct employee Reza, *emp; struct Student char Name[ 20 ]; // OK int age; char gender; }; struct Student Ce40153[80]; Each structure definition must end with a semicolon Department of Computer Engineering 7
8 Declaring Structures (struct) A structure cannot contain an instance of itself For example, a variable of type struct employee cannot be declared in the definition for struct employee A pointer to struct employee, however, may be included struct employee2 // double hourlysalary; struct employee2 person; /* ERROR */ struct employee2 *eptr; /* pointer */ }; A structure containing a member that is a pointer to the same structure type is referred to as a self-referential structure Department of Computer Engineering 8
9 Declaring Structures (struct) The structure tag name is optional struct char firstname[ 20 ]; char lastname[ 20 ]; int age; char gender; double hourlysalary; } Ali; If a structure definition does not contain a structure tag name, variables of the structure type may be declared only in the structure definition not in a separate declaration Department of Computer Engineering 9
10 Structure s sizeof Structure definitions do not reserve any space in memory; rather, each definition creates a new data type that is used to define variables sizeof(struct ) = sum of sizeof(members) + alignment padding (Processor- and compiler-specific) struct employee char firstname[ 20 ]; char lastname[ 20 ]; int age; char gender; double hourlysalary; }; struct employee Ali, emp[10]; printf("%d", sizeof(ali)); printf("%d", sizeof(emp)); printf("%d", sizeof(struct employee)); Department of Computer Engineering 10
11 Memory layout struct COST int amount; char currency_type[2]; } struct PART char id[2]; struct COST cost; int num_avail; } currency_type 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! Department of Computer Engineering 11
12 Memory layout struct COST int amount; char currency_type[2]; } struct PART struct COST cost; char id[2]; int num_avail; } currency_type amount cost id num_avail Implementation dependent!!! Department of Computer Engineering 12
13 Accessing Struct Members Individual members of a struct variable may be accessed using the structure member operator (the dot, "."): myemp.firstname ; employee. firstname; // Error Or, if a pointer to the struct has been declared and initialized employee *emp = &myemp ; by using the structure pointer operator : emp -> firstname; // arrow operator which could also be written as: (* emp).firstname; struct employee char firstname[ 20 ]; // } myemp; Department of Computer Engineering 13
14 An Example - Initialization //Create a struct but don t reserve space struct personal long id; // student ID float gpa; // grade point average }; struct identity char FirstName[30]; char LastName[30]; unsigned age; struct personal person; }; struct identity js = "Joe", "Smith", 25}, *ptr = &js ; js.person.id = ; js.person.gpa = 3.4 ; js.personal.id strcpy(js.firstname, = "Joe", "Smith","Joe"); 25, Error 9, 10} printf ("%s %s %d %ld %f\n", js.firstname, js.lastname, js.age, js.person.id, js.person.gpa) ; printf ("%s %s %d %ld %f\n", ptr->firstname, ptr->lastname,ptr->age, ptr->person.id, ptr->person.gpa) ; Department of Computer Engineering 14
15 An Example - Assignment //Create a struct but don t reserve space struct personal long id; // student ID float gpa; // grade point average }; struct identity char FirstName[30]; char LastName[30]; unsigned age; struct personal person; }; struct identity js = "Joe", "Smith", 25}, oj ; js.person.id = ; js.person.gpa = 3.4 ; oj = js; printf ("%s %s %d %ld %f\n", oj.firstname, oj.lastname, oj.age, js.person.id, oj.person.gpa) ; printf ("%s %s %d %ld %f\n", ptr->firstname, ptr->lastname,ptr->age, ptr->person.id, ptr->person.gpa) ; Department of Computer Engineering 15
16 Arrays of Structures //Create struct identity a struct sharifc40153[80] but don t reserve = "omid", space "Jafarinezhad", struct identity14, , 20, struct personal "Samad", "Shekarestani", 90, , 20} ; strcpy(sharifc40153[2].firstname, "Khaje Nezam"); char FirstName[30]; strcpy(sharifc40153[2].lastname, long id; // student ID "Shekarestani"); char LastName[30]; sharifc40153[2]. float gpa; age // = grade 100; point average unsigned age; }; sharifc40153[2]. person.id = ; struct personal person; sharifc40153[2]. person. gpa = 20; } students[4]; FirstName LastName age id person gpa students[0] omid Jafarinezhad Students[1] Samad Shekarestani students[2] Khaje Nezam Shekarestani students[3] Department of Computer Engineering 16
17 An Example #define NFRIENDS 10 struct Date unsigned year; unsigned month; unsigned day; }; struct Friend char FirstName[30]; char LastName[30]; struct Date Birthday; }; Department of Computer Engineering 17 bool check_birthday(struct Date today, struct Date myfriend) if ((today.month == myfriend.month) && (today.day == myfriend.day)) return (true); return (false); } int typedef main() struct struct unsigned Friend year; friends[nfriends]; struct unsigned Date month; today = 2012, 3, 11}; // unsigned... day; } Date; for (i = 0; i < NFRIENDS; i++) bool check_birthday(date today, Date myfriend) if(check_birthday(today, friends[i].birthday)) // printf ("%s %s\n", friends[i].firstname, oj.lastname) ; } } //
18 Pointers to Structures Date create_date1(int month, void create_date2(date *d, int day, int month, int year) Date d; d.month = month; d.day = day; d.year = year; Pass-by-reference } int day, int year) d->month = month; d->day = day; d->year = year; } return (d); Date today; Copies date today = create_date1(9, 4, 2008); create_date2(&today, 9, 4, 2008); Department of Computer Engineering 18
19 Pointers to Structures void create_date2(date *d, int month, int day, int year) d->month = month; d->day = day; d->year = year; } 0x30A8 0x30A4 0x30A0 0x3098 year: 2008 day: 4 month: 9 d: 0x1000 void foo(void) Date today; create_date2(&today, 9, 4, 2008); 0x1008 0x1004 today.year: today.day: } 0x1000 today.month: Department of Computer Engineering 19
20 Pointers to Structures Date * create_date3(int month, int day, int year) Date *d; What is d pointing to?!?! d->month = month; d->day = day; d->year = year; } return (d); Department of Computer Engineering 20
21 Pointers to Structures void changebyvalue(date date) date.day ++; } void changebyref(date *date) date->day++; } void printdate(const Date date) printf("today(d/m/y) is : \n"); printf("%d/%d/%d\n", date.day, date.month, date.year); } Date today = 2012, 3, 11}; printdate(today); changebyvalue(today); printdate(today); changebyref(&today); printdate(today); today(d/m/y) is : 11/3/2012 today(d/m/y) is : 11/3/2012 today(d/m/y) is : 12/3/2012 Department of Computer Engineering 21
22 Compression of Structures Structures may not be compared using operators == and!=, because structure members are not necessarily stored in consecutive bytes of memory struct a int a; // OK int b; }; struct a b, c; b.a = 10; b.b = 30; c = b; if(c == b) // Error Department of Computer Engineering 22
23 Enumeration Enumeration is a user-defined data type. It is defined using the keyword enum and the syntax is: enum tag_name name_0,, name_n} ; The tag_name is not used directly. The names in the braces are symbolic constants that take on integer values from zero through n. As an example, the statement: enum colors red, yellow, green } ; creates three constants. red is assigned the value 0, yellow is assigned 1 and green is assigned 2 Department of Computer Engineering 23
24 Enumeration Values in an enum start with 0, unless specified otherwise, and are incremented by 1 The identifiers in an enumeration must be unique The value of each enumeration constant of an enumeration can be set explicitly in the definition by assigning a value to the identifier Multiple members of an enumeration can have the same constant value Assigning a value to an enumeration constant after it has been defined is a syntax error Use only uppercase letters enumeration constant names. This makes these constants stand out in a program and reminds you that enumeration constants are not variables Department of Computer Engineering 24
25 An Example /* This program uses enumerated data types to access the elements of an array */ #include <stdio.h> int main( ) int March[5][7]=0,0,1,2,3,4,5}, 6,7,8,9,10,11,12}, 13,14,15,16,17,18,19}, 20,21,22,23,24,25,26}, 27,28,29,30,31,0,0}}; enum days Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}; enum week week_one, week_two, week_three, week_four, week_five}; } printf ("Monday the third week of March is March %d\n", March [week_three] [Monday] ); Department of Computer Engineering 25
26 An Example /* enumeration constants represent months of the year */ enum months JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }; enum months month; /* initialize array of pointers */ const char *monthname[] = "", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", /* loop through months */ for (month = JAN; month <= DEC; month++ ) printf( "%2d%11s\n", month, monthname[month] ); } Department of Computer Engineering 26
27 Unions 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 The members of a union can be of any data type The number of bytes used to store a union must be at least enough to hold the largest member Only one member, and thus one data type, can be referenced at a time Department of Computer Engineering 27
28 Unions representation union mydataunion int i; char c; float f; } u1, u2; union mydataunion u3; f i c u1.i = 4; u1.c = a ; u2.i = 0xDEADBEEF; Department of Computer Engineering 28
29 Unions The operations that can be performed on a union are the following: assigning a union to another union of the same type taking the address (&) of a union variable accessing union members using the structure member operator and the structure pointer operator Unions may not be compared using operators == and!= for the same reasons that structures cannot be compared Department of Computer Engineering 29
30 Unions In a declaration, a union may be initialized with a value of the same type as the first union member union a int a; // OK char b[4]; }; union a b = 10}; printf("%d", b.a); Department of Computer Engineering 30
31 Unions A union value doesn t "know" which case it contains union AnElt int i; char c; } elt1, elt2; How should your program keep track whether elt1, elt2 hold an int or a char? elt1.i = 4; elt2.c = a ; elt2.i = 0xDEADBEEF; Basic answer: Another variable holds that info if (elt1 currently has a char) Department of Computer Engineering 31
32 Tagged Unions Tag every value with its case enum Union_Tag IS_INT, IS_CHAR}; struct TaggedUnion enum Union_Tag tag; union int i; char c; } data; }; Enum must be external to struct, so constants are globally visible Struct field must be named Department of Computer Engineering 32
33 Bit-field Structures C enables you to specify the number of bits in which an unsigned or int member of a structure or union is stored This is referred to as a bit field Bit fields enable better memory utilization by storing data in the minimum number of bits required Bit field members must be declared as int or unsigned A bit field is declared by following an unsigned or int member name with a colon (:) and an integer constant representing the width of the field (i.e., the number of bits in which the member is stored) Department of Computer Engineering 33
34 Bit-field Structures Notice that bit field members of structures are accessed exactly as any other structure member struct Flags int } foo; unsigned int unsigned int foo.f1 = -2; foo.f2 = 1; foo.f3 = 2; f1:3; f2:1; f3:2; f1 f2 f bit Padded to be an integral number of words Placement is compiler-specific 8 bit 8 bit Department of Computer Engineering 34
35 Unnamed Bit-field struct example unsigned a : 13; unsigned : 19; unsigned b : 4; }; uses an unnamed 19-bit field as padding nothing can be stored in those 19 bits An unnamed bit field with a zero width is used to align the next bit field on a new storage-unit boundary For example, the structure definition struct example unsigned a : 13; unsigned : 0; unsigned b : 4; }; uses an unnamed 0-bit field to skip the remaining bits (as many as there are) of the storage unit in which a is stored and to align b on the next storage-unit boundary Department of Computer Engineering 35
36 An Example - disk drive controller Frequently device controllers (e.g. disk drives) and the operating system need to communicate at a low level. Device controllers contain several registers which may be packed together in one integer Department of Computer Engineering 36
37 An Example - disk drive controller struct DISK_REGISTER unsigned ready:1; unsigned error_occured:1; unsigned disk_spinning:1; unsigned write_protect:1; unsigned head_loaded:1; unsigned error_code:8; unsigned track:9; unsigned sector:5; unsigned command:5; }; struct DISK_REGISTER *disk_reg = (struct DISK_REGISTER *) DISK_REGISTER_MEMORY; /* Define sector and track to start read */ disk_reg->sector = new_sector; disk_reg->track = new_track; disk_reg->command = READ; /* wait until operation done, ready will be true */ while (! disk_reg->ready ) ; /* check for errors */ if (disk_reg->error_occured) /* interrogate disk_reg->error_code for error type */ switch (disk_reg->error_code)... } Department of Computer Engineering 37
38 Notes of caution Bit-field manipulations are machine dependent Attempting to access individual bits of a bit field as if they were elements of an array is a syntax error. Bit fields are not "arrays of bits" Attempting to take the address of a bit field (the & operator may not be used with bit fields because they do not have addresses) Although bit fields save space, using them can cause the compiler to generate slower-executing machine-language code. This occurs because it takes extra machine language operations to access only portions of an addressable storage unit. This is one of many examples of the kinds of space time trade-offs that occur in computer science Department of Computer Engineering 38
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 informationStructures 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 informationProgramming 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 informationFundamentals 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 informationOBJECT 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 informationLow-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 informationIntroduction. 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 informationC Structures, Unions, Bit Manipulations, and Enumerations
C Structures, Unions, Bit Manipulations, and Enumerations Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline 10.2 Structure Definitions 10.4
More informationChapter 10 C Structures, Unions, Bit Manipulations
Chapter 10 C Structures, Unions, Bit Manipulations Skipped! Skipped! and Enumerations Skipped! Page 416 In programming languages, Arrays (Chapter 6) allows programmers to group elements of the same type
More informationDarshan 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 informationType 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공학프로그래밍언어 (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 informationFundamental of Programming (C)
Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering
More informationFundamentals 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 informationEverything Else C Programming and Software Tools. N.C. State Department of Computer Science
Everything Else C Programming and Software Tools N.C. State Department of Computer Science BOOLEANS CSC230: C and Software Tools NC State University Computer Science Faculty 2 Booleans In C99, bools are
More informationby: 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 informationPointers 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 informationData Structures and Algorithms (DSA) Course 4. Iulian Năstac
Data Structures and Algorithms (DSA) Course 4 Iulian Năstac 10. Functions for dynamic memory allocation (recapitulation) Dynamic allocation is a specific characteristic allowed by some computing languages,
More informationALQUDS 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 informationSyntax 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 informationDept. 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 informationCS349/SE382 A1 C Programming Tutorial
CS349/SE382 A1 C Programming Tutorial Erin Lester January 2005 Outline Comments Variable Declarations Objects Dynamic Memory Boolean Type structs, enums and unions Other Differences The Event Loop Comments
More informationFundamentals of Programming
Fundamentals of Programming Lecture 3 - Constants, Variables, Data Types, And Operations Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Outline C Program Data types Variables
More informationType Checking. Prof. James L. Frankel Harvard University
Type Checking Prof. James L. Frankel Harvard University Version of 7:10 PM 27-Feb-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. C Types C Types Type Category Type Category Type
More informationAIMMS Function Reference - Date Time Related Identifiers
AIMMS Function Reference - Date Time Related Identifiers This file contains only one chapter of the book. For a free download of the complete book in pdf format, please visit www.aimms.com Aimms 3.13 Date-Time
More informationBLM2031 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 informationCS Programming I: Arrays
CS 200 - Programming I: Arrays Marc Renault Department of Computer Sciences University of Wisconsin Madison Fall 2017 TopHat Sec 3 (PM) Join Code: 719946 TopHat Sec 4 (AM) Join Code: 891624 Array Basics
More informationProgramming. 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 informationRecap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam
Multimedia Programming 2004 Lecture 2 Erwin M. Bakker Joachim Rijsdam Recap Learning C++ by example No groups: everybody should experience developing and programming in C++! Assignments will determine
More informationHistory. used in early Mac development notable systems in Pascal Skype TeX embedded systems
Overview The Pascal Programming Language (with material from tutorialspoint.com) Background & History Features Hello, world! General Syntax Variables/Data Types Operators Conditional Statements Functions
More informationStructures 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 information14. Other Data Types. Compound Data Types: Defined data types (typedef) Unions typedef existing_type new_type_name ;
- 95 - Compound Data Types: 14 Other Data Types Defined data types (typedef) C++ allows the definition of our own types based on other existing data types We can do this using the keyword typedef, whose
More informationCS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation
CS113: Lecture 3 Topics: Variables Data types Arithmetic and Bitwise Operators Order of Evaluation 1 Variables Names of variables: Composed of letters, digits, and the underscore ( ) character. (NO spaces;
More informationHigh 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 informationBBM#101# #Introduc/on#to# Programming#I# Fall$2013,$Lecture$12$
BBM#101# #Introduc/on#to# Programming#I# Fall$2013,$Lecture$12$ Instructors:!Aykut!Erdem,!Erkut!Erdem,!Fuat!Akal! Today#! Structures#! Structure#Defini/ons#! Ini/alizing#Structures#! Accessing#Members#of#Structures#!
More informationComputer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary
GATE- 2016-17 Postal Correspondence 1 C-Programming Computer Science & Information Technology (CS) 20 Rank under AIR 100 Postal Correspondence Examination Oriented Theory, Practice Set Key concepts, Analysis
More informationINFORMATION TECHNOLOGY SPREADSHEETS. Part 1
INFORMATION TECHNOLOGY SPREADSHEETS Part 1 Page: 1 Created by John Martin Exercise Built-In Lists 1. Start Excel Spreadsheet 2. In cell B1 enter Mon 3. In cell C1 enter Tue 4. Select cell C1 5. At the
More informationCMSC 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 informationStructures and Pointers
Structures and Pointers Comp-206 : Introduction to Software Systems Lecture 11 Alexandre Denault Computer Science McGill University Fall 2006 Note on Assignment 1 Please note that handin does not allow
More informationArrays and Pointers (part 2) Be extra careful with pointers!
Arrays and Pointers (part 2) EECS 2031 22 October 2017 1 Be extra careful with pointers! Common errors: l Overruns and underruns Occurs when you reference a memory beyond what you allocated. l Uninitialized
More informationIV 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 informationComputer 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 informationA 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 informationLaboratory 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 informationA class is a user-defined type. It is composed of built-in types, other user-defined types and
Chapter 3 User-defined types 3.1 Classes A class is a user-defined type. It is composed of built-in types, other user-defined types and functions. The parts used to define the class are called members.
More informationVariables 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 informationIntroduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers
Introduction to C Geared toward programmers Sean Ogden Slide heritage: Alin Dobra Niranjan Nagarajan Owen Arden Robert Escriva Zhiyuan Teo Ayush Dubey Cornell CS 4411, August 30, 2013 Administrative Information
More informationFundamental of Programming (C)
Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi CE 43 - Fall 97 Lecture 4 Input and Output Department of Computer Engineering Outline printf
More informationArrays and Pointers (part 2) Be extra careful with pointers!
Arrays and Pointers (part 2) CSE 2031 Fall 2011 23 October 2011 1 Be extra careful with pointers! Common errors: Overruns and underruns Occurs when you reference a memory beyond what you allocated. Uninitialized
More informationC BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.
C BOOTCAMP DAY 2 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh Pointers 2 Pointers Pointers are an address in memory Includes variable addresses,
More informationStructures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures
Structures Proseminar C Grundlagen und Konzepte Michael Kuhn Research Group Scientific Computing Department of Informatics Faculty of Mathematics, Informatics und Natural Sciences University of Hamburg
More informationIntroduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers
Introduction to C Geared toward programmers Ayush Dubey Slide heritage: Alin Dobra Niranjan Nagarajan Owen Arden Robert Escriva Zhiyuan Teo Cornell CS 4411, August 31, 2012 Administrative Information Outline
More informationEEE145 Computer Programming
EEE145 Computer Programming Content of Topic 2 Extracted from cpp.gantep.edu.tr Topic 2 Dr. Ahmet BİNGÜL Department of Engineering Physics University of Gaziantep Modifications by Dr. Andrew BEDDALL Department
More informationCS3157: Advanced Programming. Announcement
CS3157: Advanced Programming Lecture #10 Mar 20 Shlomo Hershkop shlomo@cs.columbia.edu Announcement Welcome back from spring break Hope you ve caught up with your courses Have the exams back, will return
More informationC: 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 informationData Types and Variables in C language
Data Types and Variables in C language Basic structure of C programming To write a C program, we first create functions and then put them together. A C program may contain one or more sections. They are
More informationLecture 14. Dynamic Memory Allocation
Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 14-1 Lecture 14. Dynamic Memory Allocation The number of variables and their sizes are determined at compile-time before a program runs /*
More informationIntroduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers
Introduction to C Geared toward programmers Robert Escriva Slide heritage: Alin Dobra Niranjan Nagarajan Owen Arden Cornell CS 4411, August 30, 2010 1 Why C? 2 A Quick Example 3 Programmer s Responsibilities
More informationArrays III and Enumerated Types
Lecture 15 Arrays III and Enumerated Types Multidimensional Arrays & enums CptS 121 Summer 2016 Armen Abnousi Multidimensional Arrays So far we have worked with arrays with one dimension. Single dimensional
More informationTokens, Expressions and Control Structures
3 Tokens, Expressions and Control Structures Tokens Keywords Identifiers Data types User-defined types Derived types Symbolic constants Declaration of variables Initialization Reference variables Type
More informationProcedures, 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 informationInput 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 informationBinghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program
Syntax What the Compiler needs to understand your program 1 Pre-Processing Any line that starts with # is a pre-processor directive Pre-processor consumes that entire line Possibly replacing it with other
More informationDesign and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso
Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso Why C? Test on 21 Android Devices with 32-bits and 64-bits processors and different versions
More informationExample. Section: PS 709 Examples of Calculations of Reduced Hours of Work Last Revised: February 2017 Last Reviewed: February 2017 Next Review:
Following are three examples of calculations for MCP employees (undefined hours of work) and three examples for MCP office employees. Examples use the data from the table below. For your calculations use
More information3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type
Pointer Basics What is a pointer? pointer = memory address + type C Language III CMSC 313 Sections 01, 02 A pointer can contain the memory address of any variable type A primitive (int, char, float) An
More informationadvanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef
today advanced data types (1) typedef. mon 23 sep 2002 homework #1 due today homework #2 out today quiz #1 next class 30-45 minutes long one page of notes topics: C advanced data types dynamic memory allocation
More information10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013
Midterm Topic Review Pointer Basics C Language III CMSC 313 Sections 01, 02 1 What is a pointer? Why Pointers? Pointer Caution pointer = memory address + type A pointer can contain the memory address of
More informationRecursion Enums. Basics of Programming 1. Department of Networked Systems and Services G. Horváth, A.B. Nagy, Z. Zsóka, P. Fiala, A.
Recursion The enumerated type Recursion Enums Basics of Programming 1 Department of Networked Systems and Services G. Horváth, A.B. Nagy, Z. Zsóka, P. Fiala, A. Vitéz 31 October, 2018 based on slides by
More informationProgramming 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 informationPROGRAMMAZIONE I A.A. 2017/2018
PROGRAMMAZIONE I A.A. 2017/2018 STRUCTURES STRUCTS Arrays allow to define type of variables that can hold several data items of the same kind. Similarly structure is another user defined data type available
More informationHigh Performance Computing in C and C++
High Performance Computing in C and C++ Rita Borgo Computer Science Department, Swansea University Summary Introduction to C Writing a simple C program Compiling a simple C program Running a simple C program
More informationStructures 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 informationCSE 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 informationC OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed
C Overview C OVERVIEW Goals speed portability allow access to features of the architecture speed C fast executables allows high-level structure without losing access to machine features many popular languages
More informationC OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure
C Overview Basic C Program Structure C OVERVIEW BASIC C PROGRAM STRUCTURE Goals The function main( )is found in every C program and is where every C program begins speed execution portability C uses braces
More information12 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 informationComputer 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 informationUNIT-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 informationData 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 informationCS240: Programming in C
CS240: Programming in C Lecture 10: Review - Structures and Memory Allocation Unions Recap: Structures Holds multiple items as a unit Treated as scalar in C: can be returned from functions, passed to functions
More informationDepartment of Computer Science and Engineering. Programming for Problem Solving. I Year B.Tech. (I - Sem) Assistant Professor (M.
Website : www.aceec.ac.in ACE Engineering College Ankushapur, Ghatkesar, Telangana 501301 (EAMCET Code: ACEG) NAAC Accridated with A Grade Department of Computer Science and Engineering Programming for
More informationCMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012
CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012 TOPICS TODAY Project 5 Pointer Basics Pointers and Arrays Pointers and Strings POINTER BASICS Java Reference In Java,
More informationData 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 informationMain Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University
C Programming Notes Dr. Karne Towson University Reference for C http://www.cplusplus.com/reference/ Main Program #include main() printf( Hello ); Comments: /* comment */ //comment 1 Data Types
More informationFor each of the following variables named x, specify whether they are static, stack-dynamic, or heapdynamic:
For each of the following variables named x, specify whether they are static, stack-dynamic, or heapdynamic: a) in C++: int* x = new(int); b) in Java: class book { protected string title; book(string x)
More informationSchedule/BACnet Schedule
Object Dictionary 1 Schedule/BACnet Schedule Introduction Note: The Johnson Controls Schedule object is considered a BACnet Schedule object because it supports BACnet functionality. In addition, this object
More informationCprE 288 Introduction to Embedded Systems Exam 1 Review. 1
CprE 288 Introduction to Embedded Systems Exam 1 Review http://class.ece.iastate.edu/cpre288 1 Overview of Today s Lecture Announcements Exam 1 Review http://class.ece.iastate.edu/cpre288 2 Announcements
More informationC 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 informationData Types. 9. Types. a collection of values and the definition of one or more operations that can be performed on those values
Data Types 1 data type: a collection of values and the definition of one or more operations that can be performed on those values C++ includes a variety of built-in or base data types: short, int, long,
More informationComplex data structures. Cedric Saule
Complex data structures Cedric Saule cedric.saule@uni-bielefeld.de Arrays in language C In language C, arrays are contiguous spaces in memory. We make the difference between two kinds of arrays : Static
More informationData Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19
Data Storage Geoffrey Brown Bryce Himebaugh Indiana University August 9, 2016 Geoffrey Brown, Bryce Himebaugh 2015 August 9, 2016 1 / 19 Outline Bits, Bytes, Words Word Size Byte Addressable Memory Byte
More informationFundamental of Programming (C)
Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 9 Pointer Department of Computer Engineering 1/46 Outline Defining and using Pointers
More informationKurt Schmidt. October 30, 2018
to Structs Dept. of Computer Science, Drexel University October 30, 2018 Array Objectives to Structs Intended audience: Student who has working knowledge of Python To gain some experience with a statically-typed
More informationC 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 informationProgramming 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 informationProgramming 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 informationConditional Formatting
Microsoft Excel 2013: Part 5 Conditional Formatting, Viewing, Sorting, Filtering Data, Tables and Creating Custom Lists Conditional Formatting This command can give you a visual analysis of your raw data
More informationProgramming Logic and Design Sixth Edition
Objectives Programming Logic and Design Sixth Edition Chapter 6 Arrays In this chapter, you will learn about: Arrays and how they occupy computer memory Manipulating an array to replace nested decisions
More informationThis is part of the book COMPUTER PROGRAMMING THE C LANGUAGE. by Adriana ALBU. Conspress Publisher, Bucureşti, România, 2013 ISBN:
This is part of the book COMPUTER PROGRAMMING THE C LANGUAGE by Adriana ALBU Conspress Publisher, Bucureşti, România, 2013 ISBN: 978-973-100-270-5 Chapter 11 User-defined types Besides the standard data
More information