SlideShare a Scribd company logo
Global variables, sorting static variables,function and arrays,
GROUP # 04
GROUP MEMBERS;
HAFIZ AHMAD ALI EN-15024
SAQIB KHOKHAR EN-15017
HASNAIN BILAL EN-15025
GLOBAL VARIABLE
• GLOBAL VARIABLES HOLD THEIR VALUES THROUGHOUT THE LIFETIME OF YOUR PROGRAM AND
THEY CAN BE ACCESSED INSIDE ANY OF THE FUNCTIONS DEFINED FOR THE PROGRAM.
• GLOBAL AND LOCAL VARIABLES
• A LOCAL VARIABLE IS A VARIABLE THAT IS DECLARED INSIDE A FUNCTION.
• A GLOBAL VARIABLE IS A VARIABLE THAT IS DECLARED OUTSIDE ALL FUNCTIONS.
• A LOCAL VARIABLE CAN ONLY BE USED IN THE FUNCTION WHERE IT IS DECLARED.
• A GLOBAL VARIABLE CAN BE USED IN ALL FUNCTIONS.
#include<stdio.h> // Global variables int A; int B; int Add() { return A + B; } int main() { int answer; // Local variable A = 5; B = 7; answer = Add(); printf("%dn",answer); return 0; }
Sorting
 To arrange a set of items in sequence.
 Many applications require sorting;
 Many applications perform sorting when
they don't have to;
 Many applications use inefficient sorting
algorithms.
Sorting Applications
 To prepare a list of student ID, names, and
scores in a table (sorted by ID or name) for
easy checking.
 To prepare a list of scores before letter
grade assignment.
 To produce a list of horses after a race
(sorted by the finishing times) for payoff
calculation.
 To prepare an originally unsorted array for
ordered binary searching.
Global variables, sorting static variables,function and arrays,
Global variables, sorting static variables,function and arrays,
Static and Automatic
Variables
• Automatic variable - memory is allocated
at block entry and deallocated at block
exit
• Static variable - memory remains
allocated as long as the program
executes
• Variables declared outside of any block
are static variables
• By default, variables declared within a
block are automatic variables
• Declare a static variable within a block by
using the reserved word static
Static and Automatic
Variables (continued)
• The syntax for declaring a static variable
is:
static dataType identifier;
• The statement
static int x;
declares x to be a static variable of the type
int
• Static variables declared within a block
are local to the block
• Their scope is the same as any other
Summary (continued)
• An automatic variable is a variable for
which memory is allocated on function
(or block) entry and deallocated on
function (or block) exit
• A static variable is a variable for which
memory remains allocated throughout
the execution of the program
• C allows functions to have default
parameters
FUNCTIONS
Basically there are two categories of function:
1. Predefined functions: available in C / C++
standard library such as stdio.h, math.h,
string.h etc.
2. User-defined functions: functions that
programmers create for specialized tasks such
as graphic and multimedia libraries,
implementation extensions or dependent etc.
FUNCTION & ARRAY:
FOR A FUNCTION TO RECEIVE AN ARRAY THROUGH A FUNCTION CALL, THE
FUNCTION’S PARAMETER LIST MUST SPECIFY THAT AN ARRAY WILL BE RECEIVED.
SYNTAX
INDICATING THAT MODIFY ARRAY EXPECTS TO RECEIVE AN ARRAY OF INTEGERS
IN PARAMETER B AND THE NUMBER OF ARRAY ELEMENTS IN PARAMETER SIZE.
THE SIZE OF THE ARRAY IS NOT REQUIRED BETWEEN
THE ARRAY BRACKETS.
Global variables, sorting static variables,function and arrays,
#include <stdio.h>
float average(float a[]);
int main(){
float avg, c[]={23.4, 55, 22.6, 3, 40.5, 18};
avg=average(c); /* Only name of array is passed as argument. */
printf("Average age=%.2f",avg);
return 0;
}
float average(float a[ ])
{
int i;
float avg, sum=0.0;
for(i=0;i<6;++i){
sum+=a[i];
}
avg =(sum/6);
return avg;
}
Output Average age=27.08
C program to pass an array containing age of person to a function. This function should find average
age and display the average age in main function.
Global variables, sorting static variables,function and arrays,
#include
void Function(int c[2][2]);
int main(){
int c[2][2],i,j;
printf("Enter 4 numbers:n");
for(i=0;i<2;++i)
for(j=0;j<2;++j){
scanf("%d",&c[i][j]);
}
Function(c); /* passing multi-dimensional array to function */
return 0;
}
void Function(int c[2][2]){
/* Instead to above line, void Function(int c[][2]){ is also valid */
int i,j;
printf("Displaying:n");
for(i=0;i<2;++i)
for(j=0;j<2;++j)
printf("%dn",c[i][j]);
} Output
Enter 4 numbers:
2
3
4
5
Displaying:
2
3
4
5
Global variables, sorting static variables,function and arrays,

