SlideShare a Scribd company logo
Chapter 4:
Functions, Pointers, Errors, Testing
By Mr. Samwel Tarus
Overview
Functions
Definition
Role functions
Types of functions
Communication in functions
Types of variables
Recursion
Program demos
Pointers
Uses of pointers
Declaration of pointers
Errors
Definition
 Types of errors
Testing
Qualities of a good program
Functions
Self contained block of statements which performs a particular task and
returns a value.
The function contains the set of programming statements enclosed by {}.
Collection of functions creates a C program i.e
C = main() + add() + sum() + ….. +last()
Function also called module, block, partition, procedure, subroutine
Every C program must have the main()
Check the role of main() [ 2 main / key roles ]
Importance of functions in a program
Facilitate code Re-use
Facilitate program expansion
Facilitate the location and isolation of faulty functions in a program
Facilitate top down programming
Types of functions
1. Library Functions: Functions which are
declared in the C header files such as
scanf(), printf(), gets(), puts(), ceil(), floor(),
sqrt(), pow() etc.
Functions which have been coded, compiled
and utilized in other programs.
2. User-defined functions: Functions which
are created by the C programmer, so that
he/she can use it many times. It reduces the
complexity of a big program and optimizes
the code.
Communication in functions
Functions communicate by passing messages.
Three concepts in functions that are very important:
Function declaration
Function call
Function definition
User defined functions must be declared before they are used in the
program.
Program demo for functions
/* functions demo program */
#include<stdio.h>
#include<conio.h>
void japan();
void china();
int main()
{
printf(“n initially in main functionn”);
japan();
printf(“nfrom japan n”);
china();
printf(“nfrom china n”);
return 0;
}
void china()
{
printf(“n iam in china n”);
}
void japan()
{
printf(“n iam in japan n”);
}
Concepts of functions
1. Function Declaration:
Done outside all other functions [ between header files and main() ]
Syntax:
return_type function_name(return_type1 arg1, ……);
E.g, int addition(int a, int b, int c);
Declaration informs the compiler about the following:
 Return type of the function
 Function name
 Return types of parameters in the argument list.
 Number of parameters in the argument list
