Skip to content

C_Memory_Address.c

ani9977 edited this page Sep 11, 2024 · 1 revision

Here's the updated code with comments and explanations:

#include <stdio.h>  // Includes the standard input-output library for input-output functions

int main() {  // Main function where the program execution starts
    int number = 10;  // Declares an integer variable 'number' and assigns the value 10 to it

    // Prints the memory address of the 'number' variable using the %p format specifier for pointers
    printf("Address of number: %p\n", &number);  // &number retrieves the address of the variable 'number'

    return 0;  // Returns 0, indicating successful program execution
}

Explanation:

  • int number = 10;: This declares an integer variable number and assigns it the value 10.
  • printf("Address of number: %p\n", &number);: The printf function prints the memory address of the number variable.
    • The &number retrieves the memory address of the variable number.
    • The %p format specifier is used to print the address in pointer format (hexadecimal).
  • return 0;: This statement indicates the program completed successfully and returns 0 to the operating system.

This program demonstrates how to use pointers in C by printing the memory address of an integer variable. In C, each variable is stored at a specific location in memory, and you can access that address using the & operator.

Clone this wiki locally