SlideShare ist ein Scribd-Unternehmen logo
1 von 6
Downloaden Sie, um offline zu lesen
Programming in C –Call By
Prof. A. Syed Mustafa, HKBK College of Engineering
Program to find area of a circle using #define.
#include<stdio.h>
#define PI 3.1412
int main()
{
int radius; float area;
printf("Enter the radius: ");
scanf("%d", &radius);
area=PI*radius*radius;
printf("Area=%.2f",area);
return 0;
}
Use of #if, #elif, #else and #endif :
The preprocessor directives #if, #elif, #else and #endif allows to
conditionally compile a block of code based on predefined symbols.
#include<stdio.h>
#define MAX 100
void main( )
{
#if (MAX)
printf("MAX is defined");
#else
printf ("MAX is not defined");
#endif
}
Preprocessor Directive / Macros
The C preprocessor [CPP] is a macro processor that is used
automatically by the C compiler to transform programmer defined
programs before actual compilation takes place. It is called a macro
processor because it allows the user to define macros, which are short
abbreviations for longer constructs.
It instructs the compiler to do required pre-processing before the
actual compilation. All preprocessor directives begin with the #
symbol (known as pound or hash).
List of pre-processor directives:
1. #include: This is used insert a particular header from another file.
2. #define, #undef : These are used to define and un-define
conditional compilation symbols.
3. #if, #elif, #else, #endif : These are used to conditionally skip
sections of source code.
Example:
#include “demo.h”
 tells CPP to get demo.h from the local directory and add the
content to the current source file.
#define PI 3.1412 /* defines symbolic constant */
 This directive tells the CPP to replace symbolic constant pi
