. Advertisement .
..3..
. Advertisement .
..4..
There is a wide range of format specifiers in C, each of which serves different functions and purposes. This article will focus on discussing the %p format specifier. This one is often used to print the data of the pointer type.
What Is %p In C?
A format specifier comes in the form of a string, which is employed in the input and output functions. Their formats are determined by the format string.
All format specifiers start with a % character. The %p format specifier works by printing a pointer’s value in C programming. In this function, the P programming language is used to model and specify any protocols in event-driven applications.
It also addresses the asynchronous computation’s challenges, including Heisenbugs and asynchrony. For this reason, the %p specifier works with a pointer while writing a C code. This one can be used with the printf() function as others do.
Code:
#include<stdio.h>
void main()
{
int i=100;
printf("%d\n",i);
int *pointer = &i;
printf("%p\n",i);
printf("%p",pointer);
}
Output:
100
0000000000000064
000000000062FE14
In this example, you need to use the int i=100 to initialize the i integer variable. Then, it is time to print this integer value, which is 100 now. Here, create a pointer pointing towards the i address.
In the next line, the pointer is printed with the %p specifier inside the print() function. You will get the pointer value at 000000000064 as 100 will likely become 64 in hexadecimal.
%p And Other Format Specifiers
%x
%p is useful in printing the pointer value while %x works by printing the hexadecimal ones. There would not be any major difference if you print values with these two specifiers.
The difference will often arise in the type of values. %p pints leading zeros while %x doesn’t.
Code:
#include<stdio.h>
main() {
int x = 59;
printf("Value using %%p: %p\n", x);
printf("Value using %%x: %x\n", x);
}
Output:
Value using %p: 000000000000003B
Value using %x: 3b
%u
The u% format specifier is often executed to fetch values from a variable’s address. This variable tends to have an unsigned decimal integer in its memory. For this reason, it will print the unsigned variable in the printf() function.
%d
You can implement this format specifier to represent integer values. In the printf() function, it prints the included integer value in the variable.
%f
Its main function is to represent fractional values. Within the printf() function, %f prints all floating or fractional values in the variable.
%c
%c works by representing and printing characters in a variable.
%s
As can be hinted from the s character, this one represents and prints strings in the character array variable.
%ld
This last format specifier represents long integer values and prints them in the variable.
Conclusion
The article has introduced you to the %d format specifier in C programming together with other specifiers. Each deals with different types of value. Thus, you should choose a suitable one.
Leave a comment