Lecture 6
C
Language
ENG : HATEM ABD EL-SALAM
• Casting
• Dynamic Memory Allocation
• Scope and Lifetime
Agenda
Casting (Cont.)
• Type casting is a way to convert a variable from one data type to another data
type. For example, if you want to store a long value into a simple integer then
you can type cast long to int. You can convert values from one type to another
explicitly using the cast operator.
There are two types of type casting in c language.
1. Implicit Conversion
2. Explicit Conversion
Casting (Cont.)
1-Implicit conversion
• Implicit conversions do not required any operator for converted . They are
automatically performed when a value is copied to a compatible type in
program.
Casting (Cont.)
1- Explicit conversion
• In c language , Many conversions, specially those that imply a different
interpretation of the value, require an explicit conversion.
Casting (Cont.)
Inbuilt Typecast Functions In C
• There are many inbuilt typecasting functions available in C language which
performs data type conversion from one type to another.
Dynamic Memory Allocation
• In C, the exact size of array is unknown until compile time, i.e., the time
when a compiler compiles your code into a computer understandable
language. So, sometimes the size of the array can be insufficient or more than
required.
• Dynamic memory allocation allows your program to obtain more memory
space while running, or to release it if it's not required.
• In simple terms, Dynamic memory allocation allows you to manually handle
memory space for your program.
Dynamic Memory Allocation (Cont.)
• there are 4 library functions under "stdlib.h" for dynamic memory allocation.
Dynamic Memory Allocation (Cont.)
malloc()
• The name malloc stands for "memory allocation".
• The function malloc() reserves a block of memory of specified size and
return a pointer of type void which can be casted into pointer of any
form.
• Syntax of malloc()
Dynamic Memory Allocation (Cont.)
• Here, ptr is pointer of cast-type. The malloc() function returns a
pointer to an area of memory with size of byte size. If the space is
insufficient, allocation fails and returns NULL pointer.
• This statement will allocate either 200 or 400 according to size of int 2
or 4 bytes respectively and the pointer points to the address of first
byte of memory.
EXAMPLE PROGRAM FOR MALLOC() FUNCTION IN C
Dynamic Memory Allocation (Cont.)
calloc()
• The name calloc stands for "contiguous allocation".
• The only difference between malloc() and calloc() is that, malloc()
allocates single block of memory whereas calloc() allocates multiple
blocks of memory each of same size and sets all bytes to zero.
• Syntax of calloc()
Dynamic Memory Allocation (Cont.)
• example:
• This statement allocates contiguous space in memory for an array of 25
elements each of size of float, i.e, 4 bytes.
EXAMPLE PROGRAM FOR CALLOC() FUNCTION IN C
Dynamic Memory Allocation (Cont.)
free()
• Dynamically allocated memory created with either calloc() or malloc()
doesn't get freed on its own. You must explicitly use free() to release the
space.
• Syntax of free()
• This statement frees the space allocated in the memory pointed by ptr.
Dynamic Memory Allocation (Cont.)
realloc()
• If the previously allocated memory is insufficient or more than
required, you can change the previously allocated memory size using
realloc().
• Syntax of realloc()
• Here, ptr is reallocated with size of newsize.
EXAMPLE PROGRAM FOR REALLOC() AND FREE() FUNCTIONS IN C
Dynamic Memory Allocation (Cont.)
• Difference between static memory allocation and dynamic memory
allocation in c.
Dynamic Memory Allocation (Cont.)
• Difference between malloc() and calloc() functions in c.
C- language Lecture 6
Scope and Lifetime
• Every variable in C programming has two properties: type and storage class.
• Type refers to the data type of a variable. And, storage class determines the scope and
lifetime of a variable.
There are 5 types of storage class:
1. automatic
2. external
3. static
4. Register
5. Constants
Scope and Lifetime(Cont.)
1-Local Variable
• The variables declared inside the function are automatic or local variables.
• The local variables exist only inside the function in which it is declared. When the
function exits, the local variables are destroyed.
• In the above code, n1 is destroyed when func() exits. Likewise, n gets destroyed when
main() exits.
Scope and Lifetime(Cont.)
Global Variable
• Variables that are declared outside of all functions are known as external
variables. External or global variables are accessible to any function.
Scope and Lifetime(Cont.)
2-External Variable
• Suppose, a global variable is declared in file1. If you try to use that variable in a
different file file2, the compiler will complain. To solve this problem, keyword
extern is used in file2 to indicate that the external variable is declared in another
file.
Scope and Lifetime(Cont.)
3-Register Variable
• The register keyword is used to declare register variables. register variables were
supposed to be faster than local variables.
• However, modern compilers are very good at code optimization and there is a
rare chance that using register variables will make your program faster.
Scope and Lifetime(Cont.)
4-Static Variable
• A static variable is declared by using keyword static.
• The value of a static variable persists until the end of the program.
Scope and Lifetime(Cont.)
Explanation of program
• During the first function call, the value of c is equal to 0. Then, it's value is
increased by 5.
• During the second function call, variable c is not initialized to 0 again. It's
because c is a static variable. So, 5 is displayed on the screen.
Scope and Lifetime(Cont.)
4-Constants Variable
• A constant is a value or an identifier whose value cannot be altered in a program.
For example: 1, 2.5, "C programming is easy", etc.
• Here, PI is a constant. Basically what it means is that, PI and 3.14 is same for this
program.
C- language Lecture 6