with 3.1412.
#define SIZE 5 /* SIZE will be replaced with 5 */
Programming in C –Call By
Prof. A. Syed Mustafa, HKBK College of Engineering
MEMORY ALLOCATION FUNCTIONS
1. Static Memory Allocation:
Memory space allocated from stack at compile time for variables
declared in the program is fixed, it cannot be altered during
execution-time. This is called static memory allocation.
Example: int a[5]; float d;
2. Dynamic Memory Allocation
It is the process of allocating memory-space during execution-time
i.e. run time from Heap. If there is an unpredictable storage
requirement, then the dynamic allocation technique is used.
This allocation technique uses predefined functions to allocate and
release memory for data during execution-time.
There are 4 library functions for dynamic memory allocation:
malloc( ), calloc( ), free( ), realloc( )
These library functions are defined under "stdlib.h"
1. malloc( ) -memory allocation
This function is used to allocate the required memory-space
during execution-time.
The syntax is shown below:
data_type *p;
p=(data_type*)malloc(size);
Here p is pointer variable.
data_type can be int, float or char. size is number of bytes to be
allocated.If memory is successfully allocated, then address of the first
byte of allocated space is returned. If memory allocation fails, then
NULL is returned.
For ex: int *ptr;
ptr=(int*)malloc(100*sizeof(int));
The above code will allocate 200 bytes assuming sizeof(int)=2 bytes.
2. calloc( ) - contiguous allocation
This function is used to allocate the required memory-size during
execution-time and at the same time, automatically initialize
memory with 0's.
syntax :
data_type *p;
p=(data_type*)calloc(n,size);
Ex:
p=(int*)calloc(25,sizeof(int));
3. free( )
Dynamically allocated memory with either calloc( ) or malloc( ) can
be deallocated using free( ) explicitly to release space.
syntax:
free(ptr);
4. realloc() -reallocation
If the previously allocated memory is insufficient or more than
sufficient. Then, we can change memory-size previously allocated
using realloc().
The syntax is shown below:
ptr=(data_type*)realloc(ptr,newsize);
void main( )
{ int i;
int *a= (int *)malloc(10);
for(i=0;i<5;i++)
*(a+i)=i+10;
for(i=0;i<5;i++)
printf(“%dt”,*(a+i)10;
}
Output: 10 11 12 13 14
Programming in C –Call By
Prof. A. Syed Mustafa, HKBK College of Engineering
Pointers
A Pointer is just an address of the data stored in memory.
A pointer is a variable whose value is the address of
another variable, i.e., direct address of the memory
location.
Syntax: <variable_type> *<name>=&variable; eg:- int *a=&b;
‘*’ used to declare a pointer variable and also used to retrieve the value
from the pointed memory location. ‘*’ is also called as derefence operator.
#include <stdio.h>
void main ()
{
int a = 20; /* actual variable declaration */
int *ip; /* pointer variable declaration */
ip = &a; /* store address of var in pointer variable*/
printf("Address of variable a is: %xn", &a );
/* address stored in pointer variable */
printf("Address stored in variable ip is: %xn", ip );
/* access the value using the pointer */
printf("Value of *ip variable: %dn", *ip );
}
Output:
Address of variable a is: bffd8b3c
Address stored in variable ip is: bffd8b3c
Value of *ip variable: 20
Actual Parameter
 The parameter’s value (or arguments) we provide while calling
a function is known as actual arguments.
 Parameter Written In Function Call is Called “Actual Parameter”
 Actual parameters are parameters as they appear in function calls.
 The actual value that is passed into the function by a caller.
Formal Parameter
 Formal parameters are parameters as they appear in function
declarations.
 the identifier used in a method to stand for the value that is passed
into the function by a caller.
 Parameter Written In Function Definition is Called “Formal Parameter”
 While declaring a function, the arguments list of parameters we
specify are known as formal parameters.
Example
Programming in C –Call By
Prof. A. Syed Mustafa, HKBK College of Engineering
CALL BY ADDRESS
The call to the function passes variable’s
address to the called function. The actual
arguments are not copied to the formal
arguments, the addresses of actual
arguments (or parameters) are passed to
the formal parameters. Hence any
operation performed by function on
formal arguments / parameters affects
actual parameters.
void swap(int *x, int *y)
{ int t=*x; *x=*y; *y=t;
}
void main( ) {
int a=5, b=10 ;
printf("Before swap: a=%d,b=%d",a,b);
swap(&a,&b); /*calling swap function*/
printf("After swap: a= %d,b=%d",a,b);
}
Output:
Before swap: a=5,b=10
After swap: a=10,b=5
Because variable declared ‘a’, ‘b’ in
main() is different from variable ‘x’, ’y’ in
swap(). Only variable names are
different but both a and x, b and y point
to the same memory address locations
respectively.
CALL BY REFERENCE
The call to the function passes base address to
the called function. The actual arguments are
not copied to the formal arguments, only
referencing is made. Hence any operation
performed by function on formal arguments /
parameters affects actual parameters.
void change(int b[ ])
{
b[0]=10; b[1]=20; b[2]=30;
}
void main( )
{
int a[3]={ 5, 15, 25 } ;
printf("BeforeChange:%d,%d,%d",a[0],a[1],a[2]);
change(a); /*calling swap function*/
printf("AfterChange:%d,%d,%d",a[0],a[1],a[2]);
}
Output:
BeforeChange: 5,15,25
AfterChange: 10,20,30
Because array variable declared ‘b’ in change() is
referencing/ pointing to array variable ‘a’ in
main(). Only variable name is different but
both are pointing / referencing to same
memory address locations.
CALL BY VALUE
The call to the function passes either the values
or the normal variables to the called function.
The actual arguments are copied to the formal
arguments, hence any operation performed by
function on arguments doesn’t affect actual
parameters.
void swap(int a, int b)
{
int t=a; a=b; b=t;
}
void main( )
{
int a=5, b=10 ;
printf("Before swap: a=%d,b=%d",a,b);
swap(a,b); /*calling swap function*/
printf("After swap: a= %d,b=%d",a,b);
}
Output:
Before swap: a=5,b=10
After swap: a=5,b=10
Because variable declared ‘a’, ‘b’ in main() is
different from variable ‘a’, ’b’ in swap(). Only
variable names are similar but their memory
address are different and stored in different
memory locations.
Programming in C –Call By
Prof. A. Syed Mustafa, HKBK College of Engineering
STACKS
• A stack is a special type of data structure where elements are inserted from one end and elements are deleted from the same
end.
• Using this approach, the Last element Inserted is the First element to be deleted Out, and hence, stack is also called LIFO data
structure.
• The various operations performed on stack:
Insert: An element is inserted from top end. Insertion operation is called push operation. Delete: An element is
deleted from top end. Deletion operation is called pop operation. Overflow: Check whether the stack is full or not.
Underflow: Check whether the stack is empty or not.
• This can be pictorially represented as shown below:
APPLICATIONS OF STACK
1) Conversion of expressions: The compiler converts the infix expressions into postfix expressions using stack.
2) Evaluation of expression: An arithmetic expression represented in the form of either postfix or prefix can be easily evaluated
using stack.
3) Recursion: A function which calls itself is called recursive function.
4) Other applications: To find whether the string is a palindrome, to check whether a given expression is valid or not.
Programming in C –Call By
Prof. A. Syed Mustafa, HKBK College of Engineering
PRIMITIVE AND NON-PRIMITIVE DATA TYPES
Data type specifies the type of data stored in a
variable. The data type can be classified into two
types: 1) Primitive data type and 2)Non-Primitive data
type
Primitive Data Type
• The primitive data types are the basic data types
that are available in most of the programming
languages.
• The primitive data types are used to represent single
values.
Integer: This is used to represent a number without
decimal point. Eg: int a=12;
Float: This is used to represent a number with
decimal point. Eg: float a=45.1; double b=67.3;
Character: This is used to represent single character
Eg: char ch=‘C’; char ch1= ‘a’;
Non Primitive Data Type
• The data types that are derived from primary data types
are known as non-Primitive data types.
• These datatypes are used to store group of values.
• The non-primitive data types are
Arrays, Structure
Stacks, Linked list
Queue, Binary tree

