top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the difference between %d and %*d in c language?

+5 votes
2,989 views
What is the difference between %d and %*d in c language?
posted Dec 4, 2013 by Anuj Mishra

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+2 votes

The %*d in a printf allows you to use a variable to control the field width, along the lines of:

int wid = 4;
printf ("%*d\n", wid, 42);

which will give you:

..42

(with each of those . characters being a space). The * consumes one argument wid and the d consumes the 42)

answer Dec 4, 2013 by Alok Kumar
Nice explanation...
0 votes

%d gives the original value of the variable and %*d gives the address of the variable.

eg:-

int a=10,b=20;
printf("\n%d%d",a,b);
printf("\n%*d%*d",a,b);

Result is
10 20
1775 1775

Here 1775 is the starting address of the memory allocation for the integer. a and b having same address because of contagious memory allocation.

answer Dec 8, 2013 by Neeraj Pandey
...