Question:
How do you print an address?
Answer:
The safest way is to use printf() (or fprintf() or sprintf()) with the %P specification. That prints a void pointer (void*). Different compilers might print a pointer with different formats. Your compiler will pick a format that’s right for your environment.
If you have some other kind of pointer (not a void*) and you want to be very safe, cast the pointer to a void*:
printf( “%Pn”, (void*) buffer ); Source: CoolInterview.com
Address can also be printed using %x. Here is a program which gives the same output for both %p (not %P) and %x. #include<stdio.h> int main() { char *ch; char ch1='b'; ch=&ch1; printf("%p %x",(void *)ch,(void *)ch); getch(); }
Source: CoolInterview.com
Answered by: Syed Baseer Ahmed | Date:
| Contact Syed Baseer Ahmed
by using %u or %p format specifiers we can print the adress But %p used to print the adress as the computer address (Hex format )] %u in integer format Source: CoolInterview.com
Answered by: murali | Date: 3/16/2009
| Contact murali
More simpler way is using & in printf statement...& specifies the address of that variable
#include<stdio.h> void main() { int a; a=10; printf("%d",&a); getch(); } Source: CoolInterview.com
Answered by: Subrahmanya | Date: 9/11/2009
| Contact Subrahmanya
main() { int i=8; printf("%u",&i); getch(); } Source: CoolInterview.com
Answered by: mukul garg | Date: 9/29/2009
| Contact mukul garg
if u want to print the address of variable then just like this main() { int i; printf("address of i is %u",&i); return; } Source: CoolInterview.com
Answered by: Amol Patil | Date: 10/5/2009
| Contact Amol Patil
void main() { int a=7,*p;; p=&a; printf("address of variable a:",p); getch(); } 1.using pointer variable to get the variable address and using printf statement to print the value. Source: CoolInterview.com
Answered by: k.Ganeshwari | Date: 2/13/2010
| Contact k.Ganeshwari
printf("%d",&x); simply prints the address of variable x. Source: CoolInterview.com
Answered by: azmath | Date: 6/3/2010
| Contact azmath
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|