What is the format specifier to use for printing address?
What is the Format Specifier for Printing Address in C?
When working with addresses in the C programming language, it's important to use the correct format specifier to ensure that the address is displayed accurately. The format specifier is a special code used in printf and scanf functions to define the type of data being passed.
Format Specifier for Address in C
In C, the correct format specifier for printing an address is %p. This specifier is specifically designed for pointers, which are variables that store memory addresses.
void main() {
int var = 10;
printf("Address of var: %p", (void*)&var);
}
In this example, %p is used to print the address of the variable var. The & operator is used to obtain the address of the variable.
Remember, it's important to typecast the pointer to (void*) because %p expects a void* type.
Example Usage
void main() {
int num = 42;
int *ptr = #
printf("Address of num: %p", (void*)ptr);
}
This code snippet demonstrates how to use %p to print the address of an integer variable.
Conclusion
Using the correct format specifier is crucial in C programming, especially when dealing with memory addresses. The %p specifier is used to print addresses, ensuring that they are displayed accurately.