0% found this document useful (0 votes)
3 views

User Defined Functions-structures

This document discusses user-defined functions and structures in C programming. It covers the need for user-defined functions, their elements, and various categories, as well as the definition, declaration, and initialization of structures. Additionally, it explains how to access structure members and provides examples of structure usage in code.

Uploaded by

adityavarpe69
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

User Defined Functions-structures

This document discusses user-defined functions and structures in C programming. It covers the need for user-defined functions, their elements, and various categories, as well as the definition, declaration, and initialization of structures. Additionally, it explains how to access structure members and provides examples of structure usage in code.

Uploaded by

adityavarpe69
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

UNIT V:USER DEFINED

FUNCTIONS
-by
Ujwala Wanaskar
Contents:
◦ User Defined Functions: Need for User-defined Functions, A Multi-Function
Program, Elements of User defined Functions, Definition of Functions, Return
Values and their Types, Function Calls, Function Declaration, Category of
Functions: No Arguments and no Return Values, Arguments but No Return
Values, Arguments with Return values, No Arguments but Returns a Value,
Functions that Return Multiple Values, Nesting of Functions, Recursion

Structures :
◦ What is a Structure? Structure Type Declarations, Structure Declarations,
Referencing Structure Members, Referencing Whole Structures, Initialization
of Structures.
Structures

◦ The structure in C is a user-defined data type that can be used to


group items of possibly different types into a single type.
◦ The struct keyword is used to define the structure in the C
programming language.
◦ The items in the structure are called its member and they can be
of any valid data type.
◦ Additionally, the values of a structure are stored in contiguous
memory locations.
Structure Declaration
Syntax: ◦ This syntax is also called a
structure template or
structure prototype
struct structure_name {
data_type member_name1; ◦ and no memory is allocated
data_type member_name2; to the structure in the
declaration.
....
....
};
C Structure Definition
To use structure in our program, we have to define its instance. We can do that by creating variables
of the structure type. We can define structure variables using two methods:

1. Structure Variable Declaration ◦ 2. Structure Variable Declaration


with Structure Template after Structure Template
◦ // structure declared beforehand
struct structure_name { ◦ struct structure_name variable1,
data_type member_name1; variable2, .......;
data_type member_name1;
....
....
}variable1, varaible2, ...;
50 bytes 50 bytes 8 bytes 4 bytes
Book1: Title Author Price pages

example Total=112 bytes

struct book{ struct book{


char title[50]; char title[50];
char author[50]; char author[50];
double price; double price;
int pages; int pages;
} book1; };
Struct book book1,b2,bk3[10];
Structure Initialization Structure members cannot be initialized with
the declaration. For example, the following C
program fails in the compilation.
struct book book1 = {"Learn C",
struct Point
"Dennis Ritchie", 675.50, 325};
{
int x = 0; // COMPILER ERROR: cannot
initialize members here
int y = 0; // COMPILER ERROR: cannot
initialize members here
};
The reason for the above error is simple.
When a datatype is declared, no memory is
allocated for it. Memory is allocated only
when variables are created.
We can initialize structure members in 3 ways which are as follows:
1.Initialization using Assignment 3. Initialization using Designated
Operator Initializer List
struct structure_name str; Designated Initialization allows
str.member1 = value1; structure members to be initialized
in any order. This feature has been
str.member2 = value2; added in the C99 standard.
str.member3 = value3; struct structure_name str = {
2. Initialization using Initializer List .member1 = value1, .member2 =
struct structure_name str = { value1, value2, .member3 = value3 };
value2, value3 }; The Designated Initialization is only
In this type of initialization, the supported in C but not in C++.
values are assigned in sequential
order as they are declared in the
structure template.
Access Structure Members
◦ We can access structure members by using the ( . ) dot operator.

◦ Syntax
◦ structure_variable.member1;
◦ strcuture_variable.member2;

◦ In the case where we have a pointer to the structure, we can also


use the arrow operator to access the members.
/* Create Structure EMPLOYEE for storing details (Name, Designation, gender, Date of Joining and Salary),
and store the data and update the data in structure.*/
#include <stdio.h> printf("\nGender:");
int main() { puts(e1.gender);
struct employee printf("\nDate of Joining:");
{
puts(e1.DOJ);
char name[50];
printf("\nSalary=%f",e1.salary);
char desig[30];
//updating structure data
char gender[10];
printf("\nEnter the updated salary of an employee");
char DOJ[30];
float salary; scanf("%f",&e1.salary);
}e1; printf("\nFollowing are the updated details of the employee:\nName:");
printf("Reading the data of an employee"); puts(e1.name);
printf("\n Enter name:"); printf("\nDesignation:");
gets(e1.name); puts(e1.desig);
printf("\nEnter Designation:"); printf("\nGender:");
gets(e1.desig);
puts(e1.gender);
printf("\nEnter gender");
printf("\nDate of Joining:");
gets(e1.gender);
puts(e1.DOJ);
printf("\nEnter date of joining(mm/dd/yyyy");
printf("\nSalary=%f",e1.salary);
gets(e1.DOJ);
printf("\nEnter salary:"); return 0;
scanf("%f",&e1.salary); }
printf("\nFollowing are the details of the employee:\nName:");
puts(e1.name);
printf("\nDesignation:");
puts(e1.desig);

You might also like