Weitere ähnliche Inhalte

Was ist angesagt?

Looping statements in C
Looping statements in CLooping statements in C
Looping statements in C
Jeya Lakshmi
 

Was ist angesagt? (20)

structure of a c program
structure of a c programstructure of a c program
structure of a c program
 
Functions in c
Functions in cFunctions in c
Functions in c
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
Function in c
Function in cFunction in c
Function in c
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
C functions
C functionsC functions
C functions
 
Union In language C
Union In language CUnion In language C
Union In language C
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in C
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Function in C
Function in CFunction in C
Function in C
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
 
Linux commands
Linux commandsLinux commands
Linux commands
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
 
File in C language
File in C languageFile in C language
File in C language
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiers
 

Andere mochten auch

Call by value
Call by valueCall by value
Call by value
Dharani G
 
e computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statementse computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statements
ecomputernotes
 
e computer notes - Reference variables
e computer notes - Reference variablese computer notes - Reference variables
e computer notes - Reference variables
ecomputernotes
 
e computer notes - Manipulating data
e computer notes - Manipulating datae computer notes - Manipulating data
e computer notes - Manipulating data
ecomputernotes
 
C programs
C programsC programs
C programs
Minu S
 

Andere mochten auch (20)

Parameter passing to_functions_in_c
Parameter passing to_functions_in_cParameter passing to_functions_in_c
Parameter passing to_functions_in_c
 
Call by value
Call by valueCall by value
Call by value
 
e computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statementse computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statements
 
e computer notes - Reference variables
e computer notes - Reference variablese computer notes - Reference variables
e computer notes - Reference variables
 
e computer notes - Manipulating data
e computer notes - Manipulating datae computer notes - Manipulating data
e computer notes - Manipulating data
 
week-20x
week-20xweek-20x
week-20x
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
C programming language
C programming languageC programming language
C programming language
 
Palindrome number program in c
Palindrome number program in cPalindrome number program in c
Palindrome number program in c
 
Functions
FunctionsFunctions
Functions
 
Module 03 File Handling in C
Module 03 File Handling in CModule 03 File Handling in C
Module 03 File Handling in C
 
C lab-programs
C lab-programsC lab-programs
C lab-programs
 
Structure c
Structure cStructure c
Structure c
 
File in c
File in cFile in c
File in c
 
File handling in c
File handling in c File handling in c
File handling in c
 
UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in C
 
C programs
C programsC programs
C programs
 
C Programming
C ProgrammingC Programming
C Programming
 
File handling in c
File handling in cFile handling in c
File handling in c
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 

Ähnlich wie Pointers and call by value, reference, address in C

1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
hara69
 
presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.ppt
SandipPradhan23
 