More Related Content

What's hot (20)

PPTX
Storage classes in C
Self employed
 
PPTX
Method parameters in c#
Dr.Neeraj Kumar Pandey
 
PPTX
Fda unit 1 lec 1
Eugin Britto
 
PPTX
Storage Classes and Functions
Jake Bond
 
PDF
201801 CSE240 Lecture 06
Javier Gonzalez-Sanchez
 
PPTX
parameter passing in c#
khush_boo31
 
PPTX
Storage classes in C
Nitesh Bichwani
 
PPTX
User defined functions
Rokonuzzaman Rony
 
PPTX
Types of function call
ArijitDhali
 
PPT
Cinfo
teach4uin
 
PPT
User Defined Functions in C
RAJ KUMAR
 
PPTX
Functions in c
sunila tharagaturi
 
PPTX
Increment and Decrement operators in C++
Neeru Mittal
 
PPTX
Presentation on function
Abu Zaman
 
PPTX
Storage class in c
kash95
 
PPTX
Functions
preetikapri1
 
PPTX
Storage class
MANJULA_AP
 
PPSX
Function in c
savitamhaske
 
PPTX
Function in C program
Nurul Zakiah Zamri Tan
 
PDF
Learning the C Language
nTier Custom Solutions
 
Storage classes in C
Self employed
 
Method parameters in c#
Dr.Neeraj Kumar Pandey
 
Fda unit 1 lec 1
Eugin Britto
 
Storage Classes and Functions
Jake Bond
 
201801 CSE240 Lecture 06
Javier Gonzalez-Sanchez
 
parameter passing in c#
khush_boo31
 
Storage classes in C
Nitesh Bichwani
 
User defined functions
Rokonuzzaman Rony
 
Types of function call
ArijitDhali
 
Cinfo
teach4uin
 
User Defined Functions in C
RAJ KUMAR
 
Functions in c
sunila tharagaturi
 
Increment and Decrement operators in C++
Neeru Mittal
 
Presentation on function
Abu Zaman
 
Storage class in c
kash95
 
Functions
preetikapri1
 
Storage class
MANJULA_AP
 
Function in c
savitamhaske
 
Function in C program
Nurul Zakiah Zamri Tan
 
Learning the C Language
nTier Custom Solutions
 

Viewers also liked (20)

PDF
CS Unitec Nut Runners
CS Unitec
 
PPT
Stat family :Role of Stat 3 in skin disorders by yousry
M.YOUSRY Abdel-Mawla
 
PDF
JACK-IT® Line Card
Stephen Biondy
 
PDF
Transients characteristics
Aldo Uribe
 
PPTX
The osterberg cell static loading test
Alaa Metwally
 
PDF
Grammarware Memes
Eelco Visser
 
PDF
Pecha - Kucha
Lookgade
 
PPTX
Managerial Accounting
Melody Chen
 
PDF
E470 20MnV6 Steel Pipe for Hydraulic Cylinders
Shanghai Unite Steel Trading Co., Ltd.
 
PPT
Management reporting
Pw Botha
 
PPT
Requirements - Part 1
stat
 
PPTX
Measurement of hydraulic pipe and hose dynamic
FLUIDON GmbH
 
PPT
Stat3 protein in psoriasis by yousry
M.YOUSRY Abdel-Mawla
 
PDF
Hydraulic Ram; by Allen Inversin, VITA
Fatin62c
 
PPTX
Apache zookeeper seminar_trinh_viet_dung_03_2016
Viet-Dung TRINH
 
PDF
CS Unitec Pneumatic & Hydraulic Nut Runners
CS Unitec
 
PPTX
Hidraulika
Nanang s
 
PPT
Inference of the JAK-STAT Gene Network via Graphical Models
SSA KPI
 
PDF
Hydraulic losses in pipe
MOHAMMAD ATIF ALI
 
PPT
Lesson 5: Thesis Statements
bsimoneaux
 
