Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

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

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

IV Unit Second Part STRUCTURES

Computer programming UNIT IV UNIT IV. GPCET,Dept of CSE, P Kiran Rao

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

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

CS349/SE382 A1 C Programming Tutorial

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

UNIT - V STRUCTURES AND UNIONS

Data Types and Variables in C language

struct structure_name { //Statements };

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

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

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

Chapter 10 C Structures, Unions, Bit Manipulations

Programming for Engineers Structures, Unions

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

Unit IV & V Previous Papers 1 mark Answers

Structures and Pointers

12 CREATING NEW TYPES

Structures Unions and Enumerated Datatypes 224

Fundamental of C programming. - Ompal Singh

Subject: Fundamental of Computer Programming 2068

ALQUDS University Department of Computer Engineering

Fundamental of Programming (C)

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

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

This is part of the book COMPUTER PROGRAMMING THE C LANGUAGE. by Adriana ALBU. Conspress Publisher, Bucureşti, România, 2013 ISBN:

Dynamic Memory Allocation

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

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

Object Oriented Pragramming (22316)

Input And Output of C++

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

22c:111 Programming Language Concepts. Fall Types I

3.3 Structures. Department of CSE

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

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

Multi-Dimensional arrays

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

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

Chapter-14 STRUCTURES

Questions Bank. 14) State any four advantages of using flow-chart

Chapter 7: Structuring the Data. Lecture7 1

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Bit Manipulation in C

Lecture 3. The syntax for accessing a struct member is

BLM2031 Structured Programming. Zeyneb KURT

Q 1. Attempt any TEN of the following:

Fundamentals of Programming Session 24

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

Chapter 21: Introduction to C Programming Language

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

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

User Defined data Types

C: How to Program. Week /Mar/05

PROGRAMMAZIONE I A.A. 2017/2018

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

POINTERS, STRUCTURES AND INTRODUCTION TO DATA STRUCTURES

F.Y. Diploma : Sem. II [CO/CD/CM/CW/IF] Programming in C

Principles of C and Memory Management

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Computer Systems Principles. C Pointers

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

More about BOOLEAN issues

Basics of Programming

Tokens, Expressions and Control Structures

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) MODEL ANSWER

DEPARTMENT OF MATHS, MJ COLLEGE

DECISION CONTROL AND LOOPING STATEMENTS

NCS 301 DATA STRUCTURE USING C

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

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

CSCI312 Principles of Programming Languages!

Darshan Institute of Engineering & Technology for Diploma studies Unit 4

Structures in C. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

A Fast Review of C Essentials Part I

Topic 5: Enumerated Types and Switch Statements

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

CSE 230 Intermediate Programming in C and C++

Module 5. Arrays. Adapted from Absolute Java, Rose Williams, Binghamton University

Datatypes, Variables, and Operations

Chapter 14. Structures

Compiling and Running a C Program in Unix

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

6.096 Introduction to C++ January (IAP) 2009

Chapter 11: Structured Data

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

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

Chapter-8 DATA TYPES. Introduction. Variable:

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

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

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

More Pointers Week 11

'C' Programming Language

Computers Programming Course 5. Iulian Năstac

// CSE/CE/IT 124 JULY 2015, 2.a. algorithm to inverse the digits of a given integer. Step 1: Start Step 2: Read: NUMBER Step 3: r=0,rev=0

Lecture 12. Data Types and Strings

Chapter 11: Structured Data

Guide for The C Programming Language Chapter 4

Transcription:

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 a user defined data type. Structure helps to organize complex data in a more meaningful way. Syntax of Structure: struct structure_name data_type member1; data_type member2; struct is a keyword. structure_name is a tag name of a structure. member1, member2 are members of structure. #include<conio.h> struct book char title[100]; char author[50]; int pages; float price; void main() struct book book1; printf("enter title, author name, pages and price of book"); scanf( %s,book1.title); scanf( %s, book1.author); scanf("%d",&book1.pages); scanf("%f",&book1.price); printf("\n detail of the book"); printf( %s,book1.title); printf( %s,book1.author); printf("%d",book1.pages); printf("%f",book1.price); getch(); book is structure whose members are title, author, pages and price. book1 is a structure variable. 1

2 How do we declare and access structure variables? Declaration of structure: A structure variable declaration is similar to the declaration of variables of any other data type. It includes the following elements: 1) The keyword struct. 2) The structure tag name. 3) List of variable names separated by commas. 4) A terminating semicolon. struct book char title[100]; char author[50]; int pages; float price; book1; struct book book2; We can declare structure variable in two ways: 1) Just after the structure body like book1. 2) With struct keyword and structure tag name like book2. Accessing structure members: The following syntax is used to access the member of structure. structure_variable.member_name structure_variable is a variable of structure and member_name is the name of variable which is a member of a structure. The. (dot) operator or period operator connects the member name to structure name. book1.price represents price of book1. We can assign values to the member of the structure variable book1 as below, strcpy(book1.title, ANSI C ); strcpy(book1.author, Balagurusamy ); book1.pages=250; book1.price=120.50; We can also use scanf function to assign value through a keyboard. scanf( %s,book1.title); scanf( %d,&book1.pages); 3 What is Union? Union is user defined data type just like structure. Each member in structure is assigned its own unique storage area where as in Union; all the members share common storage area. All members share the common area so only one member can be active at a time. Unions are used when all the members are not assigned value at the same time. 2

