Q:

Golang program to print the variable address using \'%p\' format specifier

belongs to collection: Golang Basic Programs

0

In this program, we will create an integer number and assign the address of the variable to the pointer and then we will print the address of the variable using the "%p" format specifier on the console screen.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to print the variable address using the "%p" format specifier is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Golang program to print the variable address
// using "%p" format specifier

package main

import "fmt"

func main() {
	var num int = 20
	var ptr *int

	ptr = &num

	fmt.Printf("Value   : %d\n", *ptr)
	fmt.Printf("Address : %p\n", &num)
	fmt.Printf("Address : %p\n", ptr)
}

Output:

Value   : 20
Address : 0xc000100010
Address : 0xc000100010

Explanation:

In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt package then we can use a function related to the fmt

In the main() function, we created an integer variable num and a pointer ptr. After that, we assigned the address of variable num to the pointer ptr and then we printed the address of the variable using the "%p" format specifier on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Golang Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Golang program to demonstrate the io.WriteString()... >>
<< Golang program to print the string in double-quote...