Nb: Declaration creates a template / structure of the function. This structure will
be used to link the function call and function definition.
#include<stdio.h>
#include<conio.h>
int sum(int, int);
void main()
{
int a,b,result;
printf("nEnter two numbers:");
scanf("%d %d",&a,&b);
result = sum(a,b);
printf("nThe sum is : %d", result);
}
int sum(int a, int b)
{
return a+b;
}
#include<stdio.h>
#include<conio.h>
int sum();
void main()
{
int result;
result = sum();
printf("%d", result);
}
int sum()
{
int a,b;
printf("nEnter two numbers");
scanf("%d %d",&a,&b);
return a+b;
}
Communication in functions: Cont’d…..
1. Pass by value
2. Pass by reference
Pass by value
Call/Pass by value is the process where the copies of the actual parameters
are passed in one to one correspondence to the formal parameters in the
function definition. Therefore, any changes made in the formal parameters do
not affect the actual parameters.
e.g, int addition(int a, int b, int c);
Copies
int addition(int x, int y, int z)
Nb: Variables a, b, c are called actual parameters
Variables x, y, z are called formal parameters
/* Pass by Value*/
#include<stdio.h>
#include<conio.h>
int badilisha(int, int);
int main()
{
int a=5, b=7;
printf("nOriginal valuesn");
printf("na = %dt", a);
printf("b = %dn",b);
badilisha(a,b);
printf("nValue after interchangen");
printf("na = %dt", a);
printf("b = %dn",b);
return 0;
}
int badilisha(int x, int y)
{
int z;
z = x;
x = y;
y = z;
printf("nValues in function
definitin");
printf("na = %dt", x);
printf("b = %dn",y);
}
Pass by reference
Call by reference is the process where the original values are passed to the
corresponding function definition. Any changes made in the function definition
affects the actual parameters.
eg int addition(int &a, int &b, int &c);
int addition(int *x, int *y, int *z)
Pass by reference uses the concept of pointers:
5
Operators used in Pointers
& Ampersand
Address of operator
Returns the address where the variable is residing in the memory
* Asterisk
 Value at address operator
 returns the value contained in the address
Let a = 5;
a
5
6500
Pointers
Variable which stores the address of another variable.
Pointer variables can be of type i.e, int, float, char, array, function, etc.
Pointers are user defined datatypes
Syntax for declaration:
return type *variable_name;
int *ptr;
char *c;
/* Pass by Reference*/
#include<stdio.h>
#include<conio.h>
int swap(int *x, int *y);
int main()
{
int a=5, b=7;
printf("nOriginal valuesn");
printf("na = %dt", a);
printf("b = %dn",b);
badilisha(&a,&b);
printf("nValue after interchangen");
printf("na = %dt", a);
printf("b = %dn",b);
return 0;
}
int badilisha(int *x, int *y)
{
int z;
z = *x;
*x = *y;
*y = z;
printf("nValues in function
definitionn");
printf("na = %dt", *x);
printf("b = %dn", *y);
}
Advantages / uses of pointers
Reduces the code and improves the performance, it is used to retrieving strings,
trees, etc. and used with arrays, structures, and functions.
We can return multiple values from a function using the pointer.
It makes you able to access any memory location in the computer's memory.
Dynamic memory allocation
c language, we can dynamically allocate memory using malloc() and calloc()
functions
Arrays, Functions, and Structures
C language pointers are widely used in arrays, functions, and structures.
Types of variables
Local variables:
Variables declared within a function. Lifetime and the scope is within the
function for which it is declared.
Global variables:
Variables declared outside all other functions in the program. Lifetime and
the scope is the entire program
Program example
/* Types of Variable*/
#include<stdio.h>
#include<conio.h>
int a = 20;
int show();
int main()
{
int a = 10;
printf("n a = %dn",a);
show();
return 0;
}
int show()
{
printf("n a = %dn", a);
return 0;
}
Storage Classes in C
Storage classes in C are used to determine the lifetime, visibility,
memory location, and initial value of a variable.
There are four types of storage classes in C:
Automatic
External
Static
Register
Storage
Classes
Storage
Place
Default
Value
Scope Lifetime
auto RAM Garbage Value Local Within function
extern RAM Zero Global Whole program
static RAM Zero Local Till the end of the main program,
Retains value between multiple
functions call
register Register Garbage Value Local Within the function
Storage Classes in C
Recursion in C
A process where a function calls itself several times; e.g,
/*Recursion */
#include<stdio.h>
#include<conio.h>
int main()
{
printf("nExample of recursionn");
main();
return 0;
}
/*factorial of any value */
#include <stdio.h>
#include<conio.h>
int fact(int);
int main()
{
int n,f;
printf("Enter valuen");
scanf("%d",&n);
f = fact(n);
printf("factorial = %dn", f);
}
int fact(int n)
{
int res = 1;
if (n==0)
{
return res;
}
else if ( n == 1)
{
return res;
}
else
{
res = res * n*fact(n-1);
return res;
}
Errors
Problems or faults that occur in the program, which makes the behavior of the
program abnormal.
Also known as the bugs or faults
Detected either during the time of compilation or execution.
The process of removing these bugs is known as debugging.
Types of errors
1. Syntax error
2. Run-time error
3. Logical error
4. Latent / Hidden errors
5. Semantic error
Syntax error
Errors that occur as a result of the violation of the rules of the language;
Detected by the compiler at compilation time.
Commonly occurred syntax errors are:
If we miss the parenthesis (}) while writing the code.
Displaying the value of a variable without its declaration.
If we miss the semicolon (;) at the end of the statement.
#include <stdio.h>
int main()
{
a = 10;
printf("The value of a is : %d", a);
return 0;
}
Runtime error
Errors that occur at runtime.
Detected by the compiler when running the program.
Examples
Mismatch of data types
Trying to open a file which is not created
Lack of free memory space.
Logical error
Errors that occur as a result of poor understanding of the logic.
Compiler cannot detect such errors.
Program runs and outputs results but results are wrong.
Latent error
Errors that are only visible when some set of values are used in the program.
Example:
result = (a + b) /(a – b);
Let1 a = 10, b = 5;
Output: result = 3
Let2 a = 5, b = 5;
Output: division by zero
Semantic error
Errors that occurred when the statements are not understandable by the compiler. E.g,
Use of a un-initialized variable.
int i;
i=i+2;
Type compatibility
int b = "javatpoint";
Errors in expressions
int a, b, c;
a+b = c;
Testing
The process of evaluating a system or its component(s) with the intent to find
whether it satisfies the specified requirements or not.
Executing a system in order to identify any gaps, errors, or missing
requirements in contrary to the actual requirements.
The process of analyzing a software item to detect the differences between
existing and required conditions (that is defects/errors/bugs) and to evaluate the
features of the software item.
Criteria for testing a program
1. Accuracy
2. Functionality
3. Reliability
4. Usability
5. Efficiency
6. Maintainability
7. Portability
8. Robustness
9. User friendliness
10. Completeness
11. Consistency
Functions.pptx, programming language in c

More Related Content

Similar to Functions.pptx, programming language in c

Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
Hattori Sidek
 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
indrasir
 

Similar to Functions.pptx, programming language in c (20)

Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
 
C function
C functionC function
C function
 
Basic of C Programming | 2022 Updated | By Shamsul H. Ansari
Basic of C Programming | 2022 Updated | By Shamsul H. AnsariBasic of C Programming | 2022 Updated | By Shamsul H. Ansari
Basic of C Programming | 2022 Updated | By Shamsul H. Ansari
 
Presentation 2 (1).pdf
Presentation 2 (1).pdfPresentation 2 (1).pdf
Presentation 2 (1).pdf
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
C FUNCTIONS
C FUNCTIONSC FUNCTIONS
C FUNCTIONS
 
Function in c program
Function in c programFunction in c program
Function in c program
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
 
Chapter3
Chapter3Chapter3
Chapter3
 
C structure
C structureC structure
C structure
 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
 
(Lect. 2 & 3) Introduction to C.ppt
(Lect. 2 & 3) Introduction to C.ppt(Lect. 2 & 3) Introduction to C.ppt
(Lect. 2 & 3) Introduction to C.ppt
 
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptxIIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
 
Unit 4 Foc
Unit 4 FocUnit 4 Foc
Unit 4 Foc
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
Fundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan KumariFundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan Kumari
 

More from floraaluoch3 (6)

comp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semanticscomp 122 Chapter 2.pptx,language semantics
comp 122 Chapter 2.pptx,language semantics
 
DOC-20210303-WA0017..pptx,coding stuff in c
DOC-20210303-WA0017..pptx,coding stuff in cDOC-20210303-WA0017..pptx,coding stuff in c
DOC-20210303-WA0017..pptx,coding stuff in c
 
901470_Chap1.ppt.artificial intelligence
901470_Chap1.ppt.artificial intelligence901470_Chap1.ppt.artificial intelligence
901470_Chap1.ppt.artificial intelligence
 
lecture-2-3_Memory.pdf,describing memory
lecture-2-3_Memory.pdf,describing memorylecture-2-3_Memory.pdf,describing memory
lecture-2-3_Memory.pdf,describing memory
 
lect11-12_parallel.pdf,describing parallelism
lect11-12_parallel.pdf,describing parallelismlect11-12_parallel.pdf,describing parallelism
lect11-12_parallel.pdf,describing parallelism
 
Computational models,vonneuman model,turing model
Computational models,vonneuman model,turing modelComputational models,vonneuman model,turing model
Computational models,vonneuman model,turing model
 

Recently uploaded

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 

Recently uploaded (20)

ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through Observability
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
 
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 

Functions.pptx, programming language in c

  • 1. Chapter 4: Functions, Pointers, Errors, Testing By Mr. Samwel Tarus
  • 2. Overview Functions Definition Role functions Types of functions Communication in functions Types of variables Recursion Program demos Pointers Uses of pointers Declaration of pointers Errors Definition  Types of errors Testing Qualities of a good program
  • 3. Functions Self contained block of statements which performs a particular task and returns a value. The function contains the set of programming statements enclosed by {}. Collection of functions creates a C program i.e C = main() + add() + sum() + ….. +last() Function also called module, block, partition, procedure, subroutine Every C program must have the main() Check the role of main() [ 2 main / key roles ]
  • 4. Importance of functions in a program Facilitate code Re-use Facilitate program expansion Facilitate the location and isolation of faulty functions in a program Facilitate top down programming
  • 5. Types of functions 1. Library Functions: Functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor(), sqrt(), pow() etc. Functions which have been coded, compiled and utilized in other programs. 2. User-defined functions: Functions which are created by the C programmer, so that he/she can use it many times. It reduces the complexity of a big program and optimizes the code.
  • 6. Communication in functions Functions communicate by passing messages. Three concepts in functions that are very important: Function declaration Function call Function definition User defined functions must be declared before they are used in the program.
  • 7. Program demo for functions /* functions demo program */ #include<stdio.h> #include<conio.h> void japan(); void china(); int main() { printf(“n initially in main functionn”); japan(); printf(“nfrom japan n”); china(); printf(“nfrom china n”); return 0; } void china() { printf(“n iam in china n”); } void japan() { printf(“n iam in japan n”); }
  • 8. Concepts of functions 1. Function Declaration: Done outside all other functions [ between header files and main() ] Syntax: return_type function_name(return_type1 arg1, ……); E.g, int addition(int a, int b, int c); Declaration informs the compiler about the following:  Return type of the function  Function name  Return types of parameters in the argument list.  Number of parameters in the argument list Nb: Declaration creates a template / structure of the function. This structure will be used to link the function call and function definition.
  • 9. #include<stdio.h> #include<conio.h> int sum(int, int); void main() { int a,b,result; printf("nEnter two numbers:"); scanf("%d %d",&a,&b); result = sum(a,b); printf("nThe sum is : %d", result); } int sum(int a, int b) { return a+b; }
  • 10. #include<stdio.h> #include<conio.h> int sum(); void main() { int result; result = sum(); printf("%d", result); } int sum() { int a,b; printf("nEnter two numbers"); scanf("%d %d",&a,&b); return a+b; }
  • 11. Communication in functions: Cont’d….. 1. Pass by value 2. Pass by reference
  • 12. Pass by value Call/Pass by value is the process where the copies of the actual parameters are passed in one to one correspondence to the formal parameters in the function definition. Therefore, any changes made in the formal parameters do not affect the actual parameters. e.g, int addition(int a, int b, int c); Copies int addition(int x, int y, int z) Nb: Variables a, b, c are called actual parameters Variables x, y, z are called formal parameters
  • 13. /* Pass by Value*/ #include<stdio.h> #include<conio.h> int badilisha(int, int); int main() { int a=5, b=7; printf("nOriginal valuesn"); printf("na = %dt", a); printf("b = %dn",b); badilisha(a,b); printf("nValue after interchangen"); printf("na = %dt", a); printf("b = %dn",b); return 0; } int badilisha(int x, int y) { int z; z = x; x = y; y = z; printf("nValues in function definitin"); printf("na = %dt", x); printf("b = %dn",y); }
  • 14. Pass by reference Call by reference is the process where the original values are passed to the corresponding function definition. Any changes made in the function definition affects the actual parameters. eg int addition(int &a, int &b, int &c); int addition(int *x, int *y, int *z) Pass by reference uses the concept of pointers:
  • 15. 5 Operators used in Pointers & Ampersand Address of operator Returns the address where the variable is residing in the memory * Asterisk  Value at address operator  returns the value contained in the address Let a = 5; a 5 6500
  • 16. Pointers Variable which stores the address of another variable. Pointer variables can be of type i.e, int, float, char, array, function, etc. Pointers are user defined datatypes Syntax for declaration: return type *variable_name; int *ptr; char *c;
  • 17. /* Pass by Reference*/ #include<stdio.h> #include<conio.h> int swap(int *x, int *y); int main() { int a=5, b=7; printf("nOriginal valuesn"); printf("na = %dt", a); printf("b = %dn",b); badilisha(&a,&b); printf("nValue after interchangen"); printf("na = %dt", a); printf("b = %dn",b); return 0; } int badilisha(int *x, int *y) { int z; z = *x; *x = *y; *y = z; printf("nValues in function definitionn"); printf("na = %dt", *x); printf("b = %dn", *y); }
  • 18. Advantages / uses of pointers Reduces the code and improves the performance, it is used to retrieving strings, trees, etc. and used with arrays, structures, and functions. We can return multiple values from a function using the pointer. It makes you able to access any memory location in the computer's memory. Dynamic memory allocation c language, we can dynamically allocate memory using malloc() and calloc() functions Arrays, Functions, and Structures C language pointers are widely used in arrays, functions, and structures.
  • 19. Types of variables Local variables: Variables declared within a function. Lifetime and the scope is within the function for which it is declared. Global variables: Variables declared outside all other functions in the program. Lifetime and the scope is the entire program
  • 20. Program example /* Types of Variable*/ #include<stdio.h> #include<conio.h> int a = 20; int show(); int main() { int a = 10; printf("n a = %dn",a); show(); return 0; } int show() { printf("n a = %dn", a); return 0; }
  • 21. Storage Classes in C Storage classes in C are used to determine the lifetime, visibility, memory location, and initial value of a variable. There are four types of storage classes in C: Automatic External Static Register
  • 22. Storage Classes Storage Place Default Value Scope Lifetime auto RAM Garbage Value Local Within function extern RAM Zero Global Whole program static RAM Zero Local Till the end of the main program, Retains value between multiple functions call register Register Garbage Value Local Within the function Storage Classes in C
  • 23. Recursion in C A process where a function calls itself several times; e.g, /*Recursion */ #include<stdio.h> #include<conio.h> int main() { printf("nExample of recursionn"); main(); return 0; }
  • 24. /*factorial of any value */ #include <stdio.h> #include<conio.h> int fact(int); int main() { int n,f; printf("Enter valuen"); scanf("%d",&n); f = fact(n); printf("factorial = %dn", f); } int fact(int n) { int res = 1; if (n==0) { return res; } else if ( n == 1) { return res; } else { res = res * n*fact(n-1); return res; }
  • 25. Errors Problems or faults that occur in the program, which makes the behavior of the program abnormal. Also known as the bugs or faults Detected either during the time of compilation or execution. The process of removing these bugs is known as debugging. Types of errors 1. Syntax error 2. Run-time error 3. Logical error 4. Latent / Hidden errors 5. Semantic error
  • 26. Syntax error Errors that occur as a result of the violation of the rules of the language; Detected by the compiler at compilation time. Commonly occurred syntax errors are: If we miss the parenthesis (}) while writing the code. Displaying the value of a variable without its declaration. If we miss the semicolon (;) at the end of the statement. #include <stdio.h> int main() { a = 10; printf("The value of a is : %d", a); return 0; }
  • 27. Runtime error Errors that occur at runtime. Detected by the compiler when running the program. Examples Mismatch of data types Trying to open a file which is not created Lack of free memory space.
  • 28. Logical error Errors that occur as a result of poor understanding of the logic. Compiler cannot detect such errors. Program runs and outputs results but results are wrong.
  • 29. Latent error Errors that are only visible when some set of values are used in the program. Example: result = (a + b) /(a – b); Let1 a = 10, b = 5; Output: result = 3 Let2 a = 5, b = 5; Output: division by zero
  • 30. Semantic error Errors that occurred when the statements are not understandable by the compiler. E.g, Use of a un-initialized variable. int i; i=i+2; Type compatibility int b = "javatpoint"; Errors in expressions int a, b, c; a+b = c;
  • 31. Testing The process of evaluating a system or its component(s) with the intent to find whether it satisfies the specified requirements or not. Executing a system in order to identify any gaps, errors, or missing requirements in contrary to the actual requirements. The process of analyzing a software item to detect the differences between existing and required conditions (that is defects/errors/bugs) and to evaluate the features of the software item.
  • 32. Criteria for testing a program 1. Accuracy 2. Functionality 3. Reliability 4. Usability 5. Efficiency 6. Maintainability 7. Portability 8. Robustness 9. User friendliness 10. Completeness 11. Consistency