More Related Content

PPTX
Basic Structure of a Computer System
PDF
C- language Lecture 5
PDF
C- language Lecture 7
PDF
C- language Lecture 4
PDF
C- language Lecture 8
PDF
Embedded C - Optimization techniques
PPTX
Lecture 3 instruction set
PPTX
Peephole optimization techniques in compiler design
Basic Structure of a Computer System
C- language Lecture 5
C- language Lecture 7
C- language Lecture 4
C- language Lecture 8
Embedded C - Optimization techniques
Lecture 3 instruction set
Peephole optimization techniques in compiler design

What's hot (20)

PPTX
Compiler in System Programming/Code Optimization techniques in System Program...
PPTX
C basics
PPTX
TensorRT survey
PPTX
Code Optimization
PDF
Run time storage
PPTX
Instruction Set (Part 2)
PPTX
Unit v memory & programmable logic devices
PPTX
Introduction to Assembly Language
PDF
Code optimization in compiler design
PPT
Assembly Language Basics
PDF
PPTX
Lecture 4 assembly language
PPTX
Programming the basic computer
PDF
Compiler Design- Machine Independent Optimizations
PDF
Code generation in Compiler Design
PDF
Learning New Semi-Supervised Deep Auto-encoder Features for Statistical Machi...
PPTX
Presentation on C++ programming
PDF
Embedded C - Lecture 3
PDF
08 subprograms
PDF
Supporting Arrays and Allocatables in LFortran
Compiler in System Programming/Code Optimization techniques in System Program...
C basics
TensorRT survey
Code Optimization
Run time storage
Instruction Set (Part 2)
Unit v memory & programmable logic devices
Introduction to Assembly Language
Code optimization in compiler design
Assembly Language Basics
Lecture 4 assembly language
Programming the basic computer
Compiler Design- Machine Independent Optimizations
Code generation in Compiler Design
Learning New Semi-Supervised Deep Auto-encoder Features for Statistical Machi...
Presentation on C++ programming
Embedded C - Lecture 3
08 subprograms
Supporting Arrays and Allocatables in LFortran
Ad

Similar to C- language Lecture 6 (20)

PPTX
Dynamic Memory Allocation in C
PPTX
BBACA-SEM-III-Datastructure-PPT(0) for third semestetr
PPTX
Lecture 3.3.1 Dynamic Memory Allocation and Functions.pptx
PPTX
DYNAMIC MEMORY ALLOCATION.pptx
PPTX
DYNAMIC MEMORY ALLOCATION.pptx
PPTX
Advance topics of C language
PPTX
pointers in c programming - example programs
PPTX
dynamic_v1-3.pptx
PPTX
final GROUP 4.pptx
PPTX
Dynamic Memory allocation
PPTX
Programming in C sesion 2
PDF
dynamic-allocation.pdf
PPTX
Unit-9zxknaksldmoasdoiasmdmiojoisa(DMA).pptx
PDF
C interview-questions-techpreparation
PPT
cs8251 unit 1 ppt
PPTX
Technical Interview
PPT
Dynamic memory allocation
PPT
Lecture 18 - Pointers
PPTX
visiblity and scope.pptx
PPTX
Dynamic Memory Allocation in C
BBACA-SEM-III-Datastructure-PPT(0) for third semestetr
Lecture 3.3.1 Dynamic Memory Allocation and Functions.pptx
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
Advance topics of C language
pointers in c programming - example programs
dynamic_v1-3.pptx
final GROUP 4.pptx
Dynamic Memory allocation
Programming in C sesion 2
dynamic-allocation.pdf
Unit-9zxknaksldmoasdoiasmdmiojoisa(DMA).pptx
C interview-questions-techpreparation
cs8251 unit 1 ppt
Technical Interview
Dynamic memory allocation
Lecture 18 - Pointers
visiblity and scope.pptx
Ad

