printf %p vs. %x 
Author Message
 printf %p vs. %x

I don't understand the difference between using %p and %x.  It seems they
give the same answer.  

#include<stdio.h>

int main(void){

   char a;
   char b;
   int x = 100;
   int * p_x = &x;

   printf("%p %p %p\n", &a, p_x, &b);

   printf("%x %x %x\n", &a, p_x, &b);
   return 0;

Quote:
}

prints (in this case)


Sat, 20 Aug 2005 13:26:54 GMT  
 printf %p vs. %x

Quote:

> I don't understand the difference between using %p and %x.  It seems they
> give the same answer.  

> #include<stdio.h>

> int main(void){

>    char a;
>    char b;
>    int x = 100;
>    int * p_x = &x;

>    printf("%p %p %p\n", &a, p_x, &b);

>    printf("%x %x %x\n", &a, p_x, &b);
>    return 0;

> }

> prints (in this case)

prints (in this case)

0xbffffc3b 0xbffffc34 0xbffffc3a
bffffc3b bffffc34 bffffc3a



Sat, 20 Aug 2005 13:34:15 GMT  
 printf %p vs. %x

Quote:

> I don't understand the difference between using %p and %x.  It seems they
> give the same answer.  

%p prints a void * pointer.
%x prints a hexadecimal integer.
On some systems, these happen to correspond in format, but on
other systems they vary.

Quote:
> #include<stdio.h>

> int main(void){

>    char a;
>    char b;
>    int x = 100;
>    int * p_x = &x;

>    printf("%p %p %p\n", &a, p_x, &b);

It is incorrect to pass a char * or an int * as the argument for
%p.  You must cast all of these to void * for correctness.

Quote:
>    printf("%x %x %x\n", &a, p_x, &b);

It is incorrect to pass a char * or an int * as the argument for
%x.  You must pass an unsigned int, not a pointer.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
 \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


Sat, 20 Aug 2005 13:40:44 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. (void)printf() vs. printf()

2. Re(2): Re(2): (void)printf() vs. printf()

3. Re(2): (void)printf() vs. printf()

4. replacing printf with my printf, then calling C lib's printf

5. cout vs printf()

6. printf vs fprintf

7. 2 printf's vs. a ternary

8. cout vs printf

9. printf vs. vprintf

10. printf vs fprintf

11. printf() vs. puts()

12. printf vs. cout

 

 
Powered by phpBB® Forum Software