C Programming - Input / Output - Discussion

Discussion Forum : Input / Output - Yes / No Questions (Q.No. 3)
3.
Will the following program work?
#include<stdio.h>

int main()
{
    int n=5;
    printf("n=%*d\n", n, n);
    return 0;
}
Yes
No
Answer: Option
Explanation:
It prints n=    5
Discussion:
22 comments Page 1 of 3.

DipikaMore said:   9 years ago
@Ullesh.

#include<stdio.h>

int main()
{
int n=5,l=900,k=0;
printf("n=%*d\n", n, k,l);
return 0;
}

In this program, * will be replaced by the value of n & then next variable here is k will print. For l to print no access specifier. So it will not print anything.

NITESH KHATRI said:   1 decade ago
#include<stdio.h>

int main()
{
int n=5,l=9;
printf("n=%*d\n", n, l);
return 0;
}

What @Medha said is correct, '*' will ignore the first variable and will print the value of l
Try to run this program, all confusion will be cleared.

Ronak said:   1 decade ago
#include<stdio.h>

int main()
{
int n=5,l=9;
printf("n=%*d\n", n, l);
return 0;
}

What vijeth said is correct, '*' will ignore the first variable and will print the value of l.
Try to run this program, all confusion will be cleared.

Kiran S P said:   5 years ago
Can anyone explain this and also interchange " printf("n=%**d\n", n, l);" and explain how it works?

#include<stdio.h>

int main()
{
int n=5,l=9;
printf("n=%**d\n", l, n);
return 0;
}

Crystal said:   1 decade ago
@All.

Try these:

int main()
{
int i=10,j=20,k=30,l=40;
printf("%*d",i);//prints garbage
printf("%*d",i,j,k,l);//prints 2nd integer
printf("%*d",k);//garbage
}

Shashank said:   1 decade ago
* means it specifies the width and gives those no.of spaces.

According to Nitesh's code whatever value you give to n, those many spaces are appended.

Sunitha said:   1 decade ago
Here in printf("n=%*d\n", n, n);

("%*d",n,n) tells the compiler to skip the first n value.

Thus second n value gets printed.

Ullesh Chavadi said:   1 decade ago
#include<stdio.h>

int main()
{
int n=5,l=900,k=0;
printf("n=%*d\n", n, k,l);
return 0;
}

What about this?

Aniruddha said:   8 years ago
* is a suppression character(which is optional after %), It suppresses the conversion that skips the content.

Vijeth said:   1 decade ago
It is right justified to n. , if you use "%-*d" then it will be left justified. n can be any variable.


Post your comments here:

Your comments will be displayed after verification.