More from Hatem Abd El-Salam (12)

PDF
Java- language Lecture 7
PDF
Java- language Lecture 6
PDF
Java- language Lecture 5
PDF
Java- language Lecture 4
PDF
Java- language Lecture 3
PDF
Java- Language Lecture 2
PDF
Java- language Lecture 1
PDF
introduction to embedded systems part 2
PDF
introduction to embedded systems part 1
PDF
C- language Lecture 3
PDF
C- Language Lecture 2
PDF
C-language Lecture 1
Java- language Lecture 7
Java- language Lecture 6
Java- language Lecture 5
Java- language Lecture 4
Java- language Lecture 3
Java- Language Lecture 2
Java- language Lecture 1
introduction to embedded systems part 2
introduction to embedded systems part 1
C- language Lecture 3
C- Language Lecture 2
C-language Lecture 1

Recently uploaded (20)

PDF
International_Financial_Reporting_Standa.pdf
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PPTX
Introduction to pro and eukaryotes and differences.pptx
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PDF
Race Reva University – Shaping Future Leaders in Artificial Intelligence
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
PDF
advance database management system book.pdf
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PPTX
Virtual and Augmented Reality in Current Scenario
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PDF
MICROENCAPSULATION_NDDS_BPHARMACY__SEM VII_PCI .pdf
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
PDF
HVAC Specification 2024 according to central public works department
PPTX
Module on health assessment of CHN. pptx
International_Financial_Reporting_Standa.pdf
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
B.Sc. DS Unit 2 Software Engineering.pptx
Introduction to pro and eukaryotes and differences.pptx
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
Race Reva University – Shaping Future Leaders in Artificial Intelligence
What if we spent less time fighting change, and more time building what’s rig...
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
advance database management system book.pdf
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
Virtual and Augmented Reality in Current Scenario
Uderstanding digital marketing and marketing stratergie for engaging the digi...
Cambridge-Practice-Tests-for-IELTS-12.docx
MICROENCAPSULATION_NDDS_BPHARMACY__SEM VII_PCI .pdf
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
HVAC Specification 2024 according to central public works department
Module on health assessment of CHN. pptx

