Functions in c

Functions
• A function is a sub program, which performs a
particular task when called.
• A C program is a collection of some functions.
• main() is the predefined function from where the
execution of a program starts.
• C program = Main() + user defined functions
..continued
• Uses of functions:
• Re-useability
• Modularity
• Re-usability: C functions are used to avoid rewriting same code again and
again in a program.
• We can call functions any number of times in a program and from any place in
a program for the same task to be performed.
• Modularity: Dividing a big task into small pieces improves understandability
of very large C programs.
• A large C program can easily be tracked when it is divided into functions.
Defining a Function
• Syntax:
function definition function
declaration
return_type function_name(List of
parameters)
{
//set of statements
function body
..continued
• Function definition – This contains all the
statements to be executed.
Function definition = function declaration +function body
A function body is a group of statements enclosed
in flower brackets.
eg: void main() function declaration
{
int a=10; function definition
function body
printf(“a = %d”,a);
}
Function terminology
• Function prototype
• Function definition
• Function call
Function prototype or declaration - This informs
compiler about the function name, function
parameters and return value’s data type.
Syntax: return_type function_name(parameter list);
This is written before main() function. To inform the
compiler that there exists a function in the program.
Function Call
• This statement actually calls the function.
• Syntax: function_name( list of parameter
values);
• This statement is written in the calling
function.
• With this statement, the control is
transferred to the function definition.
• Eg: sum(10,20);
Function calls
• Functions can be defined in any one of the
following 4 different ways.
 No arguments , No return value .
 No arguments , With return value .
 With arguments , No return value .
 With arguments , With return value .
• Arguments:-
These values are passed from main()
function to the invoked function . Generally
Ex:- Program to print the sum of given two numbers using
functions?
i)No arguments , No return value
void add ( ) ; //function
prototype
void main()
{
add(); //function calling
}
void add() //function definition
{
int a,b,c;
ii)No arguments , With return value
int add ( ) ; //function prototype
void main()
{
printf(“nsum is=%d”,add()); //
function call
}
int add() ; //function definition
{
int a,b,c;
iii)With arguments , No return value
void add ( int , int ) ; //function
prototype
void main()
{
int a , b ;
printf(“enter any two numbers”);
scanf(“%d%d”,&a,&b);
add(a,b); //function call
}
iv)With arguments , With return value
int add ( int , int ) ; //function
prototype
void main()
{
int a , b ;
printf(“enter any two numbers”);
scanf(“%d%d”,&a,&b);
printf(“sum is=%d”,add(a,b));
//function call
Methods of Function calls
There are two methods
that a C function can
be called from a
program. They are,
Call by value Call by reference
Arguments
• Actual Argument: This is the argument
which is used in the function call.
– Eg: sum(a,b); // a and b are actual arguments
• Formal Argument: This is the argument
which is used in the function definition.
– Eg: void sum(int m, int n) //m and n are
formal arguments.
{
.
.
}
Call/pass by value
• In this method, the values only values of
actual arguments are passed into formal
arguments.
• Different Memory is allocated for both
actual and formal parameters.
• The value of the actual parameter can not
be modified by formal parameter.
Call/pass by reference
– In this method, the address of the variable, not value ,is
passed to the function as parameter.
– Same memory is used for both actual and formal
parameters since only address is used by both
parameters.
– The value of the actual parameter can be modified by
formal parameter.
Recursion
• It is possible in c for functions to call
themselves.
• Recursion is a technique in which a
function calls itself.
• Eg: int fact(int a) //function definition
{
int f;
if(a==1)
return 1;
else
f=a*fact(a-1); //function call
Scope of the variable
• Two kinds of variables: Local and Global
– Local - A variable declared within the function
is called as local variable. This can be used
within that function only.
• Eg: void main()
{
int x,y;
.
.
}
X and y are local variables. Their scope is upto main function.
They can be used in only main function.
Scope of the variables
• Global variable: A variable declared global
section of the program. This can be used
anywhere in the program.
• Eg: int x; // global variable
void main()
{
int y; // local variable
.
}
void sum()
{
int a,b,c; //local variables
.
C – Storage Class Specifiers
• Storage class specifiers tells the compiler
– where to store a variable,
– how to store the variable,
– what is the initial value of the variable and
– life time of the variable.
• Syntax: storage_specifier data_type
variable _name
• Types : 4
– auto
– extern
auto
• The scope of this auto variable is within
the function only.
• It is declared using auto keyword. It is
equivalent to local variable.
• All local variables are auto variables by
default.
extern
• The scope of the extern variable is global.
• It is available throughout the main
program.
• Is declared using the keyword extern. Can
be declared anywhere in the program.
• It is stored in memory.
static
• It’s scope is local. It’s value persists
between function calls.
• Declared using static keyword.
• Stored in memory.
• Default is zero.
register
• Scope is local.
• Available within the function.
• Stored in register memory.
• Default value is garbage value.
• Declared using register keyword.
…continued
S.No
.
Storage
Specifier
Storage
place
Initial /
default
value
Scop
e
Life
1 auto
CPU
Memory
Garbage
value
local Within the function only.
2 extern
CPU
memory
Zero Global
Till the end of the main program. Variable
definition might be anywhere in the C
program
3 static
CPU
memory
Zero local
Retains the value of the variable between
different function calls.
4 register
Register
memory
Garbage
value
local Within the function
Structures
• A structure is a collection of one or more
variables, usually of different types,
grouped together under a single name.
• A structure can be declared using the
keyword ‘struct’.
• Syntax: struct structure_name
{
Structure example
• structure example: struct book
{
char book_name[20];
char author[20];
int no_of_pages;
float price;
}
Structure members
• Each variable inside the structure
definition is called a structure member.
• In the book example above,
book_name , author, no_of_pages and
price are the struct members.
Structure variables
• A structure variable can be declared using
the structure name.
• Syntax:
struct struct_name
variable_name1,variable_name2,…;
• Example: struct book b1,b2,b3;
Accessing structure members
• Structure members can be accessed using
the dot(.) operator.
• Syntax : struct_variable . struct
member;
• Example : b1.book_name;
b2.book_name;
etc.,
Initializing structure members
• Intializing can be done in two ways:
1. using assignment operator (=)..
2. from the keyboard
1. Using the assignment operator:
eg: b1.no_of_pages=100;
b1. price=235;
2. From the keyboard:
eg: scanf(“%d”,&b1.no_of_pages);
Array of structures
• An array of structures can be declared in
the same way we declare any other array.
• Example: struct book b1[10];
b1 is the array of structures which
holds 10 structures.
• Each structure variable is accessed
using the array index.
Union
• A Union is a collection of one or more
variables, usually of different types,
grouped together under a single name.
• A union can be declared using the
keyword ‘union’.
• Syntax: union union_name
{
union example
• Union example: union book
{
char book_name[20];
char author[20];
int no_of_pages;
float price;
}
Union members
• Each variable inside the union definition is
called a union member.
• In the book example above,
book_name , author, no_of_pages and
price are the union members.
union variables
• A union variable can be declared using the
union name.
• Syntax:
union union_name
variable_name1,variable_name2,…;
• Example: union book b1,b2,b3;
Accessing union members
• union members can be accessed using
the dot(.) operator.
• Syntax : union_variable .union
member;
• Example : b1.book_name;
b2.book_name;
etc.,
Initializing union members
• Intializing can be done in two ways:
1. using assignment operator (=)..
2. from the keyboard
1. Using the assignment operator:
eg: b1.no_of_pages=100;
b1. price=235;
2. From the keyboard:
eg: scanf(“%d”,&b1.no_of_pages);
Array of unions
• An array of unions can be declared in the
same way we declare any other array.
• Example: union book b1[10];
b1 is the array of unions which
holds 10 unions.
• Each union variable is accessed using
the array index.
Differences between structure and
union
S.no C Structure C Union
1 Structure allocates storage space for all its members separately.
Union allocates one common storage space for all its
members.
Union finds that which of its member needs high storage
space over other members and allocates that much space
2 Structure occupies higher memory space. Union occupies lower memory space over structure.
3 We can access all members of structure at a time. We can access only one member of union at a time.
4
Structure example:
struct student
{
int mark;
char name[6];
double average;
};
Union example:
union student
{
int mark;
char name[6];
double average;
};
5
For above structure, memory allocation will be like
below.
int mark – 2B
char name[6] – 6B
double average – 8B
Total memory allocation = 2+6+8 = 16 Bytes
For above union, only 8 bytes of memory will be allocated
since double data type will occupy maximum space of
memory over other data types.
Total memory allocation = 8 Bytes
Pointers
• A pointer is a variable which points to
another variable.
• A pointer stores the address of another
variable.
• Pointers are used to allocate memory
dynamically.
Address of operator
• & is called as ‘addressof ‘operator.
• Helps to store the address of a variable in
a pointer.
• Then the pointer points to that variable.
• A pointer variable is default initalized to
null.
Advantages of pointers
• To return more than one value from a
function.
• To pass arguments to functions by
reference.
• Pointer concepts are very useful in
development of system software.
• With the address known, data can be
accesses from any where in the
program.
1 von 42

Recomendados

C string von
C stringC string
C stringUniversity of Potsdam
4.6K views22 Folien
Function in c von
Function in cFunction in c
Function in cRaj Tandukar
6.8K views22 Folien
C functions von
C functionsC functions
C functionsUniversity of Potsdam
9.9K views32 Folien
Functions in c language von
Functions in c language Functions in c language
Functions in c language tanmaymodi4
2.7K views30 Folien
Functions in C von
Functions in CFunctions in C
Functions in CKamal Acharya
25.3K views22 Folien
Function in c program von
Function in c programFunction in c program
Function in c programumesh patil
1.4K views23 Folien

Más contenido relacionado

Was ist angesagt?

Functions in c++ von
Functions in c++Functions in c++
Functions in c++Rokonuzzaman Rony
1.3K views25 Folien
Variables in C and C++ Language von
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ LanguageWay2itech
5.1K views23 Folien
Function in c von
Function in cFunction in c
Function in csavitamhaske
1.3K views18 Folien
Functions in C++ von
Functions in C++Functions in C++
Functions in C++Mohammed Sikander
2.4K views29 Folien
Pointers C programming von
Pointers  C programmingPointers  C programming
Pointers C programmingAppili Vamsi Krishna
13.1K views36 Folien

Was ist angesagt?(20)

Variables in C and C++ Language von Way2itech
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
Way2itech5.1K views
Dynamic memory allocation von Viji B
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
Viji B640 views
structure and union von student
structure and unionstructure and union
structure and union
student15.3K views
Function overloading(c++) von Ritika Sharma
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma14.6K views
FUNCTIONS IN c++ PPT von 03062679929
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
0306267992925.4K views
Pointers,virtual functions and polymorphism cpp von rajshreemuthiah
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah2.4K views
Storage classes in C von Self employed
Storage classes in C Storage classes in C
Storage classes in C
Self employed2.9K views
Structures in c language von tanmaymodi4
Structures in c languageStructures in c language
Structures in c language
tanmaymodi44.3K views
Presentation on Function in C Programming von Shuvongkor Barman
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman14.8K views
Unit 4 Foc von JAYA
Unit 4 FocUnit 4 Foc
Unit 4 Foc
JAYA3.1K views
RECURSION IN C von v_jk
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk14.1K views

Destacado

Introduction to c von
Introduction to cIntroduction to c
Introduction to csunila tharagaturi
121 views70 Folien
Introduction to c programming von
Introduction to c programmingIntroduction to c programming
Introduction to c programmingSivant Kolhe
1.2K views10 Folien
Lets tune-in-to-europe von
Lets tune-in-to-europeLets tune-in-to-europe
Lets tune-in-to-europeSPKoszecin
82 views9 Folien
Токийский гуль von
Токийский гульТокийский гуль
Токийский гульDima Luk
400 views43 Folien
Introduction to c programming von
Introduction to c programmingIntroduction to c programming
Introduction to c programminggajendra singh
2.8K views74 Folien
Ppt 1 von
Ppt 1Ppt 1
Ppt 1Gordon Clark
46 views7 Folien

Destacado(16)

Introduction to c programming von Sivant Kolhe
Introduction to c programmingIntroduction to c programming
Introduction to c programming
Sivant Kolhe1.2K views
Lets tune-in-to-europe von SPKoszecin
Lets tune-in-to-europeLets tune-in-to-europe
Lets tune-in-to-europe
SPKoszecin82 views
Токийский гуль von Dima Luk
Токийский гульТокийский гуль
Токийский гуль
Dima Luk400 views
PUCPR Aspectos Legais - Aula 2 responsabilidade civil do médico von alcindoneto
PUCPR Aspectos Legais - Aula 2   responsabilidade civil do médicoPUCPR Aspectos Legais - Aula 2   responsabilidade civil do médico
PUCPR Aspectos Legais - Aula 2 responsabilidade civil do médico
alcindoneto2.6K views
La rinconada18mar17m ldealfonsorodriguezvera[1] von Winston1968
La rinconada18mar17m ldealfonsorodriguezvera[1]La rinconada18mar17m ldealfonsorodriguezvera[1]
La rinconada18mar17m ldealfonsorodriguezvera[1]
Winston19681.2K views
Les cancers de l’endomètre : actualités 2016 von Elisabeth RUSS
Les cancers de l’endomètre : actualités 2016Les cancers de l’endomètre : actualités 2016
Les cancers de l’endomètre : actualités 2016
Elisabeth RUSS1.4K views
Behavioral Weight Loss Interventions.State of the Science, Alex Psychiatry 26... von Nilly Shams
Behavioral Weight Loss Interventions.State of the Science, Alex Psychiatry 26...Behavioral Weight Loss Interventions.State of the Science, Alex Psychiatry 26...
Behavioral Weight Loss Interventions.State of the Science, Alex Psychiatry 26...
Nilly Shams964 views
презентация 8 марта von virtualtaganrog
презентация 8 мартапрезентация 8 марта
презентация 8 марта
virtualtaganrog217 views
Cassavabase workshop ibadan March17 von solgenomics
Cassavabase workshop ibadan March17Cassavabase workshop ibadan March17
Cassavabase workshop ibadan March17
solgenomics679 views
C language (Collected By Dushmanta) von Dushmanta Nath
C language  (Collected By Dushmanta)C language  (Collected By Dushmanta)
C language (Collected By Dushmanta)
Dushmanta Nath7.9K views

Similar a Functions in c

CH.4FUNCTIONS IN C (1).pptx von
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxsangeeta borde
6 views31 Folien
Unit iii von
Unit iiiUnit iii
Unit iiiSHIKHA GAUTAM
187 views91 Folien
Function C programming von
Function C programmingFunction C programming
Function C programmingAppili Vamsi Krishna
11.6K views29 Folien
unit_2 (1).pptx von
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptxJVenkateshGoud
12 views56 Folien
CH.4FUNCTIONS IN C_FYBSC(CS).pptx von
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxSangeetaBorde3
32 views31 Folien
Functions von
FunctionsFunctions
FunctionsGolda Margret Sheeba J
467 views41 Folien

Similar a Functions in c(20)

CH.4FUNCTIONS IN C_FYBSC(CS).pptx von SangeetaBorde3
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde332 views
Functions in c language von Tanmay Modi
Functions in c languageFunctions in c language
Functions in c language
Tanmay Modi606 views
Chapter Introduction to Modular Programming.ppt von AmanuelZewdie4
Chapter Introduction to Modular Programming.pptChapter Introduction to Modular Programming.ppt
Chapter Introduction to Modular Programming.ppt
AmanuelZewdie4344 views
FUNCTIONS IN C.pptx von SKUP1
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptx
SKUP13 views
FUNCTIONS IN C.pptx von LECO9
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptx
LECO915 views
Modular Programming in C von bhawna kol
Modular Programming in CModular Programming in C
Modular Programming in C
bhawna kol777 views

Último

Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium... von
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...Lisi Hocke
35 views124 Folien
What is API von
What is APIWhat is API
What is APIartembondar5
13 views15 Folien
Ports-and-Adapters Architecture for Embedded HMI von
Ports-and-Adapters Architecture for Embedded HMIPorts-and-Adapters Architecture for Embedded HMI
Ports-and-Adapters Architecture for Embedded HMIBurkhard Stubert
33 views19 Folien
Benefits in Software Development von
Benefits in Software DevelopmentBenefits in Software Development
Benefits in Software DevelopmentJohn Valentino
5 views15 Folien
predicting-m3-devopsconMunich-2023.pptx von
predicting-m3-devopsconMunich-2023.pptxpredicting-m3-devopsconMunich-2023.pptx
predicting-m3-devopsconMunich-2023.pptxTier1 app
8 views24 Folien
Quality Assurance von
Quality Assurance Quality Assurance
Quality Assurance interworksoftware2
5 views6 Folien

Último(20)

Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium... von Lisi Hocke
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Lisi Hocke35 views
Ports-and-Adapters Architecture for Embedded HMI von Burkhard Stubert
Ports-and-Adapters Architecture for Embedded HMIPorts-and-Adapters Architecture for Embedded HMI
Ports-and-Adapters Architecture for Embedded HMI
Burkhard Stubert33 views
predicting-m3-devopsconMunich-2023.pptx von Tier1 app
predicting-m3-devopsconMunich-2023.pptxpredicting-m3-devopsconMunich-2023.pptx
predicting-m3-devopsconMunich-2023.pptx
Tier1 app8 views
Top-5-production-devconMunich-2023.pptx von Tier1 app
Top-5-production-devconMunich-2023.pptxTop-5-production-devconMunich-2023.pptx
Top-5-production-devconMunich-2023.pptx
Tier1 app9 views
tecnologia18.docx von nosi6702
tecnologia18.docxtecnologia18.docx
tecnologia18.docx
nosi67025 views
ADDO_2022_CICID_Tom_Halpin.pdf von TomHalpin9
ADDO_2022_CICID_Tom_Halpin.pdfADDO_2022_CICID_Tom_Halpin.pdf
ADDO_2022_CICID_Tom_Halpin.pdf
TomHalpin95 views
predicting-m3-devopsconMunich-2023-v2.pptx von Tier1 app
predicting-m3-devopsconMunich-2023-v2.pptxpredicting-m3-devopsconMunich-2023-v2.pptx
predicting-m3-devopsconMunich-2023-v2.pptx
Tier1 app12 views
How Workforce Management Software Empowers SMEs | TraQSuite von TraQSuite
How Workforce Management Software Empowers SMEs | TraQSuiteHow Workforce Management Software Empowers SMEs | TraQSuite
How Workforce Management Software Empowers SMEs | TraQSuite
TraQSuite6 views
Automated Testing of Microsoft Power BI Reports von RTTS
Automated Testing of Microsoft Power BI ReportsAutomated Testing of Microsoft Power BI Reports
Automated Testing of Microsoft Power BI Reports
RTTS10 views

Functions in c

  • 1. Functions • A function is a sub program, which performs a particular task when called. • A C program is a collection of some functions. • main() is the predefined function from where the execution of a program starts. • C program = Main() + user defined functions
  • 2. ..continued • Uses of functions: • Re-useability • Modularity • Re-usability: C functions are used to avoid rewriting same code again and again in a program. • We can call functions any number of times in a program and from any place in a program for the same task to be performed. • Modularity: Dividing a big task into small pieces improves understandability of very large C programs. • A large C program can easily be tracked when it is divided into functions.
  • 3. Defining a Function • Syntax: function definition function declaration return_type function_name(List of parameters) { //set of statements function body
  • 4. ..continued • Function definition – This contains all the statements to be executed. Function definition = function declaration +function body A function body is a group of statements enclosed in flower brackets. eg: void main() function declaration { int a=10; function definition function body printf(“a = %d”,a); }
  • 5. Function terminology • Function prototype • Function definition • Function call Function prototype or declaration - This informs compiler about the function name, function parameters and return value’s data type. Syntax: return_type function_name(parameter list); This is written before main() function. To inform the compiler that there exists a function in the program.
  • 6. Function Call • This statement actually calls the function. • Syntax: function_name( list of parameter values); • This statement is written in the calling function. • With this statement, the control is transferred to the function definition. • Eg: sum(10,20);
  • 7. Function calls • Functions can be defined in any one of the following 4 different ways.  No arguments , No return value .  No arguments , With return value .  With arguments , No return value .  With arguments , With return value . • Arguments:- These values are passed from main() function to the invoked function . Generally
  • 8. Ex:- Program to print the sum of given two numbers using functions? i)No arguments , No return value void add ( ) ; //function prototype void main() { add(); //function calling } void add() //function definition { int a,b,c;
  • 9. ii)No arguments , With return value int add ( ) ; //function prototype void main() { printf(“nsum is=%d”,add()); // function call } int add() ; //function definition { int a,b,c;
  • 10. iii)With arguments , No return value void add ( int , int ) ; //function prototype void main() { int a , b ; printf(“enter any two numbers”); scanf(“%d%d”,&a,&b); add(a,b); //function call }
  • 11. iv)With arguments , With return value int add ( int , int ) ; //function prototype void main() { int a , b ; printf(“enter any two numbers”); scanf(“%d%d”,&a,&b); printf(“sum is=%d”,add(a,b)); //function call
  • 12. Methods of Function calls There are two methods that a C function can be called from a program. They are, Call by value Call by reference
  • 13. Arguments • Actual Argument: This is the argument which is used in the function call. – Eg: sum(a,b); // a and b are actual arguments • Formal Argument: This is the argument which is used in the function definition. – Eg: void sum(int m, int n) //m and n are formal arguments. { . . }
  • 14. Call/pass by value • In this method, the values only values of actual arguments are passed into formal arguments. • Different Memory is allocated for both actual and formal parameters. • The value of the actual parameter can not be modified by formal parameter.
  • 15. Call/pass by reference – In this method, the address of the variable, not value ,is passed to the function as parameter. – Same memory is used for both actual and formal parameters since only address is used by both parameters. – The value of the actual parameter can be modified by formal parameter.
  • 16. Recursion • It is possible in c for functions to call themselves. • Recursion is a technique in which a function calls itself. • Eg: int fact(int a) //function definition { int f; if(a==1) return 1; else f=a*fact(a-1); //function call
  • 17. Scope of the variable • Two kinds of variables: Local and Global – Local - A variable declared within the function is called as local variable. This can be used within that function only. • Eg: void main() { int x,y; . . } X and y are local variables. Their scope is upto main function. They can be used in only main function.
  • 18. Scope of the variables • Global variable: A variable declared global section of the program. This can be used anywhere in the program. • Eg: int x; // global variable void main() { int y; // local variable . } void sum() { int a,b,c; //local variables .
  • 19. C – Storage Class Specifiers • Storage class specifiers tells the compiler – where to store a variable, – how to store the variable, – what is the initial value of the variable and – life time of the variable. • Syntax: storage_specifier data_type variable _name • Types : 4 – auto – extern
  • 20. auto • The scope of this auto variable is within the function only. • It is declared using auto keyword. It is equivalent to local variable. • All local variables are auto variables by default.
  • 21. extern • The scope of the extern variable is global. • It is available throughout the main program. • Is declared using the keyword extern. Can be declared anywhere in the program. • It is stored in memory.
  • 22. static • It’s scope is local. It’s value persists between function calls. • Declared using static keyword. • Stored in memory. • Default is zero.
  • 23. register • Scope is local. • Available within the function. • Stored in register memory. • Default value is garbage value. • Declared using register keyword.
  • 24. …continued S.No . Storage Specifier Storage place Initial / default value Scop e Life 1 auto CPU Memory Garbage value local Within the function only. 2 extern CPU memory Zero Global Till the end of the main program. Variable definition might be anywhere in the C program 3 static CPU memory Zero local Retains the value of the variable between different function calls. 4 register Register memory Garbage value local Within the function
  • 25. Structures • A structure is a collection of one or more variables, usually of different types, grouped together under a single name. • A structure can be declared using the keyword ‘struct’. • Syntax: struct structure_name {
  • 26. Structure example • structure example: struct book { char book_name[20]; char author[20]; int no_of_pages; float price; }
  • 27. Structure members • Each variable inside the structure definition is called a structure member. • In the book example above, book_name , author, no_of_pages and price are the struct members.
  • 28. Structure variables • A structure variable can be declared using the structure name. • Syntax: struct struct_name variable_name1,variable_name2,…; • Example: struct book b1,b2,b3;
  • 29. Accessing structure members • Structure members can be accessed using the dot(.) operator. • Syntax : struct_variable . struct member; • Example : b1.book_name; b2.book_name; etc.,
  • 30. Initializing structure members • Intializing can be done in two ways: 1. using assignment operator (=).. 2. from the keyboard 1. Using the assignment operator: eg: b1.no_of_pages=100; b1. price=235; 2. From the keyboard: eg: scanf(“%d”,&b1.no_of_pages);
  • 31. Array of structures • An array of structures can be declared in the same way we declare any other array. • Example: struct book b1[10]; b1 is the array of structures which holds 10 structures. • Each structure variable is accessed using the array index.
  • 32. Union • A Union is a collection of one or more variables, usually of different types, grouped together under a single name. • A union can be declared using the keyword ‘union’. • Syntax: union union_name {
  • 33. union example • Union example: union book { char book_name[20]; char author[20]; int no_of_pages; float price; }
  • 34. Union members • Each variable inside the union definition is called a union member. • In the book example above, book_name , author, no_of_pages and price are the union members.
  • 35. union variables • A union variable can be declared using the union name. • Syntax: union union_name variable_name1,variable_name2,…; • Example: union book b1,b2,b3;
  • 36. Accessing union members • union members can be accessed using the dot(.) operator. • Syntax : union_variable .union member; • Example : b1.book_name; b2.book_name; etc.,
  • 37. Initializing union members • Intializing can be done in two ways: 1. using assignment operator (=).. 2. from the keyboard 1. Using the assignment operator: eg: b1.no_of_pages=100; b1. price=235; 2. From the keyboard: eg: scanf(“%d”,&b1.no_of_pages);
  • 38. Array of unions • An array of unions can be declared in the same way we declare any other array. • Example: union book b1[10]; b1 is the array of unions which holds 10 unions. • Each union variable is accessed using the array index.
  • 39. Differences between structure and union S.no C Structure C Union 1 Structure allocates storage space for all its members separately. Union allocates one common storage space for all its members. Union finds that which of its member needs high storage space over other members and allocates that much space 2 Structure occupies higher memory space. Union occupies lower memory space over structure. 3 We can access all members of structure at a time. We can access only one member of union at a time. 4 Structure example: struct student { int mark; char name[6]; double average; }; Union example: union student { int mark; char name[6]; double average; }; 5 For above structure, memory allocation will be like below. int mark – 2B char name[6] – 6B double average – 8B Total memory allocation = 2+6+8 = 16 Bytes For above union, only 8 bytes of memory will be allocated since double data type will occupy maximum space of memory over other data types. Total memory allocation = 8 Bytes
  • 40. Pointers • A pointer is a variable which points to another variable. • A pointer stores the address of another variable. • Pointers are used to allocate memory dynamically.
  • 41. Address of operator • & is called as ‘addressof ‘operator. • Helps to store the address of a variable in a pointer. • Then the pointer points to that variable. • A pointer variable is default initalized to null.
  • 42. Advantages of pointers • To return more than one value from a function. • To pass arguments to functions by reference. • Pointer concepts are very useful in development of system software. • With the address known, data can be accesses from any where in the program.