what is the difference between %d and %*d in c languaga?

Answer Posted / shivam chaurasia

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
", wid, 42);

output,...
..42


if the form is like this...
printf ("%*d %*d
", a, b);
is undefined behaviour as per the standard, since you should be providing four arguments after the format string, not two (and good compilers like gcc will tell you about this if you bump up the warning level). From C11 7.20.6 Formatted input/output functions:

If there are insufficient arguments for the format, the behavior is undefined.

It should be something like:

printf ("%*d %*d
", 4, a, 4, b);


check this link for extra detail....
http://www.cplusplus.com/reference/cstdio/printf/

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

in case any function return float value we must declare a) the function must be declared as 'float' in main() as well b) the function automatically returned float values c) function before declared 'float' keyword d) all the above

601


How can I call system when parameters (filenames, etc.) Of the executed command arent known until run time?

595


What is register variable in c language?

607


What are # preprocessor operator in c?

632


What is the use of ?

626






Explain what is the benefit of using an enum rather than a #define constant?

725


What is echo in c programming?

559


which of the following statement is wrong a) mes=123.56; b) con='T'*'A'; c) this='T'*20; d) 3+a=b;

2410


What functions are in conio h?

662


What is static and auto variables in c?

568


What is pointer in c?

743


Is it possible to have a function as a parameter in another function?

600


Explain what is wrong with this program statement?

621


int far *near * p; means

3123


Which is the best website to learn c programming?

584