Currently I am learning C language in termux
When i try to compile a simple code of pointers using gcc pointer.c
The code is
#include <stdio.h>
int main(){
int a=57;
printf("The value of a is %d \n",a);
printf("The address of a is %u\n",&a);
return 0;
}
Then I got this error (actually warning) as shown in the image enter image description here
But i see that it compiled my code and it successfully running as shown in the image enter image description here
I want to know why it gives error
CodePudding user response:
You should use %p
instead of %u
to display pointer address. Your code will be:
#include <stdio.h>
int main()
{
int a=57;
printf("The value of a is %d \n",a);
printf("The address of a is %p\n",&a);
return 0;
}
CodePudding user response:
#include <stdio.h>
int main()
{
int a=57;
printf("The value of a is %d \n",a);
printf("The address of a is %p\n",(void*)&a);
return 0;
}
The address of a variable is an integer numeric quantity and the format specifier used to print such a quantity is %p
(for printing address in hexadecimal form) and %lld
(for printing address in decimal form). To print the address of a variable, we use &
along with the variable name.The %p
specifier expects a void*
.