Ähnlich wie Pointers and call by value, reference, address in C (20)

Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
Function in c
Function in cFunction in c
Function in c
 
C function
C functionC function
C function
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.ppt
 
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
 
Function in c program
Function in c programFunction in c program
Function in c program
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
 
4th unit full
4th unit full4th unit full
4th unit full
 
C structure
C structureC structure
C structure
 
Functions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxFunctions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptx
 
Function
Function Function
Function
 

Mehr von Syed Mustafa

Mehr von Syed Mustafa (20)

BPOPS203 PRINCIPLES OF PROGRAMMING USING C LAB Manual.pdf
BPOPS203 PRINCIPLES OF PROGRAMMING USING C LAB Manual.pdfBPOPS203 PRINCIPLES OF PROGRAMMING USING C LAB Manual.pdf
BPOPS203 PRINCIPLES OF PROGRAMMING USING C LAB Manual.pdf
 
18CSL58 DBMS LAB Manual.pdf
18CSL58 DBMS LAB Manual.pdf18CSL58 DBMS LAB Manual.pdf
18CSL58 DBMS LAB Manual.pdf
 
Syed IoT - module 5
Syed  IoT - module 5Syed  IoT - module 5
Syed IoT - module 5
 
IoT - module 4
IoT - module 4IoT - module 4
IoT - module 4
 
15CS81- IoT- VTU- module 3
15CS81- IoT- VTU- module 315CS81- IoT- VTU- module 3
15CS81- IoT- VTU- module 3
 
15CS81- IoT Module-2
15CS81- IoT Module-215CS81- IoT Module-2
15CS81- IoT Module-2
 
Internet of Things - module 1
Internet of Things -  module 1Internet of Things -  module 1
Internet of Things - module 1
 
15CS664- Python Application Programming- Question bank 1
15CS664- Python Application Programming- Question bank 115CS664- Python Application Programming- Question bank 1
15CS664- Python Application Programming- Question bank 1
 
15CS664- Python Application Programming VTU syllabus
15CS664- Python Application Programming  VTU syllabus15CS664- Python Application Programming  VTU syllabus
15CS664- Python Application Programming VTU syllabus
 
15CS664- Python Application Programming - Module 3
15CS664- Python Application Programming - Module 315CS664- Python Application Programming - Module 3
15CS664- Python Application Programming - Module 3
 
15CS664-Python Application Programming - Module 3 and 4
15CS664-Python Application Programming - Module 3 and 415CS664-Python Application Programming - Module 3 and 4
15CS664-Python Application Programming - Module 3 and 4
 
15CS664 Python Question Bank-3
15CS664 Python Question Bank-315CS664 Python Question Bank-3
15CS664 Python Question Bank-3
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
 
Usp notes unit6-8
Usp notes unit6-8Usp notes unit6-8
Usp notes unit6-8
 
Grid computing notes
Grid computing notesGrid computing notes
Grid computing notes
 
Unix system programming
Unix system programmingUnix system programming
Unix system programming
 
answer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdanswer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcd
 
VTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CVTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in C
 
Data structures lab c programs
Data structures lab  c programsData structures lab  c programs
Data structures lab c programs
 
Infix prefix postfix expression -conversion
Infix  prefix postfix expression -conversionInfix  prefix postfix expression -conversion
Infix prefix postfix expression -conversion
 

Kürzlich hochgeladen

"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
Kamal Acharya
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 

Kürzlich hochgeladen (20)

Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptx
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 

