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.

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.

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.

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.

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?

PG srinu said:   1 decade ago
I think exactly * will ignore first variable then k l printed.

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
}

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.

Hero said:   8 years ago
Can anyone explain how many spaces were there in the answer?

Loper said:   8 years ago
* means 4 spaces.

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


Post your comments here:

Your comments will be displayed after verification.