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 2 of 3.

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.

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.

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.

Medha said:   1 decade ago
One n is ignored with 4 space because of '*'and another n value is taken.

Nandhu said:   1 decade ago
Please anybody can explain this.

Lakshmidevi said:   1 decade ago
Using * means it skips one variable, so only one n value is taken and another n value is ignored.

Veena said:   1 decade ago
I thought here two n's are not required. Please any one can explain.

Rozario selvanathan said:   1 decade ago
Anyone please make it clear...

Madhusudan said:   1 decade ago
'*' will provide 4 spaces in between 'n=' and 5.


Post your comments here:

Your comments will be displayed after verification.