Memory Address :-
When a variable is created in C, a memory address is assigned to the variable. The memory address is the
location of where the variable is stored on the computer. When we assign a value to the variable, it is stored in
this memory address.
To access it, use the reference operator (&), and the result represents where the variable is stored:
Example :- int myAge = 43;
printf("%p", &myAge); // Outputs 0x7ffe5367e044
Note :-The memory address is in hexadecimal form (0x..). You will probably not get the same result in your
program, as this depends on where the variable is stored on your computer.
Why We Uses memory address:- Pointers are important in C, because they allow us to manipulate the data in
the computer's memory - this can reduce the code and improve the performance.
The Importance of Memory Management in C :-
This chapter explains dynamic memory management in C. The C programming language provides several
functions for memory allocation and management. These functions can be found in the <stdlib.h> header file.
If you want more control over all this, you need dynamic storage allocation. C supports dynamic storage
allocation, which is the ability to reserve memory as you need it and free that memory as soon as you're finished
using it. Many programming languages have automatic memory allocation and garbage collection that handle
these memory management tasks. C, though, allows (and in some cases requires) you to be explicit about
memory allocation with the following key functions from the standard C library:
Sr.No. Function Description
1 void *calloc(int num, int Alternate memory allocation options in C are calloc, which
size); also clears the memory when it's reserved.
2 void free(void *address); This function releases a block of memory block specified by address.
3 void *malloc(size_t size); This function allocates an array of num bytes and leave them
uninitialized
, 4 void *realloc(void *address, This function re-allocates memory extending it upto newsize.
int newsize);
Example :- #include <stdio.h>
int main(void)
{
// declare variables
int a;
float b;
char c;
printf("Address of a: %p\n", &a);
printf("Address of b: %p\n", &b);
printf("Address of c: %p\n", &c);
return 0;
}
Output :- Address of a: 0x7ffee63c8620
Address of b: 0x7ffee63c8624
Address of c: 0x7ffee63c861f
File Handling :-
So far the operations using the C program are done on a prompt/terminal which is not stored anywhere. But in
the software industry, most programs are written to store the information fetched from the program. One such
way is to store the fetched information in a file.
Different operations that can be performed on a file are :-
Creation of a new file (fopen() with attributes as “a” or “a+” or “w” or “w+”)
Opening an existing file (fopen())
Reading from file (fscanf() or fgets())
Writing to a file (fprintf() or fputs())
Moving to a specific location in a file (fseek(), rewind())
When a variable is created in C, a memory address is assigned to the variable. The memory address is the
location of where the variable is stored on the computer. When we assign a value to the variable, it is stored in
this memory address.
To access it, use the reference operator (&), and the result represents where the variable is stored:
Example :- int myAge = 43;
printf("%p", &myAge); // Outputs 0x7ffe5367e044
Note :-The memory address is in hexadecimal form (0x..). You will probably not get the same result in your
program, as this depends on where the variable is stored on your computer.
Why We Uses memory address:- Pointers are important in C, because they allow us to manipulate the data in
the computer's memory - this can reduce the code and improve the performance.
The Importance of Memory Management in C :-
This chapter explains dynamic memory management in C. The C programming language provides several
functions for memory allocation and management. These functions can be found in the <stdlib.h> header file.
If you want more control over all this, you need dynamic storage allocation. C supports dynamic storage
allocation, which is the ability to reserve memory as you need it and free that memory as soon as you're finished
using it. Many programming languages have automatic memory allocation and garbage collection that handle
these memory management tasks. C, though, allows (and in some cases requires) you to be explicit about
memory allocation with the following key functions from the standard C library:
Sr.No. Function Description
1 void *calloc(int num, int Alternate memory allocation options in C are calloc, which
size); also clears the memory when it's reserved.
2 void free(void *address); This function releases a block of memory block specified by address.
3 void *malloc(size_t size); This function allocates an array of num bytes and leave them
uninitialized
, 4 void *realloc(void *address, This function re-allocates memory extending it upto newsize.
int newsize);
Example :- #include <stdio.h>
int main(void)
{
// declare variables
int a;
float b;
char c;
printf("Address of a: %p\n", &a);
printf("Address of b: %p\n", &b);
printf("Address of c: %p\n", &c);
return 0;
}
Output :- Address of a: 0x7ffee63c8620
Address of b: 0x7ffee63c8624
Address of c: 0x7ffee63c861f
File Handling :-
So far the operations using the C program are done on a prompt/terminal which is not stored anywhere. But in
the software industry, most programs are written to store the information fetched from the program. One such
way is to store the fetched information in a file.
Different operations that can be performed on a file are :-
Creation of a new file (fopen() with attributes as “a” or “a+” or “w” or “w+”)
Opening an existing file (fopen())
Reading from file (fscanf() or fgets())
Writing to a file (fprintf() or fputs())
Moving to a specific location in a file (fseek(), rewind())