Join Regular Classroom : Visit ClassroomTech

Difference between %u and %p format specifiers in C

%p is a format specifier in C programming language used to print the pointer type data. It prints the memory address of the variable in hexadecimal form.

On the other hand, %u is a format specifier in C programming language used to print unsigned integer. If it is used to print the memory address of the variable then it prints the address in unsigned integer form.

Example

/* C program to understand %u and %p */
/* www.codewindow.in */

#include<stdio.h>

// Driver code
int main() {
   	int a = 50;
   	int *p = &a;        // Assigning the address of the variable to the pointer
   	
	printf("The address in Hexadecimal: %p", p);
	printf("\nThe address in Unsigned Integer form: %u", p);
    
    return 0;
}

Output

The address in Hexadecimal: 0x7ffd2da6177c
The address in Unsigned Integer form: 765859708

Explanation
The memory address of integer variable a is stored in pointer variable p. First, we printed the address using “%p” specifier. Output was in hexadecimal form (0x7ffd2da6177c) as described earlier.

Next we printed the address in unsigned integer form. So the output came 765859708 in our case.

(NOTE: Memory allocation address will vary machine to machine and also it can change upon different execution)


Follow Us


You Missed

Also Checkout