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

DOCX
Programming Global variable
PPTX
Static variable
DOC
Storage classess of C progamming
DOC
5.program structure
PPTX
Storage classes in c language
PPTX
structure of a c program
Programming Global variable
Static variable
Storage classess of C progamming
5.program structure
Storage classes in c language
structure of a c program

What's hot (20)

PPTX
Storage classes in C
PPTX
Method parameters in c#
PPTX
Fda unit 1 lec 1
PPTX
Storage Classes and Functions
PDF
201801 CSE240 Lecture 06
PPTX
parameter passing in c#
PPTX
Storage classes in C
PPTX
User defined functions
PPTX
Types of function call
PPT
Cinfo
PPT
User Defined Functions in C
PPTX
Functions in c
PPTX
Increment and Decrement operators in C++
PPTX
Presentation on function
PPTX
Storage class in c
PPTX
Functions
PPTX
Storage class
PPSX
Function in c
PPTX
Function in C program
PDF
Learning the C Language
Storage classes in C
Method parameters in c#
Fda unit 1 lec 1
Storage Classes and Functions
201801 CSE240 Lecture 06
parameter passing in c#
Storage classes in C
User defined functions
Types of function call
Cinfo
User Defined Functions in C
Functions in c
Increment and Decrement operators in C++
Presentation on function
Storage class in c
Functions
Storage class
Function in c
Function in C program
Learning the C Language
Ad

Viewers also liked (20)

PDF
CS Unitec Nut Runners
PPT
Stat family :Role of Stat 3 in skin disorders by yousry
PDF
JACK-IT® Line Card
PDF
Transients characteristics
PPTX
The osterberg cell static loading test
PDF
Grammarware Memes
PDF
Pecha - Kucha
PPTX
Managerial Accounting
PDF
E470 20MnV6 Steel Pipe for Hydraulic Cylinders
PPT
Management reporting
PPT
Requirements - Part 1
PPTX
Measurement of hydraulic pipe and hose dynamic
PPT
Stat3 protein in psoriasis by yousry
PDF
Hydraulic Ram; by Allen Inversin, VITA
PPTX
Apache zookeeper seminar_trinh_viet_dung_03_2016
PDF
CS Unitec Pneumatic & Hydraulic Nut Runners
PPTX
Hidraulika
PPT
Inference of the JAK-STAT Gene Network via Graphical Models
PDF
Hydraulic losses in pipe
PPT
Lesson 5: Thesis Statements
CS Unitec Nut Runners
Stat family :Role of Stat 3 in skin disorders by yousry
JACK-IT® Line Card
Transients characteristics
The osterberg cell static loading test
Grammarware Memes
Pecha - Kucha
Managerial Accounting
E470 20MnV6 Steel Pipe for Hydraulic Cylinders
Management reporting
Requirements - Part 1
Measurement of hydraulic pipe and hose dynamic
Stat3 protein in psoriasis by yousry
Hydraulic Ram; by Allen Inversin, VITA
Apache zookeeper seminar_trinh_viet_dung_03_2016
CS Unitec Pneumatic & Hydraulic Nut Runners
Hidraulika
Inference of the JAK-STAT Gene Network via Graphical Models
Hydraulic losses in pipe
Lesson 5: Thesis Statements
Ad

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

PPTX
C language
PPTX
STORAGE CLASS.pptx
PPTX
C language updated
PDF
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 3.pdf
PPTX
visiblity and scope.pptx
PPTX
FUNCTIONengineeringtechnologyslidesh.pptx
PPTX
FUNCTION.pptxfkrdutytrtttrrtttttttttttttt
PDF
Unit iii
PPTX
CH.4FUNCTIONS IN C (1).pptx
PDF
Chapter 11 Function
PPT
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
PPT
Chapter Introduction to Modular Programming.ppt
PPTX
Storage classes
PPTX
Unit No 2.pptx Basic s of C Programming
PPT
Structure of a C program
PPTX
C concepts and programming examples for beginners
PPTX
Introduction to C programming
PDF
Terms and Definitions.pdf
DOC
What is storage class
PPT
Functions in c
C language
STORAGE CLASS.pptx
C language updated
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 3.pdf
visiblity and scope.pptx
FUNCTIONengineeringtechnologyslidesh.pptx
FUNCTION.pptxfkrdutytrtttrrtttttttttttttt
Unit iii
CH.4FUNCTIONS IN C (1).pptx
Chapter 11 Function
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
Chapter Introduction to Modular Programming.ppt
Storage classes
Unit No 2.pptx Basic s of C Programming
Structure of a C program
C concepts and programming examples for beginners
Introduction to C programming
Terms and Definitions.pdf
What is storage class
Functions in c

Recently uploaded (20)

DOCX
573137875-Attendance-Management-System-original
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
ETO & MEO Certificate of Competency Questions and Answers
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPT
Drone Technology Electronics components_1
PPTX
web development for engineering and engineering
PPTX
Welding lecture in detail for understanding
PPTX
Sustainable Sites - Green Building Construction
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Lecture Notes Electrical Wiring System Components
PDF
Well-logging-methods_new................
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
573137875-Attendance-Management-System-original
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
ETO & MEO Certificate of Competency Questions and Answers
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Drone Technology Electronics components_1
web development for engineering and engineering
Welding lecture in detail for understanding
Sustainable Sites - Green Building Construction
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Internet of Things (IOT) - A guide to understanding
Lecture Notes Electrical Wiring System Components
Well-logging-methods_new................
OOP with Java - Java Introduction (Basics)
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Fluid Mechanics, Module 3: Basics of Fluid Mechanics

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