CS Unitec Nut Runners
CS Unitec
 
Stat family :Role of Stat 3 in skin disorders by yousry
M.YOUSRY Abdel-Mawla
 
JACK-IT® Line Card
Stephen Biondy
 
Transients characteristics
Aldo Uribe
 
The osterberg cell static loading test
Alaa Metwally
 
Grammarware Memes
Eelco Visser
 
Pecha - Kucha
Lookgade
 
Managerial Accounting
Melody Chen
 
E470 20MnV6 Steel Pipe for Hydraulic Cylinders
Shanghai Unite Steel Trading Co., Ltd.
 
Management reporting
Pw Botha
 
Requirements - Part 1
stat
 
Measurement of hydraulic pipe and hose dynamic
FLUIDON GmbH
 
Stat3 protein in psoriasis by yousry
M.YOUSRY Abdel-Mawla
 
Hydraulic Ram; by Allen Inversin, VITA
Fatin62c
 
Apache zookeeper seminar_trinh_viet_dung_03_2016
Viet-Dung TRINH
 
CS Unitec Pneumatic & Hydraulic Nut Runners
CS Unitec
 
Hidraulika
Nanang s
 
Inference of the JAK-STAT Gene Network via Graphical Models
SSA KPI
 
Hydraulic losses in pipe
MOHAMMAD ATIF ALI
 
Lesson 5: Thesis Statements
bsimoneaux
 
Ad

Similar to Global variables, sorting static variables,function and arrays, (20)

PPT
Functions in c
SunithaVesalpu
 
PDF
C Programming Storage classes, Recursion
Sreedhar Chowdam
 
PPTX
cs3362 c programming and data structures
UdhayaSankar13
 
PPTX
C Programming and Data structure C Programming and Data structures
UdhayaSankar13
 
PPTX
C language 3
Arafat Bin Reza
 
PDF
Unit iii
SHIKHA GAUTAM
 
PPTX
function in c
subam3
 
PPT
C programming is a powerful, general-purpose language used for developing ope...
nadeemsk351
 
PDF
Storage Classes.pdf DJJKLF DKFF DSLKF. DSF; FD
AbcdR5
 
PPTX
C concepts and programming examples for beginners
SHAAMILIRAJAKUMAR1
 
PPT
presentation_functions_1443207686_140676.ppt
SandipPradhan23
 
PDF
C programming session5
Keroles karam khalil
 
PPTX
Unit 2 CMath behind coding.pptx
PragatheshP
 
PDF
Data structure scope of variables
Saurav Kumar
 
PPTX
C
PRADEEPA R
 
PPTX
Fundamentals of Programming Constructs.pptx
vijayapraba1
 
PDF
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 3.pdf
amanpathak160605
 
PPTX
C language presentation
bainspreet
 
PPTX
dinoC_ppt.pptx
DinobandhuThokdarCST
 
Functions in c
SunithaVesalpu
 
C Programming Storage classes, Recursion
Sreedhar Chowdam
 
cs3362 c programming and data structures
UdhayaSankar13
 
C Programming and Data structure C Programming and Data structures
UdhayaSankar13
 
C language 3
Arafat Bin Reza
 
Unit iii
SHIKHA GAUTAM
 
function in c
subam3
 
C programming is a powerful, general-purpose language used for developing ope...
nadeemsk351
 
Storage Classes.pdf DJJKLF DKFF DSLKF. DSF; FD
AbcdR5
 
C concepts and programming examples for beginners
SHAAMILIRAJAKUMAR1
 
presentation_functions_1443207686_140676.ppt
SandipPradhan23
 
C programming session5
Keroles karam khalil
 
Unit 2 CMath behind coding.pptx
PragatheshP
 
Data structure scope of variables
Saurav Kumar
 
Fundamentals of Programming Constructs.pptx
vijayapraba1
 
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 3.pdf
amanpathak160605
 
C language presentation
bainspreet
 
dinoC_ppt.pptx
DinobandhuThokdarCST
 
Ad

Recently uploaded (20)

PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PPTX
Day2 B2 Best.pptx
helenjenefa1
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Design Thinking basics for Engineers.pdf
CMR University
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
Day2 B2 Best.pptx
helenjenefa1
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
MRRS Strength and Durability of Concrete
CivilMythili
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 

