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

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

IV Unit Second Part STRUCTURES

Write a C program using arrays and structure

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

BLM2031 Structured Programming. Zeyneb KURT

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

Programming. Structures, enums and unions

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

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

Unit IV & V Previous Papers 1 mark Answers

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

Programming in C UVic SEng 265

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

C: How to Program. Week /Mar/05

CS100 : Computer Programming

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Module 6: Array in C

Subject: Fundamental of Computer Programming 2068

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

A Fast Review of C Essentials Part I

struct structure_name { //Statements };

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

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

Preview from Notesale.co.uk Page 6 of 52

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

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

Chapter 2 - Introduction to C Programming

UNIT - V STRUCTURES AND UNIONS

12 CREATING NEW TYPES

Computers Programming Course 5. Iulian Năstac

Basic Elements of C. Staff Incharge: S.Sasirekha

Model Viva Questions for Programming in C lab

Q 1. Attempt any TEN of the following:

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

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

Variables and Constants

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

CS Programming In C

Decision Making -Branching. Class Incharge: S. Sasirekha

UNIT 3 FUNCTIONS AND ARRAYS

Lecture 3: C Programm

Short Notes of CS201

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Fundamental of Programming (C)

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

Approximately a Test II CPSC 206

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

CS201 - Introduction to Programming Glossary By

Dynamic Memory Allocation

Compiling and Running a C Program in Unix

Review of the C Programming Language

Fundamentals of Programming Session 4

Memory Allocation in C

Review of the C Programming Language for Principles of Operating Systems

Introduction to C++ with content from

EL6483: Brief Overview of C Programming Language

Chapter 1 & 2 Introduction to C Language

C PROGRAMMING Lecture 4. 1st semester

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

INTRODUCTION 1 AND REVIEW

Input And Output of C++

DEPARTMENT OF MATHS, MJ COLLEGE

Arrays. Systems Programming Concepts

Presented By : Gaurav Juneja

Data Types and Variables in C language

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

Data Structures and Algorithms (DSA) Course 4. Iulian Năstac

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Basics of Programming

Chapter 7: Structuring the Data. Lecture7 1

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

3.3 Structures. Department of CSE

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

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Variation of Pointers

Final CSE 131B Spring 2004

COMP 2355 Introduction to Systems Programming

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

C Language, Token, Keywords, Constant, variable

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

C++ PROGRAMMING BASICS

Functions BCA-105. Few Facts About Functions:

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.

Computer Systems Principles. C Pointers

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

UNIT- 3 Introduction to C++

QUIZ. Source:

High Performance Computing in C and C++

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

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

Approximately a Final Exam CPSC 206

OBJECTIVE QUESTIONS: Choose the correct alternative:

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

SRI VIDYA COLLEGE OF ENGINEERING & TECHNOLOGY, VIRUDHUNAGAR Department of CSE & IT Internal Test I

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

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

Department of Computer Applications

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

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

Transcription:

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 code. Recursion Recursion is a programming technique that allows the programmer to express operations in terms of themselves. In C, this takes the form of a function that calls itself. Structure Structure is a user-defined data type in C language which allows us to combine data of different types together. Union A union is a special data type available in C that allows to store different data types in the same memory location. Bitfield A bit field is a data structure used in computer programming. It consists of a number of adjacent computer memory locations which have been allocated to hold a sequence of bits, stored so that any single bit or group of bits within the set can be addressed. Enumeration An enumeration is a user-defined data type that consists of integral constants. To define an enumeration, keyword enum is used.. Typedef The purpose of typedef is to assign alternative names to existing types, most often those whose standard declaration is cumbersome, potentially confusing, or likely to vary from one implementation to anothe Const The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed Command line arguments Sometimes it is useful to pass information into a program when you run it. Generally, you pass information into the main( ) function via command line arguments. A command line argument is the information that follows the program's name on the command line of the operating system. When main is called, it is called with two arguments. The first (conventionally called argc, for argument count) is the number of commandline arguments the program was invoked with. The second (argv, for argument vector) is a pointer to an array of character strings thatcontain the arguments, one per string. For example, when you compile a program, you might type something like the following after the command prompt,

$./a.out 10 20 Example Program void main(int argc,char *arg[]) int a,b,c; a=atoi(arg[1]); // atoi() converts string to number b=atoi(arg[2]); c=a+b; printf("\nsum=%d",c); Sum=30 Number of arguments=3 Explanation In the above output, the three arguments are sum, 10 and 20. By default, the argument sum is stored in arg[0] whereas 10 and 20 are stored in arg[1], arg[2] respectively. The argument in the main i.e argc counts the number of arguments. Recursion In C, it is possible for the functions to call themselves. A function is called, recursive if a statement within the body of a function calls the same function. Thus recursion is the process of defining something in terms of itself. Example Program Let us now see a simple example of recursion. Suppose we want to calculate the factorial value of an integer. As we know, the factorial of a number is the product of all the integers between 1 and that number. For example, 4 factorial is 4 * 3 * 2 * 1. This can also be expressed as 4! = 4 * 3! where! stands for factorial. Thus factorial of a number can be expressed in the form of itself. Hence this can be programmed using recursion as follows. //Recursion Example #include<stdio.h> #include<conio.h> int fact(int n); // function prototype int fact(int n)

int f; if(n==0 n==1) return(1); f=fact(n-1)*n; //fact() is recursive function return(f); int n,res; printf("\nenter the number:"); scanf("%d",&n); res=fact(n); printf("\nfactorial=%d",res); Enter the number:5 Factorial=120 Function Prototypes In modern, properly written C programs, all functions must be declared before they are used. This is normally accomplished using a function prototype. Function prototypes were not part of the original C language, but were added by C89. Although prototypes are not technically required, their use is strongly encouraged. Prototypes enable the compiler to provide stronger type checking, somewhat like that provided by languages such as Pascal. When you use prototypes, the compiler can find and report any questionable type conversions between the arguments used to call a function and the type of its parameters. The compiler will also catch differences between the number of arguments used to call a function and the number of parameters in the function. The general form of a function prototype is type func_name(type parm_namel, type parm_name2,..., type parm_namen); The use of parameter names is optional. However, they enable the compiler to identify any type mismatches by name when an error occurs, so it is a good idea to include them. Example Program //Using user defined function #include<stdio.h>

#include<conio.h> void message(); // function prototype

printf("\nhow r u?"); message(); void message() // function definition printf("\ni am fine!"); How r u? I am fine! Standard Library Function Prototypes Any standard library function used by your program must be prototyped. To accomplish this, you must include the appropriate header for each library function. All necessary headers are provided by the C compiler. In C, the library headers are (usually) files that use the.h extension. A header contains two main elements: any definitions used by the library functions and the prototypes for the library functions. For example, <stdio.h> is included in almost all programs in this book because it contains the prototype for printf( ). Declaring Variable Length Parameter Lists You can specify a function that has a variable number of parameters. The most common example is printf( ). To tell the compiler that an unknown number of arguments will be passed to a function, you must end the declaration of its parameters using three periods. For example, this prototype specifies that func( ) will have at least two integer parameters and an unknown number (including 0) of parameters after that: int func(int a, int b,...); This form of declaration is also used by a function's definition. Any function that uses a variable number of parameters must have at least one actual parameter.

For example, this is incorrect: int func(...); /* illegal */ Structures We have seen earlier how ordinary variables can hold one piece of information and how arrays can hold a number of pieces of information of the same data type. These two data types can handle a great variety of situations. But quite often we deal with entities that are collection of dissimilar data types. For example, suppose you want to store data about a book. You might want to store its name (a string), its price (a float) and number of pages in it (an int). If data about say 3 such books is to be stored, then we can follow two approaches: (a) Construct individual arrays, one for storing names, another for storing prices and still another for storing number of pages. (b) Use a structure variable. In the first approach, the program becomes more difficult to handle as the number of items relating to the book go on increasing. For example, we would be required to use a number of arrays, if we also decide to store name of the publisher, date of purchase of book, etc. To solve this problem, C provides a special data type the structure. Structure Definition A structure contains a number of data types grouped together. These data types may or may not be of the same types similar data types. For example, suppose you want to store data about a book. You might want to store its name (a string), its price (a float) and number of pages in it (an int). If data about say 3 such books is to be stored, then we can follow two approaches: (c) Construct individual arrays, one for storing names, another for storing prices and still another for storing number of pages. (d) Use a structure variable. In the first approach, the program becomes more difficult to handle as the number of items relating to the book go on increasing. For example, we would be required to use a number of arrays, if we also decide to store name of the publisher, date of purchase of book, etc. To solve this problem, C provides a special data type the structure. Structure Definition A structure contains a number of data types grouped together. These data types may or may not be of the same types

Unlike arrays which can store elements of same data type, a structure can hold data of different data types. Declaring a Structure The general form of a structure declaration statement is given below: Syntax struct <structure name> structure element 1 ; structure element 2 ; structure element 3 ;...... ; Example struct book char name ; float price ; int pages ; ; The above statement defines a new data type called struct book. Each variable of this data type will consist of a character variable called name, a float variable called price and an integer variable called pages. Once the new structure data type has been defined one or more variables can be declared to be of that type. For example the variables b1, b2, b3 can be declared to be of the type struct book, as, struct book b1, b2, b3 ;

This statement sets aside space in memory. It makes available space to hold all the elements in the structure in this case, 7 bytes one for name, four for price and two for pages. These bytes are always in adjacent memory locations. If we so desire, we can combine the declaration of the structure type and the structure variables in one statement. For example, struct book char name ; float price ; int pages ; ; struct book b1, b2, b3 ; (or) struct book char name ; float price ; int pages ; b1, b2, b3 ; Important points about structure declaration The closing brace in the structure type declaration must be followed by a semicolon. It is important to understand that a structure type declaration does not tell the compiler to reserve any space in memory. All a structure declaration does is, it defines the form of the structure. Usually structure type declaration appears at the top of the source code file, before any variables or functions are defined. In very large programs they are usually put in a separate header file, and the file is included (using the preprocessor directive #include) in whichever program we want to use this structure type. Initialization of structures

Like primary variables and arrays, structure variables can also be initialized where they are declared. The format used is quite similar to that used to initiate arrays. struct book char name[10] ; float price ; int pages ; ; struct book b1 = "Basic", 130.00, 550 ; struct book b2 = "Physics", 150.80, 800 ; Accessing Structure Elements Having declared the structure type and the structure variables, let us see how the elements of the structure can be accessed. In arrays we can access individual elements of an array using a subscript. Structures use a different scheme. They use a dot (.) operator. So to refer to pages of the structure defined in our sample program we have to use, b1.pages Similarly, to refer to price we would use, b1.price Note that before the dot there must always be a structure variable and after the dot there must always be a structure element. Example Program #include<stdio.h> #include<conio.h> struct book char name[20]; float price;

int pages; ; struct book b1="c Language",275.50,450; // Initialization of structure variable printf("\nbook Name=%s",b1.name); printf("\nprice=%.2f",b1.price); printf("\npages=%d",b1.pages); Book Name=C Language Price=275.50 Pages=450 How Structure Elements are Stored Whatever be the elements of a structure, they are always stored in contiguous memory locations. The following program would illustrate this: main() struct book char name ; float price ; int pages ; ; struct book b1 = 'B', 130.00, 550 ; printf ( "\naddress of name = %u", &b1.name ) ; printf ( "\naddress of price = %u", &b1.price ) ;

printf ( "\naddress of pages = %u", &b1.pages ) ; Memory Representation Actually the structure elements are stored in memory as shown below Address of name = 65518 Address of price = 65519 Address of pages = 65523 Example Program struct book char name[20]; float price; int page; b1; printf("\nenter the name, price, pages of book\n"); scanf("%s%f%d",&b1.name,&b1.price,&b1.page); printf("\nbook Name=%s",b1.name); printf("\nprice=%.2f",b1.price); printf("\npages=%d",b1.page);

Enter the name, price, pages of book CP 275.50 370 Book Name=CP Price=275.50 Pages=370 Arrays of Structures If we want to store data of 100 books we would be required to use 100 different structure variables from b1 to b100, which is definitely not very convenient. A better approach would be to use an array of structures. The syntax we use to reference each element of the array b is similar to the syntax used for arrays of ints and chars. For example, we refer to zeroth book s price as b[0].price. Similarly, we refer first book s pages as b[1].pages. Example Program 1 //Array of structures struct student int rno; char name[20]; ; struct student s[2]=25,"ritchie",20,"babbage"; int i;

printf("\n***students Details***"); for(i=0;i<2;i++) printf("\nroll no=%d \nname=%s",s[i].rno,s[i].name); printf("\n-----------------\n"); Expected Output ***Students Details*** Roll no=25 Name=Ritchie ----------------- Roll no=20 Name=Babbage ----------------- Example Program 2 //Array of structures elements accepting from keyboard struct student int rno; char name[20]; char branch[10];

; ; struct student s[2]; int i; printf("\nenter Rollno, Name & Branch of two students:\n"); for(i=0;i<2;i++) scanf("%d%s%s",&s[i].rno,&s[i].name,&s[i].branch); printf("\n"); printf("\n***students Details***"); for(i=0;i<2;i++) printf("\nroll no=%d \nname=%s \nbranch=%s",s[i].rno,s[i].name,s[i].branch); printf("\n-------------------\n");

Enter Rollno, Name & Branch of two students: 25 Ritchie CSE 20 Dennis CSE ***Students Details*** Roll no=25 Name=Ritchie Branch=CSE ------------------- Roll no=20 Name=Dennis Branch=CSE ------------------- In an array of structures, all elements of the array are stored in adjacent memory locations. Since each element of this array is a structure, and since all structure elements are always stored in adjacent locations you can very well visualize the arrangement of array of structures in memory.

In our example, s[0] s rno, name and branch in memory would be immediately followed by s[1] s rno, name and branch, and so on. Passing Structures to Functions Like an ordinary variable, a structure variable can also be passed to a function. The following are the three different ways of passing structure arguments to functions. (a)passing individual structure elements to a function Example Program //to pass an individual structure element void display(char *n,char *a,int p); // function prototype struct book char name[20]; char author[20]; int price; ; struct book b="computer","ayesha",250; display(b.name,b.author,b.price); // passing individual structure elements to display() void display(char *n,char *a,int p) printf("\nbook Name:%s \nauthor:%s \nprice:%d",n,a,p);

Book Name:Computer Author:Ayesha Price:250 In the above program, we are passing the base addresses of the arrays name and author, and the value stored in price. Thus, this is a mixed call a call by reference as well as a call by value. (b) Passing entire structure to a function It can be immediately realized that to pass individual elements would become more tedious as the number of structure elements go on increasing. A better way would be to pass the entire structure variable at a time. Example Program //to pass an entire structure void display(); struct book // function prototype // structure named book char name[20]; char author[20]; int price; ; struct book b="computer","ayesha",200; display(b); // passing entire structure variable b void display(struct book b1)

printf("\nname=%s \nauthor=%s \nprice=%d",b1.name,b1.author,b1.price); Book Name:Computer Author:Ayesha Price:250 (c) Passing address of structure to a function //to pass the address of structure Like variables, the address of structure variable can be passed to a function. Example Program void display(); struct book // function prototype // structure definition outside the main() char name[20]; char author[20]; int price; ; struct book b="computer","ayesha",200; display(&b); // passing address of structure variable b to display() void display(struct book *b1)

printf("\nname=%s \nauthor=%s \nprice=%d",b1->name,b1->author,b1->price); Book Name:Computer Author:Ayesha Price:250 Structure Pointers The way we can have a pointer pointing to an int, or a pointer pointing to a char, similarly we can have a pointer pointing to a struct. Such pointers are known as structure pointers. //Pointer to structure struct student; char name[20]; int rno; float per; ; struct student s="ayesha",25,71.00; struct student *ptr; ptr=&s; // pointer ptr pointing to structure variable s printf("\nname:%s \nrollno:%d \npercentage:%.2f",ptr->name,ptr->rno,ptr->per); Name: Ayesha

Rollno:25 Percentage:71.00 Arrays and Structures within Structures A member of a structure can be either a simple variable, such as an int or double, or an aggregate type. In C, aggregate types are arrays and structures. You have already seen one type of aggregate element: the character arrays used in addr. A member of a structure that is an array is treated as you might expect from the earlier examples. For example, consider this structure: struct x int a[10] [10]; /* 10 x 10 array of ints */ float b; y; To reference integer 3,7 in a of structure y, write y.a[3][7] One structure can be nested within another structure. Using this facility complex data types can be created. Example Program //Structure within Structure struct student // outer structure char name[20]; int rno; struct address // inner structure char city[20]; char state[20];

a; s; printf("enter Student name, Roll no,city,state\n"); scanf("%s%d%s%s",&s.name,&s.rno,&s.a.city,&s.a.state); printf("\nname:%s \nroll no:%d",s.name,s.rno); printf("\ncity:%s \nstate:%s",s.a.city,s.a.state); Enter Student name, Roll no,city,state Ayesha 25 Kurnool AP Name:Ayesha Roll no:25 City:Kurnool State:AP In the above program, the method used to access the element of a structure that is part of another structure. For this the dot operator is used twice, as in the expression, s.a.city and s.a.state Unions Like structure, a union can hold data belonging to different data types but it hold only one object at a time. In the structure each member has its own memory locations whereas, members of unions have same memory locations. The union requires bytes that are equal to the number of bytes required for the largest member. For example, if the union contains char, integer and float then the number of bytes reserved in the memory is 4 bytes (i.e. the size of float). Unlike structure members which can be initialized all at the same time, only one union member should be initialized at a time. Syntax

The syntax of union is similar to the structure which is shown below union <union_ name> union element 1 ; union element 2 ; union element 3 ;...... ; Let us now observe the difference between union and structure by using the following program. Example Program //To show the difference between structure & union struct student1 // structure int rno; char grade; ; union student2 //union int rno; char grade; ; struct student1 s=25,'a'; // initialization of structure members at a time union student2 u;

printf("\nrollno=%d",s.rno); printf("\ngrade=%c",s.grade); u.rno=30; // initialization of union member 1 printf("\nrollno=%d",u.rno); u.grade='b'; // initialization of union member 2 printf("\ngrade=%c",u.grade); printf("\nsize of Structure=%d bytes",sizeof(s)); printf("\nsize of Union=%d bytes",sizeof(u)); // displaying size of structure // displaying size of union Example Program Rollno=25 Grade=A Rollno=50 Grade=B Size of Structure=3 Bytes Size of Union=2 Bytes In the above, the size of structure is sum of the sizes of all its members i.e. int & char which is (2+1)=3 bytes whereas the size of union is the size of the member which belongs to largest data type i.e. int which is of 2 bytes. Bit Fields Bit field provides exact amount of bits required for storage of values. If the variable value is 1 or 0, we need a single bit to store it. Similarly, if the variable value is between 0 and 3, the two bits are sufficient for storing these values. In the same what, if the variable value is between 0 and 7 the three bits are enough to hold the values. To hold the information we use variables. The variables occupy a minimum of one byte for char and two bytes for integer. Instead of using complete integer if bits are used, space of memory can be saved.

Syntax struct numbers unsigned int first:2; unsigned int second:3; ; The colon(:) in the above declaration tells the compiler that bit fields are used in the structure and the number after it indicates how many bits are required to allot to the field. Example Program struct numbers unsigned int first:2; unsigned int second:3; ; struct numbers n; n.first=3; n.second=7; printf("\nfirst Number:%d",n.first); printf("\nsecond Number:%d",n.second); Example Program struct numbers unsigned int first:2; unsigned int second:3;

; struct numbers n; n.first=3; n.second=7; printf("\nfirst Number:%d",n.first); printf("\nsecond Number:%d",n.second); First Number:3 Second Number:7 Enumerations The Enumerated data type helps the programmer to create his/her own data type and define what values the variables of these data types can hold. Thus the use of Enumerated data type improves the readability of the program. The keyword enum is used for declaring enumerated data types. Example enum days sun, mon, tue, wed, thu, fri, sat // no semicolon(;) after identifiers ; enum days d1,d2; // d1, d2 are variables of data type days The above statement creates a user defined data type. The keyword enum is followed a tag name days. The identifies sun, mon etc are called enumerators. Their values are constant unsigned integers and start from 0. By default, the identifier sun refers to 0, mon refers to 1 and so on.

Example Program 1 enum branch // branch is enumerated data type cse,ece,eee // cse,ece,eee are values of variables b1,b2,b3 ; enum branch b1,b2,b3; b1=cse; // internally cse means 0 b2=ece; // internally ece means 1 b3=eee; // internally eee means 2 printf("\ncse=%d",b1); printf("\nece=%d",b2); printf("\neee=%d",b3); CSE=0 ECE=1 EEE=2 Example Program 2 enum branch // branch is enumerated data type cse=5,ece=4,eee=2 // cse,ece,eee are values of variables b1,b2,b3 ;

enum branch b1,b2,b3; b1=cse; // internally cse means 5 b2=ece; // internally ece means 4 b3=eee; // internally eee means 2 printf("\ncse=%d",b1); printf("\nece=%d",b2); printf("\neee=%d",b3); CSE=5 ECE=4 EEE=2 Typedef typedef provides the facility of providing alternative names for the basic data types in C such as int, pointers and structures. A program that carries out so many tasks would be very big in size. In such a program if we start using the normal integer data type to represent variables that hold different entities we would soon lose track of what that integer value actually represents. This can be overcome by using typdef. Example Programs (1)//using typdef with simple data types typedef int integer; // using integer instead of int integer a; a=10;

printf("\na=%d",a); a=10 (2)//Using typedef with pointers int a=10; typedef int* integer; integer ptr; // ptr is a pointer to int ptr=&a; printf("\naddress of a is %u",ptr); printf("\nvalue of a is %d",*ptr); Address of a is 65524 Value of a is 10 (3)//using typedef with structure struct student int rno; char name[15]; ; typedef struct student profile; profile s=25,"shariff";

printf("\nrollno=%d \nname=%s",s.rno,s.name); Assignment Questions Unit IV 1. How to copy and compare structure variables. Illustrate with example. 2. Write a program in C to display the size of structure elements using sizeof operator. 3. Give difference between the structures and arrays. 4. Explain structure declaration in C. How we can use different Ways of declaring Structure using Typedef? 5. How to point and access structure members using structure pointers, explain with example. 6. How to pass the structures to functions as an argument? Explain with a suitable example. 7. Define union. Differentiate between union and structures. 8. Explain the following: (i) Structure with in structure. (ii) Self reference structure. 9. With a suitable example, explain the difference between Structure and Unions. 10. What are the advantages and disadvantages of recursion?