C- language Lecture 6

  • 1. Lecture 6 C Language ENG : HATEM ABD EL-SALAM
  • 2. • Casting • Dynamic Memory Allocation • Scope and Lifetime Agenda
  • 3. Casting (Cont.) • Type casting is a way to convert a variable from one data type to another data type. For example, if you want to store a long value into a simple integer then you can type cast long to int. You can convert values from one type to another explicitly using the cast operator. There are two types of type casting in c language. 1. Implicit Conversion 2. Explicit Conversion
  • 4. Casting (Cont.) 1-Implicit conversion • Implicit conversions do not required any operator for converted . They are automatically performed when a value is copied to a compatible type in program.
  • 5. Casting (Cont.) 1- Explicit conversion • In c language , Many conversions, specially those that imply a different interpretation of the value, require an explicit conversion.
  • 6. Casting (Cont.) Inbuilt Typecast Functions In C • There are many inbuilt typecasting functions available in C language which performs data type conversion from one type to another.
  • 7. Dynamic Memory Allocation • In C, the exact size of array is unknown until compile time, i.e., the time when a compiler compiles your code into a computer understandable language. So, sometimes the size of the array can be insufficient or more than required. • Dynamic memory allocation allows your program to obtain more memory space while running, or to release it if it's not required. • In simple terms, Dynamic memory allocation allows you to manually handle memory space for your program.
  • 8. Dynamic Memory Allocation (Cont.) • there are 4 library functions under "stdlib.h" for dynamic memory allocation.
  • 9. Dynamic Memory Allocation (Cont.) malloc() • The name malloc stands for "memory allocation". • The function malloc() reserves a block of memory of specified size and return a pointer of type void which can be casted into pointer of any form. • Syntax of malloc()
  • 10. Dynamic Memory Allocation (Cont.) • Here, ptr is pointer of cast-type. The malloc() function returns a pointer to an area of memory with size of byte size. If the space is insufficient, allocation fails and returns NULL pointer. • This statement will allocate either 200 or 400 according to size of int 2 or 4 bytes respectively and the pointer points to the address of first byte of memory.
  • 11. EXAMPLE PROGRAM FOR MALLOC() FUNCTION IN C
  • 12. Dynamic Memory Allocation (Cont.) calloc() • The name calloc stands for "contiguous allocation". • The only difference between malloc() and calloc() is that, malloc() allocates single block of memory whereas calloc() allocates multiple blocks of memory each of same size and sets all bytes to zero. • Syntax of calloc()
  • 13. Dynamic Memory Allocation (Cont.) • example: • This statement allocates contiguous space in memory for an array of 25 elements each of size of float, i.e, 4 bytes.
  • 14. EXAMPLE PROGRAM FOR CALLOC() FUNCTION IN C
  • 15. Dynamic Memory Allocation (Cont.) free() • Dynamically allocated memory created with either calloc() or malloc() doesn't get freed on its own. You must explicitly use free() to release the space. • Syntax of free() • This statement frees the space allocated in the memory pointed by ptr.
  • 16. Dynamic Memory Allocation (Cont.) realloc() • If the previously allocated memory is insufficient or more than required, you can change the previously allocated memory size using realloc(). • Syntax of realloc() • Here, ptr is reallocated with size of newsize.
  • 17. EXAMPLE PROGRAM FOR REALLOC() AND FREE() FUNCTIONS IN C
  • 18. Dynamic Memory Allocation (Cont.) • Difference between static memory allocation and dynamic memory allocation in c.
  • 19. Dynamic Memory Allocation (Cont.) • Difference between malloc() and calloc() functions in c.
  • 21. Scope and Lifetime • Every variable in C programming has two properties: type and storage class. • Type refers to the data type of a variable. And, storage class determines the scope and lifetime of a variable. There are 5 types of storage class: 1. automatic 2. external 3. static 4. Register 5. Constants
  • 22. Scope and Lifetime(Cont.) 1-Local Variable • The variables declared inside the function are automatic or local variables. • The local variables exist only inside the function in which it is declared. When the function exits, the local variables are destroyed. • In the above code, n1 is destroyed when func() exits. Likewise, n gets destroyed when main() exits.
  • 23. Scope and Lifetime(Cont.) Global Variable • Variables that are declared outside of all functions are known as external variables. External or global variables are accessible to any function.
  • 24. Scope and Lifetime(Cont.) 2-External Variable • Suppose, a global variable is declared in file1. If you try to use that variable in a different file file2, the compiler will complain. To solve this problem, keyword extern is used in file2 to indicate that the external variable is declared in another file.
  • 25. Scope and Lifetime(Cont.) 3-Register Variable • The register keyword is used to declare register variables. register variables were supposed to be faster than local variables. • However, modern compilers are very good at code optimization and there is a rare chance that using register variables will make your program faster.
  • 26. Scope and Lifetime(Cont.) 4-Static Variable • A static variable is declared by using keyword static. • The value of a static variable persists until the end of the program.
  • 27. Scope and Lifetime(Cont.) Explanation of program • During the first function call, the value of c is equal to 0. Then, it's value is increased by 5. • During the second function call, variable c is not initialized to 0 again. It's because c is a static variable. So, 5 is displayed on the screen.
  • 28. Scope and Lifetime(Cont.) 4-Constants Variable • A constant is a value or an identifier whose value cannot be altered in a program. For example: 1, 2.5, "C programming is easy", etc. • Here, PI is a constant. Basically what it means is that, PI and 3.14 is same for this program.