Global variables, sorting static variables,function and arrays,

  • 2. GROUP # 04 GROUP MEMBERS; HAFIZ AHMAD ALI EN-15024 SAQIB KHOKHAR EN-15017 HASNAIN BILAL EN-15025
  • 3. GLOBAL VARIABLE • GLOBAL VARIABLES HOLD THEIR VALUES THROUGHOUT THE LIFETIME OF YOUR PROGRAM AND THEY CAN BE ACCESSED INSIDE ANY OF THE FUNCTIONS DEFINED FOR THE PROGRAM. • GLOBAL AND LOCAL VARIABLES • A LOCAL VARIABLE IS A VARIABLE THAT IS DECLARED INSIDE A FUNCTION. • A GLOBAL VARIABLE IS A VARIABLE THAT IS DECLARED OUTSIDE ALL FUNCTIONS. • A LOCAL VARIABLE CAN ONLY BE USED IN THE FUNCTION WHERE IT IS DECLARED. • A GLOBAL VARIABLE CAN BE USED IN ALL FUNCTIONS.
  • 4. #include<stdio.h> // Global variables int A; int B; int Add() { return A + B; } int main() { int answer; // Local variable A = 5; B = 7; answer = Add(); printf("%dn",answer); return 0; }
  • 5. Sorting  To arrange a set of items in sequence.  Many applications require sorting;  Many applications perform sorting when they don't have to;  Many applications use inefficient sorting algorithms.
  • 6. Sorting Applications  To prepare a list of student ID, names, and scores in a table (sorted by ID or name) for easy checking.  To prepare a list of scores before letter grade assignment.  To produce a list of horses after a race (sorted by the finishing times) for payoff calculation.  To prepare an originally unsorted array for ordered binary searching.
  • 9. Static and Automatic Variables • Automatic variable - memory is allocated at block entry and deallocated at block exit • Static variable - memory remains allocated as long as the program executes • Variables declared outside of any block are static variables • By default, variables declared within a block are automatic variables • Declare a static variable within a block by using the reserved word static
  • 10. Static and Automatic Variables (continued) • The syntax for declaring a static variable is: static dataType identifier; • The statement static int x; declares x to be a static variable of the type int • Static variables declared within a block are local to the block • Their scope is the same as any other
  • 11. Summary (continued) • An automatic variable is a variable for which memory is allocated on function (or block) entry and deallocated on function (or block) exit • A static variable is a variable for which memory remains allocated throughout the execution of the program • C allows functions to have default parameters
  • 12. FUNCTIONS Basically there are two categories of function: 1. Predefined functions: available in C / C++ standard library such as stdio.h, math.h, string.h etc. 2. User-defined functions: functions that programmers create for specialized tasks such as graphic and multimedia libraries, implementation extensions or dependent etc.
  • 13. FUNCTION & ARRAY: FOR A FUNCTION TO RECEIVE AN ARRAY THROUGH A FUNCTION CALL, THE FUNCTION’S PARAMETER LIST MUST SPECIFY THAT AN ARRAY WILL BE RECEIVED. SYNTAX INDICATING THAT MODIFY ARRAY EXPECTS TO RECEIVE AN ARRAY OF INTEGERS IN PARAMETER B AND THE NUMBER OF ARRAY ELEMENTS IN PARAMETER SIZE. THE SIZE OF THE ARRAY IS NOT REQUIRED BETWEEN THE ARRAY BRACKETS.
  • 15. #include <stdio.h> float average(float a[]); int main(){ float avg, c[]={23.4, 55, 22.6, 3, 40.5, 18}; avg=average(c); /* Only name of array is passed as argument. */ printf("Average age=%.2f",avg); return 0; } float average(float a[ ]) { int i; float avg, sum=0.0; for(i=0;i<6;++i){ sum+=a[i]; } avg =(sum/6); return avg; } Output Average age=27.08 C program to pass an array containing age of person to a function. This function should find average age and display the average age in main function.
  • 17. #include void Function(int c[2][2]); int main(){ int c[2][2],i,j; printf("Enter 4 numbers:n"); for(i=0;i<2;++i) for(j=0;j<2;++j){ scanf("%d",&c[i][j]); } Function(c); /* passing multi-dimensional array to function */ return 0; }
  • 18. void Function(int c[2][2]){ /* Instead to above line, void Function(int c[][2]){ is also valid */ int i,j; printf("Displaying:n"); for(i=0;i<2;++i) for(j=0;j<2;++j) printf("%dn",c[i][j]); } Output Enter 4 numbers: 2 3 4 5 Displaying: 2 3 4 5