answersLogoWhite

0

%p is used in a printf statement to show the memory address value of a pointer. It does not show the address of the pointer itself. You can see this with the following program:

#include

int main(void){
int* p;
printf("p: %p x: %x x&: %x\n",p,p,&p);
}

This gave me the following output:

p: 0x33d6d0 x: 33d6d0 x&: bf9a8c10

So %p is a way of formatting the value in a pointer to make it obvious that you are referring to a memory location.

User Avatar

Wiki User

15y ago

Still curious? Ask our experts.

Chat with our AI personalities

ViviVivi
Your ride-or-die bestie who's seen you through every high and low.
Chat with Vivi
RafaRafa
There's no fun in playing it safe. Why not try something a little unhinged?
Chat with Rafa
BeauBeau
You're doing better than you think!
Chat with Beau
More answers

The % symbol is used as the modulo (or modulus) operator. Its function is to return the remainder of a while number division operation. That is, x%y is the same as saying y-(int(x/y)*y), thus if x is 10 and y is 3, x%y is 1, the remainder of 10/3.

User Avatar

Wiki User

11y ago
User Avatar

The % token is the modulo operator, not the percent operator. The modulo operator cannot be overloaded for intrinsic types, you can only overload it for user-defined types.

Since the purpose of modulo is to return the remainder of an integer division, you should only provide a modulo operator overload where your user-defined type is intrinsically integral. One such usage might be to provide modulo functionality for an enum. For instance:

#include<iostream>

enum my_enum

{

zero=0, one, two, three, four, five, six, seven, eight, nine, ten

};

my_enum operator% (my_enum a, my_enum b)

{

int i = a;

int j = b;

return(( my_enum ) ( i % j ));

}

int main()

{

my_enum x = ten;

my_enum y = three;

my_enum mod = x%y;

std::cout<<x<<" % "<<y<<" = "<<mod<<std::endl;

}

Output:

10 % 3 = 1

It should be noted that when the result cannot be guaranteed to be converted to a valid user-defined type, you should not provide a modulo operator. In this it can since all possible results for any two enum values are contained within the enum itself.

The same principal can be applied to classes, provided the class is intrinsically integral. If the class is intrinsically floating point, do not use the % operator, overload the fmod() function instead (#include math.h). The % operator is intuitively an integer division operation and if it is to remain intuitive to your users then it must be implemented as such.

User Avatar

Wiki User

11y ago
User Avatar

"%u" is a printf format specifier that says to convert the next argument into a displayable unsigned decimal.

#include <stdio.h>

int main (int, char*) {

int i;

for (i = 1; i <= 10; ++i) {

printf ("i: %u i squared: %u\n", i, i*i);

}

return 0;

}

User Avatar

Wiki User

13y ago
User Avatar

%u gives the value of the memory location of for eg Integer

%u read an unsigned decimal Integer(scanf)

%u print an unsigned decimal Integer(printf)

User Avatar

Wiki User

15y ago
User Avatar

The percent (%) operator in C and C++ is the modulus operator. It returns the first number mod the second number.

a % b is the same as a - int (a / b) * a

User Avatar

Wiki User

14y ago
User Avatar

Add your answer:

Earn +20 pts
Q: Use of Percent in C programming?
Write your answer...
Submit
Still have questions?
magnify glass
imp