Pointers and call by value, reference, address in C

  • 1. Programming in C –Call By Prof. A. Syed Mustafa, HKBK College of Engineering Program to find area of a circle using #define. #include<stdio.h> #define PI 3.1412 int main() { int radius; float area; printf("Enter the radius: "); scanf("%d", &radius); area=PI*radius*radius; printf("Area=%.2f",area); return 0; } Use of #if, #elif, #else and #endif : The preprocessor directives #if, #elif, #else and #endif allows to conditionally compile a block of code based on predefined symbols. #include<stdio.h> #define MAX 100 void main( ) { #if (MAX) printf("MAX is defined"); #else printf ("MAX is not defined"); #endif } Preprocessor Directive / Macros The C preprocessor [CPP] is a macro processor that is used automatically by the C compiler to transform programmer defined programs before actual compilation takes place. It is called a macro processor because it allows the user to define macros, which are short abbreviations for longer constructs. It instructs the compiler to do required pre-processing before the actual compilation. All preprocessor directives begin with the # symbol (known as pound or hash). List of pre-processor directives: 1. #include: This is used insert a particular header from another file. 2. #define, #undef : These are used to define and un-define conditional compilation symbols. 3. #if, #elif, #else, #endif : These are used to conditionally skip sections of source code. Example: #include “demo.h”  tells CPP to get demo.h from the local directory and add the content to the current source file. #define PI 3.1412 /* defines symbolic constant */  This directive tells the CPP to replace symbolic constant pi with 3.1412. #define SIZE 5 /* SIZE will be replaced with 5 */
  • 2. Programming in C –Call By Prof. A. Syed Mustafa, HKBK College of Engineering MEMORY ALLOCATION FUNCTIONS 1. Static Memory Allocation: Memory space allocated from stack at compile time for variables declared in the program is fixed, it cannot be altered during execution-time. This is called static memory allocation. Example: int a[5]; float d; 2. Dynamic Memory Allocation It is the process of allocating memory-space during execution-time i.e. run time from Heap. If there is an unpredictable storage requirement, then the dynamic allocation technique is used. This allocation technique uses predefined functions to allocate and release memory for data during execution-time. There are 4 library functions for dynamic memory allocation: malloc( ), calloc( ), free( ), realloc( ) These library functions are defined under "stdlib.h" 1. malloc( ) -memory allocation This function is used to allocate the required memory-space during execution-time. The syntax is shown below: data_type *p; p=(data_type*)malloc(size); Here p is pointer variable. data_type can be int, float or char. size is number of bytes to be allocated.If memory is successfully allocated, then address of the first byte of allocated space is returned. If memory allocation fails, then NULL is returned. For ex: int *ptr; ptr=(int*)malloc(100*sizeof(int)); The above code will allocate 200 bytes assuming sizeof(int)=2 bytes. 2. calloc( ) - contiguous allocation This function is used to allocate the required memory-size during execution-time and at the same time, automatically initialize memory with 0's. syntax : data_type *p; p=(data_type*)calloc(n,size); Ex: p=(int*)calloc(25,sizeof(int)); 3. free( ) Dynamically allocated memory with either calloc( ) or malloc( ) can be deallocated using free( ) explicitly to release space. syntax: free(ptr); 4. realloc() -reallocation If the previously allocated memory is insufficient or more than sufficient. Then, we can change memory-size previously allocated using realloc(). The syntax is shown below: ptr=(data_type*)realloc(ptr,newsize); void main( ) { int i; int *a= (int *)malloc(10); for(i=0;i<5;i++) *(a+i)=i+10; for(i=0;i<5;i++) printf(“%dt”,*(a+i)10; } Output: 10 11 12 13 14
  • 3. Programming in C –Call By Prof. A. Syed Mustafa, HKBK College of Engineering Pointers A Pointer is just an address of the data stored in memory. A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Syntax: <variable_type> *<name>=&variable; eg:- int *a=&b; ‘*’ used to declare a pointer variable and also used to retrieve the value from the pointed memory location. ‘*’ is also called as derefence operator. #include <stdio.h> void main () { int a = 20; /* actual variable declaration */ int *ip; /* pointer variable declaration */ ip = &a; /* store address of var in pointer variable*/ printf("Address of variable a is: %xn", &a ); /* address stored in pointer variable */ printf("Address stored in variable ip is: %xn", ip ); /* access the value using the pointer */ printf("Value of *ip variable: %dn", *ip ); } Output: Address of variable a is: bffd8b3c Address stored in variable ip is: bffd8b3c Value of *ip variable: 20 Actual Parameter  The parameter’s value (or arguments) we provide while calling a function is known as actual arguments.  Parameter Written In Function Call is Called “Actual Parameter”  Actual parameters are parameters as they appear in function calls.  The actual value that is passed into the function by a caller. Formal Parameter  Formal parameters are parameters as they appear in function declarations.  the identifier used in a method to stand for the value that is passed into the function by a caller.  Parameter Written In Function Definition is Called “Formal Parameter”  While declaring a function, the arguments list of parameters we specify are known as formal parameters. Example
  • 4. Programming in C –Call By Prof. A. Syed Mustafa, HKBK College of Engineering CALL BY ADDRESS The call to the function passes variable’s address to the called function. The actual arguments are not copied to the formal arguments, the addresses of actual arguments (or parameters) are passed to the formal parameters. Hence any operation performed by function on formal arguments / parameters affects actual parameters. void swap(int *x, int *y) { int t=*x; *x=*y; *y=t; } void main( ) { int a=5, b=10 ; printf("Before swap: a=%d,b=%d",a,b); swap(&a,&b); /*calling swap function*/ printf("After swap: a= %d,b=%d",a,b); } Output: Before swap: a=5,b=10 After swap: a=10,b=5 Because variable declared ‘a’, ‘b’ in main() is different from variable ‘x’, ’y’ in swap(). Only variable names are different but both a and x, b and y point to the same memory address locations respectively. CALL BY REFERENCE The call to the function passes base address to the called function. The actual arguments are not copied to the formal arguments, only referencing is made. Hence any operation performed by function on formal arguments / parameters affects actual parameters. void change(int b[ ]) { b[0]=10; b[1]=20; b[2]=30; } void main( ) { int a[3]={ 5, 15, 25 } ; printf("BeforeChange:%d,%d,%d",a[0],a[1],a[2]); change(a); /*calling swap function*/ printf("AfterChange:%d,%d,%d",a[0],a[1],a[2]); } Output: BeforeChange: 5,15,25 AfterChange: 10,20,30 Because array variable declared ‘b’ in change() is referencing/ pointing to array variable ‘a’ in main(). Only variable name is different but both are pointing / referencing to same memory address locations. CALL BY VALUE The call to the function passes either the values or the normal variables to the called function. The actual arguments are copied to the formal arguments, hence any operation performed by function on arguments doesn’t affect actual parameters. void swap(int a, int b) { int t=a; a=b; b=t; } void main( ) { int a=5, b=10 ; printf("Before swap: a=%d,b=%d",a,b); swap(a,b); /*calling swap function*/ printf("After swap: a= %d,b=%d",a,b); } Output: Before swap: a=5,b=10 After swap: a=5,b=10 Because variable declared ‘a’, ‘b’ in main() is different from variable ‘a’, ’b’ in swap(). Only variable names are similar but their memory address are different and stored in different memory locations.
  • 5. Programming in C –Call By Prof. A. Syed Mustafa, HKBK College of Engineering STACKS • A stack is a special type of data structure where elements are inserted from one end and elements are deleted from the same end. • Using this approach, the Last element Inserted is the First element to be deleted Out, and hence, stack is also called LIFO data structure. • The various operations performed on stack: Insert: An element is inserted from top end. Insertion operation is called push operation. Delete: An element is deleted from top end. Deletion operation is called pop operation. Overflow: Check whether the stack is full or not. Underflow: Check whether the stack is empty or not. • This can be pictorially represented as shown below: APPLICATIONS OF STACK 1) Conversion of expressions: The compiler converts the infix expressions into postfix expressions using stack. 2) Evaluation of expression: An arithmetic expression represented in the form of either postfix or prefix can be easily evaluated using stack. 3) Recursion: A function which calls itself is called recursive function. 4) Other applications: To find whether the string is a palindrome, to check whether a given expression is valid or not.
  • 6. Programming in C –Call By Prof. A. Syed Mustafa, HKBK College of Engineering PRIMITIVE AND NON-PRIMITIVE DATA TYPES Data type specifies the type of data stored in a variable. The data type can be classified into two types: 1) Primitive data type and 2)Non-Primitive data type Primitive Data Type • The primitive data types are the basic data types that are available in most of the programming languages. • The primitive data types are used to represent single values. Integer: This is used to represent a number without decimal point. Eg: int a=12; Float: This is used to represent a number with decimal point. Eg: float a=45.1; double b=67.3; Character: This is used to represent single character Eg: char ch=‘C’; char ch1= ‘a’; Non Primitive Data Type • The data types that are derived from primary data types are known as non-Primitive data types. • These datatypes are used to store group of values. • The non-primitive data types are Arrays, Structure Stacks, Linked list Queue, Binary tree