union book char title[100]; char author[50]; int pages; float price; 4 Difference between Structure and Union Structure Each member is assigned its own unique storage area. Total memory required by all members is allocated. All members are active at a time. All members can be initialized. Requires more memory. struct SS int a; float b; char c; 1 byte for c 2 bytes for a 4 bytes for b Union All members share the same storage area. Maximum memory required by the member is allocated. Only one member is active a time. Only the first member can be initialized. Requires less memory. union UU int a; float b; char c; 4 bytes for c,b,a b a c 3 Total bytes = 1 + 2 + 4 = 7 bytes. 5 Explain nested structure with example. 4 bytes are there between a,b and c because largest memory occupies by float which is 4 bytes. A structure that contains another structure as a member variable is known as nested structure or structure within a structure. Structure which is part of other structure must be declared before the structure in which it is used. #include<conio.h> struct address char add1[50];

char add2[50]; char city[25]; struct employee char name[100]; struct address a; int salary; void main() struct employee e; printf("enter name,address,city,salary"); scanf( %s,e.name); scanf( %s,e.a.add1); scanf( %s,e.a.add2); scanf( %s,e.a.city); scanf("%d",&e.salary); printf("detail of employaee:"); printf("%s,e.name); printf("%s,e.a.add1); printf("%s,e.a.add2); printf("%s,e.a.city); printf("%d",e.salary); getch(); 6 Explain Array of Structure with Example? Array of structure mean collection of structures. Array storing different type of structure of variables. As we have an array of basic data types, same way we can have an array variable of structure. Following example shows how an array of structure can be used, #include<conio.h> struct result char name[100]; int rollno; float cpi; void main() struct result r[66]; int i; printf("enter detail of student :"); 4

for(i=0;i<66;i++) printf("\nenter name,roll no,cpi"); scanf( %s,r[i].name); scanf("%d%f",&r[i].rollno,&r[i].cpi); printf("\n detail of student:\n"); for(i=0;i<66;i++) printf( %s,r[i].name); printf("\t%d",r[i].rollno); printf("\t%f\n",r[i].cpi); getch(); 7 Explain Pointers to Structure with example. We can define pointers to structure in very similar way as you define pointer to any other variable. Syntax: struct structure_name Member 1; Member 2;.. struct structure_name *structure_pointer; struct books char title[100]; struct books *struct_pointer,book1; Now we can store the address of a structure variable in the above defined pointer variable. To find the address of a structure variable, place the & operator before the structure s name as follow: struct_pointer = &book1; To access the member of a structure using a pointer to that structure you must use the -> operator as follow: structure_pointer->title; 5 #include<conio.h>

struct name int a; float b; void main() struct name *ptr, p; ptr = &p; /* Referencing pointer to memory address of p */ printf( Enter Integer: ); scanf( %d,&(*ptr).a); printf( Enter number: ); scanf( %f,&(*ptr).b); printf( Displaying: ); printf( %d \n %f,(*ptr).a,(*ptr).b); getch(); Structure pointer member can also be accessed using -> operator. (*ptr).a is same as ptr-> a. (*ptr).b is same as ptr-> b. 8 What is Typedef? 6 Typedef is a keyword in the C language, it is used to define own identifiers that can be used in place of type specifiers such as int, float, and double. The names you define using typedef are not new data types, but synonyms for the data types or combinations of data types they represent. The name space for a typedef name is the same as other identifier. A typedef can be used to simplify the declaration for a structure. - typedef struct char firstname[20]; char lastname[20]; int no; student; Now we can use student directly to define variables of student type without using struct keyword. Following is the example:- student student_a; It is also possible to use type definitions with structures. The name of the type definition of a structure is usually in uppercase letters. typedef struct telephone char *name;

9 What is Enumeration? int number; TELEPHONE; int main() TELEPHONE index; Index.name= xyz ; Index.number=12345; printf( Name : %s\n, index.name); printf( Telephone number: %d\n,index.number); return 0; Enumeration type allows programmer to define their own data type. Keyword enum is used to defined enumerated data type. enum type_name value1, value2,..., valuen Here, type_name is the name of enumerated data type or tag. And value1,value2,.,valuen are values of type type_name. By default, value1 will be equal to 0, value2 will be 1 and so on but, the programmer can change the default value as below: enum suit club=0; diamonds=10; hearts=20; spades=3; Declaration of enumerated variable: Above code defines the type of the data but, no any variable is created. Variable of type enum can be created as: enum Boolean false; true; enum Boolean check; Here, a variable check is declared which is of type enum boolean. enum week sunday, monday, tuesday, wednesday, thursday, friday, saturday int main() 7

Output: 4 day enum week today; today=wednesday; printf( %d day,today+1); Return 0; 8